在elasticsearch中过滤facet

2024-04-28

我有一个如下查询,

    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 过滤器进行过滤。 你有什么想法 ?


尽管您所做的可行,但更干净的解决方案是进行过滤查询。http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/ http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/

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

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

理由如下:

  • 任何查询都在分面之前应用。
  • 任何过滤器都在分面之后应用。

因此,如果您希望通过某些过滤器过滤您的方面,则必须在查询中包含所述过滤器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在elasticsearch中过滤facet 的相关文章

随机推荐