2016-03-23 56 views
4

下面給出的ElasticSearch文件:如何按ElasticSearch中的字段值進行篩選?

{ 
    "_id": "3330481", 
    "_type": "user", 
    "_source": { 
     "id": "3330481", 
     "following": ["1", "2", "3", ... , "15000"] 
    } 
} 

對於給定的用戶列表[「50」,「51」,「52」]我想用ID 3330481.檢查哪些是其次是用戶因爲她是跟隨15000用戶,我不想得到整個列表,然後在本地檢查。

有沒有什麼辦法只檢索相關用戶?

回答

2

我不確定您是否可以從文檔中獲取子數組。

但是,實現此目的的一種方法是使用嵌套字段。您可以將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 
      } 
     } 
    } 
} 
+0

我用建議的inner_hits來解決我的問題。但是,我不確定'size = 0'。除非我刪除它,否則不會返回任何結果。 – fkoksal

0

如果你只是想檢索所有用戶的["50", "51", "52"]而不影響分數,你可以使用minimum_should_match與布爾查詢。示例:

{ 
    "query" : { 
    "filtered" : { 
     "filter" : { 
     "bool": { 
      "must": { 
      "terms": { 
       "following": ["50", "51", "52"], 
       "minimum_should_match": 3 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

我對其他用戶沒有興趣。我只想檢查給定的用戶。 – fkoksal

相關問題