Reputation: 625
I have the following data in my index.
{
"id":1,
"car_name" : "ABC-101"
},
{
"id":2,
"car_name" : "DEF-102"
},
{
"id":3,
"car_name" : "ABC-103"
}
Mapping of my index is
{
"car": {
"mappings": {
"_doc": {
"properties": {
"car_name": {
"type": "text",
"fielddata": true
}
}
}
}
}
}
Whey i run the following query
localhost:9200/car/_doc/_search?pretty
With following request body
{
"size" : 0,
"aggs" : {
"genres" : {
"terms" : {
"field" : "car_name"
}
}
}
}
I get the following response
"buckets": [
{
"key": "ABC",
"doc_count": 2
},
{
"key": "DEF",
"doc_count": 1
},
{
"key": "101",
"doc_count": 1
},
{
"key": "102",
"doc_count": 1
},
{
"key": "103",
"doc_count": 1
}
]
Why it is not bring the actual keys which are ABC-101 and DEF-102 why ABC and 101 are treated as separated keys.
Upvotes: 2
Views: 17821
Reputation: 38552
By default, string fields are analyzed
in elasticsearch. It means that
"ABC-101" is indexed as 2 terms "ABC" and "101". Your
query is also analyzed, and it's also converted into 2 terms "ABC" and "101"
no matter which special characters are in between them.
That's why they are matching all the strings separated by -
like ABC, 101, DEF, 102 and so on.
e.g
{
"car": {
"car_name": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
If you want to search this field exactly as it is, you should re-index it as "index":"not_analyzed"
You can make it work with keyword
on car_name field to match exactly
{
"size" : 0,
"aggs" : {
"genres" : {
"terms" : {
"field" : "car_name.keyword"
}
}
}
}
Upvotes: 6