0
votes

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.

3

3 Answers

1
votes

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.

0
votes

You can use the Term query, which returns the documents that contain an exact term in a provided field.

For more reference visit the below, https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

0
votes

Basically, you are looking for "term query" which returns the "documents that contain an exact term in a provided field".

    {
        "query":{
            "term":{
                "value":"your input"
            }
        }
    }

Here is the reference of the same: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html