[英]How to apply a OR filter on same field with different values in elasticsearch query
[英]How to filter by field values in ElasticSearch?
鑒於以下ElasticSearch文檔:
{
"_id": "3330481",
"_type": "user",
"_source": {
"id": "3330481",
"following": ["1", "2", "3", ... , "15000"]
}
}
對於給定的用戶列表[“50”,“51”,“52”]我想檢查ID為3330481的用戶遵循哪些用戶。由於她關注15000個用戶,我不想獲得整個列表然后在本地檢查。
有沒有辦法只檢索相關用戶?
我不確定你是否可以從doc獲取一個子數組。
但是,實現此目的的一種方法是使用嵌套字段。 您可以將following
字段的架構更改為nested
,如下所示。
{
"id": "3330481",
"following": [
{"userId":"1"}, {"userId":"2"},{"userId": "3"}, ... ]
}
然后,您可以使用inner_hits檢索數組中的匹配元素,如下所示。
{
"query" : {
"nested" : {
"path" : "following",
"query" : {
"terms" : {"following.userId" : ["50","51","53"]}
},
"inner_hits" : {
"size":0 // fetches all
}
}
}
}
如果您只是想要檢索所有用戶的["50", "51", "52"]
沒有影響分數,則可以將minimum_should_match與bool查詢一起使用。 例:
{
"query" : {
"filtered" : {
"filter" : {
"bool": {
"must": {
"terms": {
"following": ["50", "51", "52"],
"minimum_should_match": 3
}
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.