【问题标题】:How to combine more than two filters in elasticsearch?如何在elasticsearch中组合两个以上的过滤器?
【发布时间】:2016-11-19 02:01:22
【问题描述】:

我想为 elasticsearch 1.7 编写这样的查询:

SELECT * FROM prods WHERE 
id=1 AND 
name='flower' AND 
(count_usd=1 OR prise_usd=5) AND 
(count_eur=1 OR prise_eur=5)

? 我读过关于嵌套布尔(https://www.elastic.co/guide/en/elasticsearch/guide/1.x/combining-filters.html)但不能将它应用到我的上下文:-(

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以在bool 查询中嵌套bool 查询来实现这种过滤:

    {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "must": [{
                            "term": {"name":"flower"}
                        }],
                        "should": [{
                            "bool": {
                                "should": [{
                                    "term": {
                                        "count_usd": 1
                                    }
                                }, {
                                    "term": {
                                        "prise_usd": 5
                                    }
                                }]
                            }
                        }, {
                            "bool": {
                                "should": [{
                                    "term": {
                                        "count_eur": 1
                                    }
                                }, {
                                    "term": {
                                        "prise_eur": 5
                                    }
                                }]
                            }
                        }]
                    }
                }
            }
        }
    }
    

    请注意,should 子句的行为类似于 OR:should 中的至少一个子句必须为真才能匹配。

    【讨论】:

    • 谢谢!太好了!
    • 如果你想实现 OR 之间的 AND,我认为第一个 should 应该是 must
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多