Indivon Indivon - 1 month ago 7
JSON Question

Matching array property in elasticsearch

I have following documents, in an index called

users
:

[
{
"name": "foo",
"preferences": [{"id":1, "name":"a"}, {"id": 2, "name":"b"}]
},
{
"name": "bar",
"preferences": [{"id":2, "name":"a"}, {"id": 3, "name":"c"}, {"id": 4, "name":"d"}]
}
]


and a list of choosen ids, e.g.
var choosenPrefs=[2, 3]
, which match the id property of the preferences.

Now, I want to get all users that have at least one of the ids within their preferences.

I tried different queries, but did not get it...

currently it looks like this:

"query": {
"bool": {
"must": [
{"prefix": {"name": ""}},
{
"terms": {
"perferences.id": choosenPrefs
}
}
],
"filter": {
"geo_distance": {
"distance": "10km",
"location": position
}
}
}
}


I'm using also a distance-filter (which works without the preference-filtering/matching).

The query does not throw any error, but i don't get any results.

I hope someone has an advice?!

PS: I'm using the latest ES version

Answer

You have a typo (perferences instead of preferences):

   "query": {
    "bool": {
        "must": [
          {"prefix": {"name": ""}},
          {
            "terms": {
                "preferences.id": [2,3]
            }
          }
        ],
        "filter": {
            "geo_distance": {
                "distance": "10km",
                "location": position
            }
         }
     }
 }
Comments