Search code examples
elasticsearchfoselasticabundlebooleanquery

ElasticSearch Bool Fitler doesn't seem to work with string terms


I am trying to execute a query with a Boolean filter, but it seems like it doesn't work when the type is a string.

Example:

When I send this payload to my _search endpoint:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        { "term": { "level" : 3 } },
                        { "term": { "id" : 4 } }
                    ]
                }
            }
        }
    }
}

The result is:

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "app",
        "_type": "reviewable",
        "_id": "4",
        "_score": 1,
        "_source": {
          "id": 4,
          "name": "Hololens Bootcamp Experience",
          "providers": [],
          "gender": [],
          "duration": "3",
          "category": "COURSE",
          "themes": [],
          "baseThemes": [],
          "budget": "1000+",
          "language": "EN",
          "level": "3",
          "region": "NL_BR",
          "subscribemethod": "2",
          "kind": "2"
        }
      }
    ]
  }
}

Then when I try to filter it with the language term like this:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        { "term": { "level" : 3 } },
                        { "term": { "id" : 4 } },
                        { "term": { "language" : "EN" } }
                    ]
                }
            }
        }
    }
}

My result has 0 hits:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Which seems weird to me, since you can see in my first result that the filter should match my data.

I have search around, and one post mentioned I might not have everything indexed. But as far as I can find out, I certainly have. Any ideas?


Solution

  • Your language field is most probably an analyzed string, hence EN was tokenized and indexed as en, so your query should be like this instead:

    {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "must": [
                            { "term": { "level" : 3 } },
                            { "term": { "id" : 4 } },
                            { "term": { "language" : "en" } }   <--- lowercase here
                        ]
                    }
                }
            }
        }
    }
    

    Another way to do this is to use a match query instead, but without the wrapping filtered query which is not useful in your case:

    {
        "query": {
             "bool": {
                 "must": [
                     { "term": { "level" : 3 } },
                     { "term": { "id" : 4 } },
                     { "match": { "language" : "EN" } }
                 ]
             }
        }
    }