title: ElasticSearch usage with MySQL
body: <p>I'm using ElasticSearch for the search component of a site. The data that is being indexed and eventually searched is the same data that is being saved in a MySQL DB. </p>
  
  <p>My approach to this is to add/delete/modify data in the index when the corresponding CRUD MySQL operation happens.</p>
  
  <p>For instance, a create operation looks something like this:</p>
  
  <pre><code>public function savePost(Request $request) {
      //Firstly, create the object and save it to MySQL
      $post = new Post();
      $post-&gt;title = $request-&gt;title;
      $post-&gt;body = $request-&gt;body;
      //...
      //and so on
      $post-&gt;save();
  
      //Secondly, index this new data:
      $elasticSearchClient = ClientBuilder::create()-&gt;build();
  
      $params = [
          'index' =&gt; 'some_index_elasticsearch',
          'id' =&gt;  $post-&gt;id,
          'type' =&gt; 'post',
          'timestamp' =&gt; time(),
          'body' =&gt; [
              'id' =&gt; $post-&gt;id,
              'title' =&gt; $post-&gt;title,
              'body' =&gt; $post-&gt;body,
              //... and so on
          ],
      ];
  
      $elasticSearchClient-&gt;index($params);
  
  }
  </code></pre>
  
  <p>If the data is deleted/updated in MySQL I'd just delete it or update it from the index.</p>
  
  <p>Is this the right approach to using MySQL with ElasticSearch (or any other comparable technology like Sphinx)? or would you recommend a better approach to using MySQL as a more of a data source for ElasticSearch? (which really isn't happening at all here because there is no interaction between ElasticSearch and MySQL at all).</p>
  
  <p>I'm using <a href="https://github.com/elastic/elasticsearch-php" rel="nofollow noreferrer">https://github.com/elastic/elasticsearch-php</a> to interact with ElasticSearch if it makes any difference.</p>
  
  <p>Just to clarify: this approach does work so far - I'm just not sure if it is the <em>right</em> way, or if anyone can see problems that I may run into with this way of doing things.</p>