弹性搜索中的过滤方面

tun*_*nak 2 facets elasticsearch

我有一个如下查询,

    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"}},
        }}
Run Code Online (Sandbox Code Playgroud)

但由于我的ID过滤器,我的方面没有被过滤掉.我得到了所有方面,但我希望它们通过上面的id过滤器进行过滤.你有什么想法 ?

Gee*_*Jan 9

虽然你的工作是什么,但更清晰的解决方案是使用过滤查询. 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"}
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

理由如下:

  • 在进行分面之前应用任何查询.
  • 在切面后应用任何滤镜.

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