# How to search aggregations in ES?

**URL:** <https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930>\
**Category:** Elasticsearch\
**Created:** [September 26, 2018, 6:01am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930 "2018-09-26T06:01:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Redkin](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@Redkin](https://discuss.elastic.co/u/Redkin)\
**Post date:** [September 26, 2018, 6:01am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/1 "2018-09-26T06:01:34Z")

</div>

I have a books index which contains an array of tags (with both text/keyword types), i'd like to offer an autocomplete for tags so users type "ro" and it returns "romance" or "rock and roll".

Here's my mapping:

```auto
/books {
 ...
  tags: {
    type: 'text',
    field: {
      keyword: {type: 'keyword'}
    }
  }
}

```

Example book

```auto
{ name: "foo", tags: ['romance', 'story', 'fiction'] }

```

My aggregation for tags:

```auto
      {
      size: 0,
      aggregations: {
        options: {
          terms: {
            field: `tags.keyword`,
            size: 20
          }
        }
      }

```

The problem is i get other tags not containing "ro" but they simply happened to be in same documents that have other "ro" tags

How can I only get all distinct tags that match "ro"?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 27, 2018, 8:39am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/2 "2018-09-27T08:39:30Z")

</div>

This is not entirely trivial to do with the aggregations API. Have you considered using the [completion suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) instead? It was designed for this exact use case.

---

<div class="post-metadata">

**Author:** ![pranav24](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pranav24](https://discuss.elastic.co/u/pranav24)\
**Post date:** [September 27, 2018, 9:57am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/3 "2018-09-27T09:57:17Z")

</div>

> [@abdon](#):
>
> This is not entirely trivial to do with the aggregations API. Have you considered using the [completion suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) instead? It was designed for this exact use case.

Is it better to use completion suggester or Edge ngrams tokenizer for autocomplete process?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 27, 2018, 10:03am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/4 "2018-09-27T10:03:12Z")

</div>

The advantage of the completion suggester is that it will load the suggestions in memory. As a result, the suggestions will be returned really quickly; typically faster than a query using edge ngrams would do.

---

<div class="post-metadata">

**Author:** ![pranav24](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pranav24](https://discuss.elastic.co/u/pranav24)\
**Post date:** [September 27, 2018, 10:12am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/5 "2018-09-27T10:12:20Z")

</div>

I failed using Highlighting in Completion Suggestion, [Comparison of different methods for autocompletion](http://rea.tech/implementing-autosuggest-in-elasticsearch) also mentions that Completion Suggester fails to support Highlighting. Is there a way to overcome this issue?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 27, 2018, 10:24am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/6 "2018-09-27T10:24:55Z")

</div>

I don't think the completion suggester supports highlighting. If you need that, you may be better off with a search-based approach.

---

<div class="post-metadata">

**Author:** ![pranav24](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pranav24](https://discuss.elastic.co/u/pranav24)\
**Post date:** [September 28, 2018, 7:02am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/7 "2018-09-28T07:02:29Z")

</div>

Edge ngram fails to suggest when searching with terms containing symbols. I know whitespace analyzer will solve this issue while using it in different query. how do i mention "whitespace" tokenizer within edge ngram custom analyzer.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 28, 2018, 8:23am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/8 "2018-09-28T08:23:52Z")

</div>

The documentation has an example of how to configure a custom analyzer: [https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html#\_example\_configuration\_5](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html#_example_configuration_5)

Just replace `"tokenizer": "standard"` in that example with `"tokenizer": "whitespace"` (and of course apply the filters that you want to use instead of those in the example).

---

<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 26, 2018, 8:30am UTC](https://discuss.elastic.co/t/how-to-search-aggregations-in-es/149930/9 "2018-10-26T08:30:17Z")

</div>

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