# Autocomplete for terms aggregations

**URL:** <https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914>\
**Category:** Elasticsearch\
**Created:** [August 13, 2020, 4:10pm UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914 "2020-08-13T16:10:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 13, 2020, 4:10pm UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/1 "2020-08-13T16:10:45Z")

</div>

Hi,

There is a search-as-you-type fields since 7.x but is it possible to get similar results for filtered aggregations for autocomplete?

Example you have documents with categories on it:

- holiday
- vacation
- images

There are thousands of different categories in different combinations.  
What would be the best approach to filter and look for a term not with a prefix but also with infix. I see that it is mentioned on the search as you type documentation page but there is no example or key for it.

Searching for documents is easy with search-as-you-type, but I am interested in getting a list of keywords matching any of the ngrams of a specific field and not the documents.

```auto
        GET indexname/_search
        {
          "size": 10, 
          "query": {
            "multi_match": {
              "query": "verz",
              "type": "bool_prefix",
              "operator": "and", 
              "fields": [
                "category_search",
                "category_search._2gram",
                "category_search._3gram"
              ]
            }
          }
        }

```

If there is a better approach for keyword fields for what I am looking for, please do let me know.

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 14, 2020, 11:33am UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/2 "2020-08-14T11:33:45Z")

</div>

I hope the question is clear. There is a query filter to filter the already chosen categories. There are in total 46150 unique categories at this time.

So an autocomplete for the not chosen items.

```auto
GET webshop-products-v1/_search
{
  "size": 0, 
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "category": "holiday"
          }
        },
        {
          "term": {
            "category": "vacation"
          }
        }
      ]
    }
  }, 
  "aggs": {
    "labels": {
      "terms": {
        "field": "category",
        "exclude": ["holiday", "vacation"], 
        "size": 10
      }
    }
  }
}

```

I am looking to alter that query to only show the terms based on the provided search term, I am currently looking at a regex filter but the first letter is Uppercase for Images so ._mag._ wont give a result for that option. Curious how others would get to that solution.

@spinscale maybe you have a bright idea about this?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 14, 2020, 12:31pm UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/3 "2020-08-14T12:31:33Z")

</div>

Hey Peter!

soooooo, given it's 30 degrees I might need another take on understanding 🙂

First, I assume the `category` field is an array? Second, your current problem is, that you are filtering for above categories, but then you are not interested in those, because there are other elements in the document, that you want to display?

```auto
{ "category" : ["holiday", "adventure"] }
{ "category" : ["vacation", "family"] }

```

You expected results would be `adventure` and `family` with your above query?

Does that make sense or did I misunderstand?

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 14, 2020, 2:58pm UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/4 "2020-08-14T14:58:33Z")

</div>

I created a PoC for it here:

> **[Screen Capture on 2020-08-14 at 14-25-34.gif](http://drops.3ws.nl/30fLDy)**
>
> Shared with Droplr

Now the terms shows the top 10 (aggregations size) but I want to add a searchbox to search in the remaining found terms with a query.

I tried with a regex, only the first letter is a capital or could contain Capitals in the keyword field. Any tips?

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 16, 2020, 10:02am UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/5 "2020-08-16T10:02:28Z")

</div>

I thought of doing this:

Making everything lowercase divided with \_\_ with the original and a lowercase variant value. Now I can use the script, the variant. But that piece is going to be a stored script since there are a lot of visitors.

```auto
    GET webshop-products-v1/_search
    {
      "size": 0, 
      "aggs": {
        "category": {
          "terms": {
            "script": {
              "source": "_value + '__' + _value.toLowerCase()"
            }, 
            "field": "category",
            "include": ".*gr.*", 
            "size": 10
          }
        }
      }
    }

```

---

<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:** [September 13, 2020, 10:02am UTC](https://discuss.elastic.co/t/autocomplete-for-terms-aggregations/244914/6 "2020-09-13T10:02:37Z")

</div>

Closed on September 13, 2020, 10:02am UTC

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