[英]How to filter by field values in ElasticSearch?
Given the ElasticSearch document below: 鉴于以下ElasticSearch文档:
{
"_id": "3330481",
"_type": "user",
"_source": {
"id": "3330481",
"following": ["1", "2", "3", ... , "15000"]
}
}
For a given user list ["50", "51", "52"] I want to check which ones are followed by the user with id 3330481. Since she is following 15000 users, I don't want to get the whole list and then check it locally. 对于给定的用户列表[“50”,“51”,“52”]我想检查ID为3330481的用户遵循哪些用户。由于她关注15000个用户,我不想获得整个列表然后在本地检查。
Is there any way to retrieve only the relevant users? 有没有办法只检索相关用户?
I am not sure whether you can fetch a sub-array from a doc. 我不确定你是否可以从doc获取一个子数组。
However, one way of achieving this is by using nested fields. 但是,实现此目的的一种方法是使用嵌套字段。 You can change the schema of
following
field to be nested
as below. 您可以将
following
字段的架构更改为nested
,如下所示。
{
"id": "3330481",
"following": [
{"userId":"1"}, {"userId":"2"},{"userId": "3"}, ... ]
}
Then you can use inner_hits to retrieve your matching elements in the array as below. 然后,您可以使用inner_hits检索数组中的匹配元素,如下所示。
{
"query" : {
"nested" : {
"path" : "following",
"query" : {
"terms" : {"following.userId" : ["50","51","53"]}
},
"inner_hits" : {
"size":0 // fetches all
}
}
}
}
if you just want to retrieve all user's flollowing with ["50", "51", "52"]
without affect score, you can use minimum_should_match with bool query. 如果您只是想要检索所有用户的
["50", "51", "52"]
没有影响分数,则可以将minimum_should_match与bool查询一起使用。 Example: 例:
{
"query" : {
"filtered" : {
"filter" : {
"bool": {
"must": {
"terms": {
"following": ["50", "51", "52"],
"minimum_should_match": 3
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.