Autocomplete for terms aggregations

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.

        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.

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.

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?

Hey Peter!

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

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?

{ "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?

I created a PoC for it here:

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?

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.

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

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