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

Ramesh
  • 1,872
  • 2
  • 20
  • 33

1 Answers1

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.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
  • 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" } – Ramesh Jan 30 '20 at 05:54
  • 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. https://stackoverflow.com/a/48875105/8160318 if it's still unclear. – Joe - GMapsBook.com Jan 30 '20 at 14:46