带有多个过滤器的ElasticSearch

时间:2014-04-30 14:24:03

标签: elasticsearch

我正在尝试构建一个查找所有用户文档(docType = user)的查询,然后根据许多过滤器过滤它们。例如位置,性别,年龄等。根据我正在构建的搜索功能的用户输入添加/删除过滤器。

以下不会返回结果:

{
    "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"
                             }
                         }
                     ]
                 }
             }
        }
    }
}

后者虽然返回结果,但从长远来看不会起作用,因为我可能想要为年龄,性别等添加额外的过滤器,我似乎无法添加多个字段。如果我删除了位置过滤器,则第一个查询有效。

非常感谢任何指导帮助。

3 个答案:

答案 0 :(得分:8)

The bool filter允许您将多个MUSTSHOULDSHOULD_NOT请求链接在一起。允许您将其构建为一个查询。

答案 1 :(得分:3)

我认为你想要的是一个bool查询(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

这样你可以将多个必须链在一起以获得所需的结果。

答案 2 :(得分:2)

这是编写多个过滤器查询的方法

   text                                    is_relevant
1    good,i am interested..please mail me.         yes
2            call me...good to go with you         yes
3                     not interested...bye          no
4        i am not interested don't call me          no
5      price is too high so not interested          no
6  i have some requirement..please mail me       maybe
相关问题