title: Elasticsearch: sort documents on nested field
body: <p>I have severals doc and each doc contains nested fields and I want to order by position depending on thematic id <br/><br/>
  Document1 : </p>
  
  <pre><code>{
  ...
      "thematics":[
          {
              "id": 1,
              "position": 100
          },
          {
               "id": 2,
               "position": 1
          }
      ]
  }
  </code></pre>
  
  <p>Document2:</p>
  
  <pre><code>{
  ...
      "thematics":[
         {
               "id": 2,
               "position": 3
          }
      ]
  }
  </code></pre>
  
  <p>Document3:</p>
  
  <pre><code>{
  ...
      "thematics":[
         {
               "id": 1,
               "position": 40
          }
      ]
  }
  </code></pre>
  
  <p>For example, I would like to get only documents with thematics which contains id = 2
  So I did something like that</p>
  
  <pre><code>$filter = BoolQuery();
  ...
  $filter-&gt;addMust(new Query\Term(["thematics.id" =&gt; 2]));
  </code></pre>
  
  <p>And then when I want to apply sort method on position where thematic id = 2 and not something else.</p>
  
  <p>I tried something like that :</p>
  
  <pre><code>case 'atp': // asc thematic position
              $sort = [
                  "_score",
                  [
                    "thematics.position" =&gt; [
                        "order" =&gt; "asc",
                        "missing" =&gt; 0,
                    ],
                  ],
              ];
              break;
  ...
   $this-&gt;setSort($sort);  // call parent method setSort(array()) of elastica-ruflin
  </code></pre>
  
  <p>Response example :</p>
  
  <p>First case expected : 
  If I want to display all documents from thematic 1 the order must be :
  Document3 then Document1.</p>
  
  <p>Second case expected :
  If I want to display all documents from thematic 2 the order must be :
  Document1 then Document2.</p>
  
  <p>But for now what I've got is :<br/>
  - First case : Document3, Document1<br/>
  - Second case : Document2, Document1</p>
  
  <p>I'm guessing that it takes the first thematic's position of document1 to sort in both cases.</p>
  
  <p>Edit :
  I tried to change de mapping with a nested type</p>
  
  <pre><code> thematics:
       type: nested
       properties:
       label: { type: string, index: not_analyzed }
       slug: { index: not_analyzed }
       name: { type: string, index: not_analyzed }
       position: { type: integer }
       id: { type: integer }
  </code></pre>
  
  <p>And the query but still not working</p>
  
  <pre><code>{
  "query": {
      "function_score": {
          "query": {
              "bool": {
                  "must": [
                      {
                          "bool": {
                              "must": [
                                  {
                                      "match_all": {}
                                  }
                              ]
                          }
                      }
                  ],
                  "filter": [
                      {
                          "bool": {
                              "must": [
                                  {
                                      "term": {
                                          "is_searchable": true
                                      }
                                  },
                                  {
                                      "nested": {
                                          "path": "thematics",
                                          "query": {
                                              "term": {
                                                  "thematics.id": {
                                                      "value": 2
                                                  }
                                              }
                                          }
                                      }
                                  },
                                  {
                                      "exists": {
                                          "field": "valuation"
                                      }
                                  },
                                  {
                                      "bool": {
                                          "should": [
                                              {
                                                  "bool": {
                                                      "must": [
                                                          {
                                                              "exists": {
                                                                  "field": "valuation.translations.fr.title"
                                                              }
                                                          }
                                                      ]
                                                  }
                                              },
                                              {
                                                  "bool": {
                                                      "must": [
                                                          {
                                                              "exists": {
                                                                  "field": "valuation.translations.en.title"
                                                              }
                                                          }
                                                      ]
                                                  }
                                              },
                                              {
                                                  "bool": {
                                                      "must": [
                                                          {
                                                              "term": {
                                                                  "commercial_subcategory.category.id": 33
                                                              }
                                                          }
                                                      ]
                                                  }
                                              }
                                          ]
                                      }
                                  }
                              ]
                          }
                      }
                  ]
              }
          },
          "boost_mode": "multiply",
          "functions": [
              {
                  "field_value_factor": {
                      "field": "booster",
                      "modifier": "square"
                  }
              }
          ]
      }
  },
  "sort": [
      "_score",
      {
          "thematics.position": {
              "order": "asc",
              "missing": 0,
              "mode": "min",
              "nested_filter": {
                  "term": {
                      "thematics.id": {
                          "value": 2
                      }
                  }
              }
          }
      }
  ]
  }
  </code></pre>
  
  <p><strong>Edit2</strong>: I get around the problem.
  I changed my mapping so each document looks like that :</p>
  
  <pre><code>{
  ...
      "thematics":[
          "1": {
              "id": 1,
              "position": 100
           },
          "2": {
               "id": 2,
               "position": 1
           }
      ]
  }
  </code></pre>
  
  <p>And then I apply a <code>Bool Query must / Exists Filter</code> on "thematics.".$thematicId</p>
  
  <p>And finally, my sort method looks like this :</p>
  
  <pre><code>case 'atp': // asc thematic position
          $sort = [
              "_score",
              [
                "thematics." . $thematicId . ".position" =&gt; [
                    "order" =&gt; "asc",
                    "missing" =&gt; 0,
                ],
              ],
          ];
          break;
   ...
   $this-&gt;setSort($sort);  // call parent method setSort(array()) of elastica-ruflin
  </code></pre>