title: How to use ElasticSearch for product search (filtering) in eShop?
body: <p>I'm new in ElasticSearch!
  I have an eShop (using Laravel) with more than 1,000,000 products and each product has some properties in <code>product</code> table like name, description, price , ... and some other properties in other tables like <code>categories</code>, <code>options</code>, <code>tags</code>, <code>addresses</code>, <code>rating</code>, <code>brands</code>, ... which are in separated tables and they will join to products by <code>many-to-many</code> relationships!</p>
  <p>so I want to have a very fast and reliable search for products with many filters! like amzon! for example user would be able to find products with special categories, tags, options , ...! as you now in mysql I have to multiple use something like <code>WHERE EXISTS</code>, <code>INNER JOIN</code> and  ... and since the products number is too big these queries are so costly and slow! so I've found that elastic search has a solution for this situation! it loves to work with big data!</p>
  <p>I've read about it! now I have an idea but I'm not sure is it a correct way?</p>
  <p>I say we can store each <code>categories</code>, <code>options</code>, <code>tags</code>, <code>addresses</code>, <code>rating</code>, <code>brands</code> as a json object in elastic search just on each <strong>UPDATE/STORE</strong> event in the laravel appilication! in mysql they have separated tables and their many-to-many relationships! but we don't have to define an index for each of them (tables) and repeat to join them in elastic again same as mysql way!</p>
  <p>in elastic search we will have this pattern for products:</p>
  <pre><code>&quot;id&quot; : 782,
  &quot;title&quot; : &quot;dmorar&quot;,
  &quot;price&quot; : &quot;11000.00&quot;,
  &quot;options&quot; : [ the ids of the product's tags ],
  &quot;categories&quot; : [ the ids of the product's categories],
  &quot;tags&quot; : [ the ids of the product's tags ],
  &quot;addresses&quot; : [ the ids of the product's addresses]
  </code></pre>
  <p>this is an example:</p>
  <pre><code>  {
      &quot;_index&quot; : &quot;so_product&quot;,
      &quot;_type&quot; : &quot;_doc&quot;,
      &quot;_id&quot; : &quot;1099&quot;,
      &quot;_score&quot; : 1.0,
      &quot;_source&quot; : {
        &quot;id&quot; : 782,
        &quot;title&quot; : &quot;dmorar&quot;,
        &quot;price&quot; : &quot;11000.00&quot;,
        &quot;created_at&quot; : &quot;2020-07-30T14:01:09.000000Z&quot;,
        &quot;updated_at&quot; : &quot;2020-07-30T14:01:09.000000Z&quot;,
        &quot;options&quot; : [
          75,
          955,
          480,
          351,
          285
        ],
        &quot;categories&quot; : [
          944,
          421
        ],
        &quot;tags&quot; : [
          210,
          198,
          648,
          976,
          10,
          553,
          624,
          967,
          726,
          121,
          797
        ],
        &quot;addresses&quot; : [
          119,
          43,
          140,
          1713,
          855,
          1515,
          958
        ]
      }
    },
    {
      &quot;_index&quot; : &quot;so_product&quot;,
      &quot;_type&quot; : &quot;_doc&quot;,
      &quot;_id&quot; : &quot;1344&quot;,
      &quot;_score&quot; : 1.0,
      &quot;_source&quot; : {
        &quot;id&quot; : 429,
        &quot;title&quot; : &quot;ethyl.wehner&quot;,
        &quot;price&quot; : &quot;89000.00&quot;,
        &quot;created_at&quot; : &quot;2020-07-30T13:59:02.000000Z&quot;,
        &quot;updated_at&quot; : &quot;2020-07-30T13:59:02.000000Z&quot;,
        &quot;options&quot; : [
          121,
          195,
          348
        ],
        &quot;categories&quot; : [
          372,
          315,
          869,
          544,
          645,
          803,
          564
        ],
        &quot;tags&quot; : [
          347,
          797
        ],
        &quot;addresses&quot; : [
          609,
          1477,
          187,
          1479
        ]
      }
    },
    {
      &quot;_index&quot; : &quot;so_product&quot;,
      &quot;_type&quot; : &quot;_doc&quot;,
      &quot;_id&quot; : &quot;1370&quot;,
      &quot;_score&quot; : 1.0,
      &quot;_source&quot; : {
        &quot;id&quot; : 358,
        &quot;title&quot; : &quot;cicero.casper&quot;,
        &quot;price&quot; : &quot;67000.00&quot;,
        &quot;created_at&quot; : &quot;2020-07-30T13:58:37.000000Z&quot;,
        &quot;updated_at&quot; : &quot;2020-07-30T13:58:37.000000Z&quot;,
        &quot;options&quot; : [
          665,
          28,
          488,
          384,
          547,
          877
        ],
        &quot;categories&quot; : [
          508,
          201
        ],
        &quot;tags&quot; : [
          325,
          472,
          595,
          797,
          943,
          948,
          803,
          136,
          288,
          275,
          574,
          390
        ],
        &quot;addresses&quot; : [
          691,
          1637,
          534,
          770,
          499,
          1086,
          430,
          1365,
          1325
        ]
      }
    }
  </code></pre>
  <p>Buy this way a search query like this can give a full control to user to use each filter which he wants and it is fast:</p>
  <pre><code>GET so_product/_search
  {
    &quot;query&quot;: {
        &quot;bool&quot;: {
        &quot;must&quot;: [
          {
              &quot;term&quot;: {
              &quot;options&quot;: 5
              }
          }, 
          {
              &quot;term&quot;: {
              &quot;options&quot;: 695
              }
          } ,
          {
              &quot;term&quot;: {
              &quot;categories&quot;: 523
              }
          }
           ,
          {
              &quot;term&quot;: {
              &quot;addresses&quot;: 116
              }
          } 
          ,
          {
              &quot;term&quot;: {
              &quot;tags&quot;: 797
              }
          } 
        ]
      }
    }
  }
  </code></pre>
  <p>But as I said before I'm not sure this is the correct way or not? if it is not correct what is the standard way? how does amazon implement this search filters?</p>