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

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

Having documents with following structure

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) 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?

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"
          }
        }
      }
    }
  }
}

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