0

We have a field category in our index , We want to get number of records for each category using aggregation query.

GET /_search
{
    "aggs" : {
        "genres" : {
            "terms" : { "field" : "category" } 
        }
    }
}

We are getting results but it is giving results after analyzing category. Something like this

           {
                "key": "chil",
                "doc_count": 343503
            },
            {
                "key": "child",
                "doc_count": 343503
            },
            {
                "key": "childr",
                "doc_count": 343503
            },
            {
                "key": "childre",
                "doc_count": 343503
            },

But I want results without analyzing, I hope it is possible, Can someone help me with the query.

Expected

            {
                "key": "children",
                "doc_count": 343503
            },
            {
                "key": "Category1",
                "doc_count": 43503
            },
            {
                "key": "Category2",
                "doc_count": 60000
            }

We are having autocomplete analyzer for the field categoryqu in mapping

        "name": {
          "type": "string",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        }

Thanks

1
  • You can aggregate on keyword text datatype Commented Jan 29, 2020 at 14:41

1 Answer 1

1

Try aggregating on .keyword. But from what it looks like, you haven't specified the keyword field in your mapping.

So adjust your mapping like this:

{
  "category":{
    "type":"text",

    "fields":{
      "keyword":{
        "type":"keyword",
        "ignore_above":256
      },

      "name":{
        "analyzer":"autocomplete",
        "search_analyzer":"standard",
        "type":"text"
      }
    }
  }
}

and run the following

GET /_search
{
  "aggs":{
    "genres":{
      "terms":{
        "field":"category.keyword"
      }
    }
  }
}

Note: Performing search on category will use the ES-default text mapping. Using category.name will use the analyzer & search analyzer you specified. And searching/aggregating on category.keyword will perform the operation on the case-sensitive keyword -- exactly what you expect.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to add another field keyword under category , Do i need to pass this keyword when i ingest data ? Something like category { "keyword" : "CAT1", "name" : "Category 1" }
That's bad practice. If you want to have 2 different values for a field, rather create 2 separate fields. When you pass {"name": "Category 1"}, ES will index it once as a text and a second time as a keyword (provided you use the mapping from my answer). That's the point -- one value but 2 different indexing processes... If you want to ingest a different variant of the value, use a separate field for it and make its type equal to keyword. That way you'll be able to search on it without using the .keyword notation. stackoverflow.com/a/48875105/8160318 if it's still unclear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.