# ElasticSearch - how to use search-as-you-type for multivalued fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-how-to-use-search-as-you-type-for-multivalued-fields/249270>\
**Category:** Elasticsearch\
**Created:** [September 20, 2020, 5:59pm UTC](https://discuss.elastic.co/t/elasticsearch-how-to-use-search-as-you-type-for-multivalued-fields/249270 "2020-09-20T17:59:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ondra\_N](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ondra_n/32/75894_2.png) [@Ondra\_N](https://discuss.elastic.co/u/Ondra_N)\
**Post date:** [September 20, 2020, 5:59pm UTC](https://discuss.elastic.co/t/elasticsearch-how-to-use-search-as-you-type-for-multivalued-fields/249270/1 "2020-09-20T17:59:37Z")

</div>

I am messing around with the newly added `search-as-you-type` field type.

> **[Search-as-you-type field type | Elasticsearch Reference \[7.x\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html)**

Having documents with following structure

```auto
doc_1
...
"nickname": ["Super", "Terrible", "Mediocre"]
...
doc_2
...
"nickname": ["Grande", "Perfect", "Superior"]
....

```

For a search-as-you-type usecase I am trying to aggregate the results using terms aggregation and order the buckets by maximum `_score` for each bucket. This allows me to get the unique values that are most relevant for a single valuedfield. However sometimes the field is multivalued like `"nickname": ["Super", "Terrible", "Mediocre"]` , when searching for `Super` like in the query below, doing a terms aggregation sorted by max score now includes buckets `Super`, `Terrible`,`Grande`, `Perfect` and `Mediocre` with high \_score, because the \_score is the same for the whole document and all elements in the multivalued field are aggregated. I would like the aggregation to be limited only on the `Super` and `Superior` buckets which are the `inner_hits`. Is it it possible to limit the aggregation only to the hits inside the multivalued fields, excluding other buckets from the aggregation? It seems like `nested` datatype and `inner_hits` should help, but i fail to achieve `terms` aggregation over the `inner_hits`.  
This issue ([https://github.com/elastic/elasticsearch/issues/17376](https://github.com/elastic/elasticsearch/issues/17376)) says that it is not possible to aggregate `inner_hits` , what is the intended solution to this, should I just index the nested entities separetely in another index where I can aggregate as usual?

What I am trying to achieve is getting most relevant results which actually exist in the index so I can do some further filtering of the documents in another query.

The query targeting `search-as-you-type` field and its subfields. I would like to have a similar results with but only with inner\_hits buckets present in the results. Is it possible?

```auto
POST /target_index/_search
{
  "timeout": "5000s",
  "_source": "nickname",
  "query": {
    "multi_match": {
      "type": "phrase_prefix",
      "query": "Super",
      "fields": [
        "nickname.autocomplete",
        "nickname.autocomplete._2gram",
        "nickname.autocomplete._3gram"
      ]
    }
  },
  "aggs": {
    "termAgg": {
      "terms": {
        "field": "nickname.keyword",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "script": "_score"
          }
        }
      }
    }
  }
}

```

---

<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:** [October 18, 2020, 6:00pm UTC](https://discuss.elastic.co/t/elasticsearch-how-to-use-search-as-you-type-for-multivalued-fields/249270/2 "2020-10-18T18:00:04Z")

</div>

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