# Inner\_hits, top\_hits, span !? on nested field performance

**URL:** <https://discuss.elastic.co/t/inner-hits-top-hits-span-on-nested-field-performance/121864>\
**Category:** Elasticsearch\
**Created:** [February 28, 2018, 1:59pm UTC](https://discuss.elastic.co/t/inner-hits-top-hits-span-on-nested-field-performance/121864 "2018-02-28T13:59:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![b789](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/b789/32/5199_2.png) [@b789](https://discuss.elastic.co/u/b789)\
**Post date:** [February 28, 2018, 1:59pm UTC](https://discuss.elastic.co/t/inner-hits-top-hits-span-on-nested-field-performance/121864/1 "2018-02-28T13:59:40Z")

</div>

hi,  
i've this query:

```
{
  "query": {
"bool": {
  "must": [
    {
      "bool": {
        "should": [
          {
            "nested": {
              "path": "nestedfield",
              "query": {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "nestedfield.f1": {
                          "query": "term1 term2 term3",
                          "minimum_should_match": "2"
                        }
                      }
                    },
                    {
                      "function_score": {
                        "query": {
                          "match": {
                            "nestedfield.f1": {
                              "query": "term1 term2 term3",
                              "fuzziness": "auto",
                              "minimum_should_match": "2",
                              "prefix_length": "3"
                            }
                          }
                        },
                        "boost": 1,
                        "score_mode": "first",
                        "boost_mode": "multiply",
                        "functions": [
                          {
                            "weight": 2,
                            "filter": {
                              "span_near": {
                                "clauses": [
                                  {
                                    "span_multi": {
                                      "match": {
                                        "fuzzy": {
                                          "nestedfield.f1": {
                                            "value": "term1",
                                            "fuzziness": "auto"
                                          }
                                        }
                                      }
                                    }
                                  },
                                  {
                                    "span_multi": {
                                      "match": {
                                        "fuzzy": {
                                          "nestedfield.f1": {
                                            "value": "term2",
                                            "fuzziness": "auto"
                                          }
                                        }
                                      }
                                    }
                                  },
                                  {
                                    "span_multi": {
                                      "match": {
                                        "fuzzy": {
                                          "nestedfield.f1": {
                                            "value": "term3",
                                            "fuzziness": "auto"
                                          }
                                        }
                                      }
                                    }
                                  }
                                ],
                                "slop": 2,
                                "in_order": true
                              }
                            }
                          }
                        ]
                      }
                    }
                  ],
                  "minimum_should_match": "1"
                }
              },
              "inner_hits": {
                "name": "nestedfield_hit",
                "size": 1,
                "sort": [
                  {
                    "_score": {
                      "order": "desc"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  ]
}
  },
  "from": 0,
  "size": 10,
  "aggs": {
"name": {
  "nested": {
    "path": "nestedfield"
  },
  "aggs": {
    "name": {
      "terms": {
        "field": "nestedfield.f2"
      },
      "aggs": {
        "name": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}
  }
}

```

the problem is that the performance aren't good enough.  
the thing that i don't understand is why removing one of the following parts improve the performances by ten times.  
To obtain this i can remove the inner\_hits in the aggregations, the top\_hits on the nested query or span queries in the functions scores.

I cant see why removing one of this parts lead to a ten time improvement on the performances.  
i get that removing a part of the query will reduce the work to do but in this situation is faster to send a second query just for aggregations and i can't understand why.

I suspect that there some hidden relation between the three parts but i don't know where.  
A strange thing that i noticed is that the top hits in the aggregation present a inner\_hits field with no real value at the same level of the \_source, i don't know if this is expected.

the actual query have other parts but this is enough to demonstrate the problem.

p.s. the functions score with the spans in there just to simulate a match\_phrase with fuzzy behavior so if ther's a better way i'm open to suggestions.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 28, 2018, 2:00pm UTC](https://discuss.elastic.co/t/inner-hits-top-hits-span-on-nested-field-performance/121864/2 "2018-03-28T14:00:28Z")

</div>

Closed on March 28, 2018, 2:00pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
