TeAmEr
TeAmEr

Reputation: 4773

can't search through arrays in elasticsearch

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?

Upvotes: 0

Views: 46

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

"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"
            }}
          ]
        }
      }
    }
  }
}

Upvotes: 2

Related Questions