【问题标题】:Unable to replicate post_filter query in elasticsearch-dsl无法在 elasticsearch-dsl 中复制 post_filter 查询
【发布时间】:2021-06-20 18:05:59
【问题描述】:

我想在 DSL 中复制的查询如下:

GET /_search
{
   "query":{
      "bool":{
         "must":[
            {
               "term":{
                  "destination":"singapore"
               }
            },
            {
               "terms":{
                  "tag_ids":[
                     "tag_luxury"
                  ]
               }
            }
         ]
      }
   },
   "aggs":{
      "max_price":{
         "max":{
            "field":"price_range_from.SGD"
         }
      },
      "min_price":{
         "min":{
            "field":"price_range_from.SGD"
         }
      }
   },
   "post_filter":{
      "range":{
         "price_range_from.SGD":{
            "gte":0.0,
            "lte":100.0
         }
      }
   }
}

以上查询

  1. 匹配术语 - destinationtags_ids
  2. 汇总结果以从字段price_range_from.SGD 中查找最高价格
  3. 应用另一个 post_filter 以在价格限制内对结果集进行子集化

它在 Elastic/Kibana 控制台中运行良好。

我在 elasticsearch-dsl 中复制了上述查询,如下所示:

    es_query = []
    es_query.append(Q("term", destination="singapore"))
    es_query.append(Q("terms", tag_ids=["tag_luxury"]))
    final_query = Q("bool", must=es_query)
    
    es_conn = ElasticSearch.instance().get_client()
    dsl_client = DSLSearch(using=es_conn, index=index).get_dsl_client()
    dsl_client.query = final_query
    dsl_client.aggs.metric("min_price", "min", field="price_range_from.SGD")
    dsl_client.aggs.metric("max_price", "max", field="price_range_from.SGD")
    q = Q("range", **{"price_range_from.SGD":{"gte": 0.0, "lte": 100.0}})
    dsl_client.post_filter(q)
    
    print(dsl_client.to_dict())
    response = dsl_client.execute()
    print(response.to_dict().get("hits", {}))

虽然聚合是正确的,超出价格范围的产品也会被退回。没有返回错误,但似乎没有应用 post_filter 查询。

我深入研究了dsl_client 对象,以查看我的查询是否被正确捕获。我只看到queryaggs,但看不到对象中的post_filter 部分。使用dsl_client.to_dict() 转换为字典时的查询如下 -

{
   "query":{
      "bool":{
         "must":[
            {
               "term":{
                  "destination":"singapore"
               }
            },
            {
               "terms":{
                  "tag_ids":[
                     "tag_luxury"
                  ]
               }
            }
         ]
      }
   },
   "aggs":{
      "min_price":{
         "min":{
            "field":"price_range_from.SGD"
         }
      },
      "max_price":{
         "max":{
            "field":"price_range_from.SGD"
         }
      }
   }
}

请帮忙。谢谢!

【问题讨论】:

  • 您好,您找到解决方案了吗?

标签: python elasticsearch elasticsearch-aggregation elasticsearch-dsl


【解决方案1】:

你必须重新分配dsl_client 喜欢:

dsl_client = dsl_client.post_filter(q)

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多