带有多个过滤器的ElasticSearch [英] ElasticSearch with multiple filters

查看:71
本文介绍了带有多个过滤器的ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个查询,该查询将找到所有用户文档(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.

推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆