弹性搜索中的过滤面 [英] filtering facets in elasticsearch

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

问题描述

我有一个如下所示的查询,

I have a query like below,

    query = {

        "query": {"query_string": {"query": "%s" % q}},
        "filter":{"ids":{"values":list(ids)}},
        "facets": {"destination": {
            "terms": {"field": "destination.en"}},
        "hotel_class":{
            "terms":{"field":"hotel_class"}},
        "hotel_type":{
            "terms":{"field": "hotel_type"}},
        }}

但是由于我的ids筛选器,我的构面没有被过滤。
我得到所有的方面,但我希望它们通过我的ids过滤器过滤。
你有什么想法吗?

But my facets are not filtered due to my ids filter. I get all the facets, but I want them filtered by my ids filter above. Do you have any ideas ?

推荐答案

尽管你做了什么工作,一个更清洁的解决方案是过滤查询。
http://www.elasticsearch.org/guide/参考/ query-dsl / filtered-query /

Although what you do works, a cleaner solution would be to have a filtered query. http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/

这允许你的原始查询+一些任意的过滤器(反过来可以是一个复杂的布尔/嵌套过滤器等)

Which allows for your original query + some arbitrary filter (which in turn can be a complex boolean/ nested filter, etc.)

  {
    query: {
        "filtered" : {
           "query": {"query_string": {"query": "%s" % q}},
           "filter":{"ids":{"values":list(ids)}},
        }
    },
    "facets": {
        "destination": {
            "terms": {"field": "destination.en"}
        },
        "hotel_class": {
            "terms": {"field": "hotel_class"}
        },
        "hotel_type": {
            "terms": {"field": "hotel_type"}
        }
    }
 }

理由如下:


  • 任何查询应用于ceting。

  • 任何过滤器都在应用之后。

所以如果您希望通过某些过滤器过滤您的构面,则必须在QUERY中包含所述过滤器。

So if you want your facets to be filtered by some filter, you have to include said filter in the QUERY.

这篇关于弹性搜索中的过滤面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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