I'm working on a query based on name fields on Elasticsearch 2.4. The fields I'm interested in are:
- state
- city
- colony
If I send this query:
{"query":
{"bool" :
{"must" : [
{"match" : {"state" : {"query" : "michoacán de ocampo", "type" : "boolean"} } },
{"match" : {"colony" : {"query" : "zamora", "type" : "boolean"} } },
{"match" : {"city" : {"query" : "zamora", "type" : "boolean"} } }
],
"filter" : {"term" : {"state" : "michoacán"} }
}
} }
Results
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "71807",
"_index": "my_place",
"_score": 8.708784,
"_source": {
"@timestamp": "2019-11-13T15:34:33.373Z",
"@version": "1",
"city": "Zamora",
"city_id": 828,
"colony": "Balcones de Zamora",
"id": 71807,
"state": "Michoacán de Ocampo",
"state_id": 16,
"type": "place",
"zipcode": "59624",
"zone_id": null
},
"_type": "place"
},
{
"_id": "71762",
"_index": "my_place",
"_score": 8.634264,
"_source": {
"@timestamp": "2019-11-13T15:34:33.112Z",
"@version": "1",
"city": "Zamora",
"city_id": 828,
"colony": "Zamora de Hidalgo Centro",
"id": 71762,
"state": "Michoacán de Ocampo",
"state_id": 16,
"type": "place",
"zipcode": "59600",
"zone_id": null
},
"_type": "place"
}
],
"max_score": 8.708784,
"total": 2
},
"timed_out": false,
"took": 5
}
Which are OK
But if I sent the full name of the state in the filter, like this (note the full name "Michoacán de ocampo" in the filter)
{"query":
{"bool" :
{"must" : [
{"match" : {"state" : {"query" : "michoacán de ocampo", "type" : "boolean"} } },
{"match" : {"colony" : {"query" : "zamora", "type" : "boolean"} } },
{"match" : {"city" : {"query" : "zamora", "type" : "boolean"} } }
],
"filter" : {"term" : {"state" : "Michoacán de Ocampo"} }
}
} }
I got these results:
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [],
"max_score": null,
"total": 0
},
"timed_out": false,
"took": 6
}
I need to send the full name in the filter, how can I achieve this or reconfigure my index in order to have the same results?
Update : As OP mentioned in the comment that he is using 2.4, I am updating my solution to include the solution which works for it.
ES 2.4 solution
Index creation with required settings and mappings
Search query
An important thing to note here is creating a custom analyzer(keyword with lowercase filter), so that field on which we are creating filter stored as it is but with small letter, as that is what you are passing in your query. Now above query returns you both your document, this is the postman collection that has index creation, sample docs creation and query which return both docs returned.
ES 7.X solution
The issue is that you are defining your
state
field astext
field and then in your filter, you are using[term][1]
query which is not analyzed as explained in official ES doc.EDIT: - https://www.getpostman.com/collections/f4b9ed00d50e2f4bc7f4 is the postman collection link if you want to quickly test it.