Elastic search: How to finding exact value
I am using the ElasticSearch version 7.4 with Java API Client. I am facing an issue raleted to finding exact value:
My mapping:
elasticsearch.address.mapping={"properties":{"address":{"type":"text","analyzer": "folding"},"id":{"type":"long"},"ward_name":{"type":"text","analyzer":"folding"}, "address_number":{"type":"text", "index" : "not_analyzed"}}}
My java code:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.termQuery("address_number", "6"));
I have got some results:
- 6/12
- 6/10
- 6
- 16/6
I only want to get the address number 6, and will skip another results
Please help me on this. Thanks all
Solution 1:
According to the documentation, ES actually advises you to not use (termQuery) against TextFields, which address_number is:
Term query edit
Returns documents that contain an exact term in a provided field.
You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.
Avoid using the term query for text fields
By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.
To search text field values, use the match query instead.
https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-term-query.html#query-dsl-term-query
You should use match instead:
Example:
QueryBuilders.matchQuery("address_number", "6")