user2980769
user2980769

Reputation: 141

Terms query not returning results for list of strings

I have this Elastic query which fails to return the desired results for terms.letter_score. I'm certain there is available matches in the index. This query (excluding letter_score) returns the expected filtered results but nothing with letter_score. The only difference is (as far as I can tell), is that the cat_id values is a list of integers vs strings. Any ideas of what could be the issue here? I'm basically trying to get it to match ANY value from the letter_score list.

Thanks

{
   "size": 10,
   "query": {
      "bool": {
         "filter": [
            {
               "terms": {
                  "cat_id": [
                     1, 
                     2,
                     4
                  ]
               }
            },
            {
               "terms": {
                  "letter_score": [
                     "A",
                     "B",
                     "E"
                  ]
               }
            }
         ]
      }
   }
}

Upvotes: 0

Views: 457

Answers (1)

Val
Val

Reputation: 217554

It sounds like your letter_score field is of type text, and hence, has been analyzed, so the tokens A, B and E have been stored as a, b and e so the terms query won't match them.

Also if that's the case, the probability is high that the token a has been ignored at indexing time because it is a stop word and the standard analyzer (default) ignores them (if you're using ES 5+).

A first approach is to use a match query instead of terms, like this:

{
   "match": {
      "letter_score": "A B E"
   }
}

If that still doesn't work, I suggest that you change the mapping of your letter_score field to keyword (requires reindexing your data) and then your query will work as it is now

Upvotes: 2

Related Questions