我正在尝试建立一个查询,该查询将找到所有用户文档(docType = user),然后根据许多过滤器对其进行过滤.例如位置,性别,年龄等.这些过滤器是根据我正在构建的搜索功能上的用户输入来添加/删除的.
I am trying to build a query that will find all user documents (docType = user) and then filter them based on many filters. Such as location, gender, age, etc. The filters are added / removed based on user input on the search function I'm building.
以下未返回任何结果:
{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "and": { "filters": [ { "term": { "doc.docType": "user" } }, { "term": { "doc.data.profile.location" : "CA" } } ] } } } } }以下返回结果:
{ "query": { "filtered": { "query": { "field": { "doc.data.profile.location" : "CA" } }, "filter": { "and": { "filters": [ { "term": { "doc.docType": "user" } } ] } } } } }后者虽然返回结果,但从长远来看是行不通的,因为我可能想添加一个针对年龄,性别等的额外过滤器,而且我似乎无法添加多个字段.如果我删除位置过滤器,则第一个查询有效.
The latter, although returning results, isn't going to work in the long run as I may want to include an extra filter for age, gender, etc and I can't seem to add multiple fields. The first query works if I remove the filter for location.
推荐答案布尔过滤器允许您链接多个 MUST , SHOULD 和 SHOULD_NOT请求一起.允许您将其构造为一个查询.
The bool filter allows you to chain multiple MUST, SHOULD and SHOULD_NOT requests together. Allowing for you to construct this into one query.
更多推荐
带有多个过滤器的ElasticSearch
发布评论