【问题标题】:How do I apply 2 filters to my ElasticSearch query object?如何将 2 个过滤器应用于我的 ElasticSearch 查询对象?
【发布时间】:2013-03-24 04:53:06
【问题描述】:

我想查询我的索引,但应用了 2 个过滤器。 一是价格,二是位置。

var qobject = {
        query:{
            custom_score:{
                query:{
                    filtered:{
                        query:{
                            multi_match:{
                                query: q,
                                fields: ['title','description'],
                            }
                        },
                        filter:{
                            range:{
                                price: { from: 0, to: max_price }
                            },
                            geo_distance:{
                                'distance': distance + 'mi',
                                'location':{
                                    lat: lat,
                                    lon: lon
                                }
                            }
                        }
                    }
                },
                script: '_score + _source["price"] * 10'
            }
        }
    }

    elasticSearchClient.search('products', 'products', qobject)

如您所见,此查询对象导致错误。

但是,如果我删除 range 或 geo_distance,一切都很好!但我想要两个过滤器...

【问题讨论】:

    标签: search lucene indexing elasticsearch


    【解决方案1】:

    使用“和”过滤器。

    http://www.elasticsearch.org/guide/reference/query-dsl/and-filter.html

    未经测试:

    var qobject = {
            query:{
                custom_score:{
                    query:{
                        filtered:{
                            query:{
                                multi_match:{
                                    query: q,
                                    fields: ['title','description'],
                                }
                            },
                            filter:{
                                "and" : [
                                    range:{
                                        price: { from: 0, to: max_price }
                                    },
                                    geo_distance:{
                                        'distance': distance + 'mi',
                                        'location':{
                                            lat: lat,
                                            lon: lon
                                        }
                                    }
                                ]
                            }
                        }
                    },
                    script: '_score + _source["price"] * 10'
                }
            }
        }
    

    解释:“过滤查询”的“过滤器”部分实际上可以是您喜欢的任何类型的过滤器,包括“和”。一旦明确了这个概念,构建复杂的查询/过滤器就很简单了。

    【讨论】:

    • 使用“and”是否与将过滤器包裹在另一个过滤器中一样?
    • 我不确定你的意思。像“and”这样的一些过滤器是专门为嵌套其他过滤器而制作的。我想你可以称之为包装。
    • 当您查看文档时,您可以查找和,或者,不是过滤器.. 这些都“包装”其他过滤器(您可以任意嵌套深度:和内部等),允许您任意构建复杂的布尔查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2017-10-27
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多