2016-03-23 57 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

相关问题