John Lippson
John Lippson

Reputation: 1319

Elasticsearch term query with spaces not working

I'm trying to execute the following query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Thing.Name": {
              "value": "(item) test",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

This is not yielding results and I have no idea why. I have parens and a space. What are my options here?

Upvotes: 0

Views: 1490

Answers (1)

Nishant
Nishant

Reputation: 7864

You want to match exact value for which you are using term query. As Amit mentioned in the comment that term query doesn't use analyzer and hence it will match the documents which contains exact same tokens you need to modify the mapping for Thing.Name as below:

{
  "Thing": {
    "properties": {
      "Name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

If the mapping is auto generated by elastic then it will have similar properties for name field as above. If it is already this then you need not make any modifications in the mapping. Update you query to use Thing.Name.keyword instead of Thing.Name since a field of type keyword doesn't analyze the value and generate a single token, which is the input value itself.

So the query will be :

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Thing.Name.keyword": {
              "value": "(item) test",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

Upvotes: 2

Related Questions