title: Sort a nested array and return top 10 in elastic
body: <p>I have a nested data type in an elastic index and want to sort this ascending for all returned results. I have tried the following:</p>
  
  <pre><code>GET indexname/_search
  {
                  "_source" : ["m_iTopicID", "m_iYear", "m_Companies"],
                  "query": {
                      "terms":{
                          "m_iTopicID": [11,12,13]
                      }
                  },
     "sort" : [
         {
            "m_Companies.value" : {
               "order" : "asc",
               "nested_path" : "m_Companies"
            }
         }
      ]
  }
  </code></pre>
  
  <p>The mapping of the index as follows:</p>
  
  <pre><code>{
    "indexname": {
      "mappings": {
        "topicyear": {
          "properties": {
            "m_Companies": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "m_People": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "m_Places": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "m_Subtopics": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "m_fActivation": {
              "type": "float"
            },
            "m_iDocBodyWordCnt": {
              "type": "long"
            },
            "m_iNodeID": {
              "type": "long"
            },
            "m_iTopicID": {
              "type": "long"
            },
            "m_iYear": {
              "type": "long"
            },
            "m_szDocID": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "m_szDocTitle": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "m_szGeo1": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "m_szSourceType": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "value": {
                  "type": "float"
                }
              }
            },
            "m_szSrcUrl": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "m_szTopicNames": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>This returns all topics with ID 11, 12 or 13 with a list of m_Companies... but the lists aren't sorted ascending by the value field.</p>
  
  <p>I would then like to only return the top 10 of each list. So the list doesn't return hundreds like currently but just n. If I can't achieve this option I will just obtain the top 10 at the front-end with a javascript splice(0,10) but it would be great if elastic could do this for me.</p>
  
  <p>Thanks in advance.</p>