Abhay Bhargav
Abhay Bhargav

Reputation: 399

Get Values of Unique Sentences - ElasticSearch

I have a field in my indexed document that is a sentence. I intend to find the values of unique sentences across all documents in the index. This field is a "string" field and is analyzed. I have tried the cardinality aggregations but it gives me a count of unique sentences, but not the actual unique values. How do I solve this problem?

This is my search query

{
   "fields":[
      "incident.name"
   ],
   "aggs":{
      "unique_vuls":{
         "cardinality":{
            "field":"incident.name"
         }
      }
   }
}

Upvotes: 0

Views: 136

Answers (1)

Abhay Bhargav
Abhay Bhargav

Reputation: 399

Update & Answer: Per @AndreiStefan's advice, I re-mapped the field as a multi-field and re-indexed the data. Subsequently, I queried using the incident.name.raw field and was able to obtain all the unique sentences in the index.

Here's the snippet of the mapping:

{
   "name":{                          #incident.name field
      "type":"string",
      "index":"analyzed",
      "fields":{
         "raw":{
            "type":"string",
            "index":"not_analyzed"
         }
      }
   }
}

Here's the snippet of the search query with terms aggregation:

{
   "aggs":{
      "unique_incidents":{
         "terms":{
            "field":"incident.name.raw"
         }
      }
   }
}

Upvotes: 1

Related Questions