如何在elasticsearch中组合多个bool查询

时间:2016-04-03 18:38:03

标签: elasticsearch

我想创建以下查询的等价物 -

(city = 'New York' AND state = 'NY') AND ((businessName='Java' and businessName='Shop') OR (category='Java' and category = 'Shop'))

我尝试了使用must和bool查询的不同组合,但似乎没有任何工作。可以这样做吗?

1 个答案:

答案 0 :(得分:16)

这样的事情怎么样:

{
    "query": {
        "match_all": {}
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "city": "New york"
                    }
                },
                {
                    "term": {
                        "state": "NY"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "businessName": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "businessName": "Shop"
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "category": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "category": "Shop"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}