Issue
The mapping contains nested field.
I'm wondering if it's possible to do an exact match on "value" without changing its type to "keyword".
"mappings": {
"properties": {
"tag": {
"type": "nested",
"properties": {
"value": {
"type": "text"
},
"key": {
"type": "keyword"
}
}
}
}
}
Below is the code I tried to do an exact match on "value" field.
BoolQueryBuilder boolQ = boolQuery();
boolQ.must(matchQuery("tag.key", "key"));
boolQ.must(matchQuery("tag.value", "value").fuzziness(Fuzziness.ZERO));
entireQuery.must(nestedQuery("tag", boolQ, ScoreMode.None));
The above returned a result matched with tokenized words of "value" as well.
I would really appreciate if any references related to the question is present.
Solution
Since tag.value
is of type text
, its content has been analyzed and the resulting tokens (i.e. not exact value) have been indexed in Elasticsearch.
Even though the term
query doesn't analyze the search token, it doesn't help here because the indexed tokens have already been analyzed.
There are now two options:
A. If tag.value
only contains a single token (e.g. "Dog"
), you can still match them exactly by using either term
or match
but by lowercasing the value, as in:
{
"nested": {
"path": "tag",
"query":{
"term":{
"tag.value":"dog"
}
}
}
}
B. If tag.value
contains multiple tokens (e.g. "the big dog"
), it is therefore not possible to search for their exact value anymore.
Answered By - Val
Answer Checked By - Cary Denson (JavaFixing Admin)