在elasticsearch中匹配数组属性(Matching array property in elasticsearch)

我有以下文件,在一个名为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"}] } ]

和一个选择的id列表,例如var choosenPrefs=[2, 3] ,它们匹配首选项的id属性。

现在,我想要让所有至少有一个ID位于其偏好范围内的用户。

我尝试了不同的查询,但没有得到它...

目前看起来像这样:

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

我也使用了距离过滤器(它没有偏好过滤/匹配)。

查询不会抛出任何错误,但我没有得到任何结果。

我希望有人有建议?!

PS:我正在使用最新的ES版本

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

最满意答案

你有一个错字( preferences而不是preferences ):

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

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

更多推荐