0

i have these documents in Elasticsearch:

{
    "id_object": "9",
    "size_id": "-1",
    "enabled": 1,
    "verified": 0,
    "countries": [
       "Germany"
    ]
},
{
    "id_object": "19",
    "size_id": "-1",
    "enabled": 1,
    "verified": 0,
    "countries": [
       "Germany",
       "France"
    ]
}

I use the following query to fetch all documents that have "Germany" inside:

GET abc/def/_search
{
  "query": {
    "filtered": {
      "filter": {
    "bool": {
      "must": {
        "term": {
          "countries": "Germany"
        }
      }
    }
  }
}

} }

but it returns no results! what am i doing wrong?

1 Answer 1

2

"Term" filters are not analyzed, meaning they will match exact terms, without lowercasing, without any other filtering or processing. So, your query will look into the inverted index by exact match "Germany", whereas if you used the standard analyzer when you put those two documents in ES, they would have been recorded as "germany" (lowercase) and as "france" lowercase.

So, in your case your filter will match like this:

{   "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "term": {
              "countries": "germany"
            }
          }
        }
      }
    }}}

On the other hand, "match" queries will be analyzed, so, if you're looking for "Germany", this instead will match:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {"match": {
              "countries": "Germany"
            }}
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Which one would be faster in processing if i have like 3 million documents ? query or filter?
Filter. If you query for the same terms often, then it would help because filters are cached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.