Azima
Azima

Reputation: 4141

term query returns 0 results elasticsearch

I have a following document:

        {
            "_index": "testrest",
            "_type": "testrest",
            "_id": "sadfasdfw1",
            "_score": 1,
            "_source": {,
                "s_item_name": "Create",
                "request_id": "35",
                "Result": "Match",
            }
        },

I am trying to get records with term value Match in Result field and request_id of 35.

Here's my query:

{
  "query": { 
    "bool": { 
      "must": [ 
        { "term":  { "Result": "Match" }}
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

But I am getting 0 results.

Replacing term with match gives me results though.

Upvotes: 3

Views: 1257

Answers (1)

Val
Val

Reputation: 217274

This is because Result is a text field, and thus, it is analyzed and the value is indexed as match instead of Match.

In general, you should not use a term query, but prefer a match query instead. In your case, that would solve the issue.

If you absolutely want to use a term query and have a field called Result.keyword, then you can also do that.

So to recap:

{
  "query": { 
    "bool": { 
      "must": [ 
        { "match":  { "Result": "Match" }}         <-- use match query
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

or

{
  "query": { 
    "bool": { 
      "must": [ 
        { "term":  { "Result.keyword": "Match" }}  <-- use term query on keyword field
      ],
      "filter": {
        "term": {
            "request_id": "35"
        }
      }
    }
  }
}

Upvotes: 5

Related Questions