python - 从Elasticseacrch-dsl-py中删除默认的 'match_all'查询

标签 python elasticsearch elasticsearch-dsl elasticsearch-py

如何从以下查询中删除“match_all”:

es = Elasticsearch()
s = Search(es)   
s = s.filter("term", status="Free")
s.to_dict()
{'query': {'filtered': {'filter': {'term': {'status': 'Free'}}, 'query': {'match_all': {}}}}}

最佳答案

match_all 查询在此处是可选的,它是过滤的查询的一部分:

{
    'query': {
        'filtered': {
            'filter': {
                'term': {
                    'status': 'Free'
                }
            },
            'query': {
                'match_all': {}
            }
        }
    }
}

根据规范,您可以将其删除,默认为 match_all :
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filtering_without_a_query

从python中的字典中删除键的一种方法是 pop 方法:
d = s.to_dict()
d['query']['filtered'].pop('query')

您不必在发送查询之前删除键,服务器将忽略它。

关于python - 从Elasticseacrch-dsl-py中删除默认的 'match_all'查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655145/

相关文章:

elasticsearch - Elasticsearch 性能相关的海量数据查询

elasticsearch - 过滤对象数组的不同字段

Python 链接抓取器

javascript - 在 Javascript 中解析 Python 日期时间字符串

python - 将 numpy 矩阵转换为 pyspark rdd

elasticsearch - Spring Data Elasticsearch 自动重连

testing - 检查Elasticsearch是否已经完成索引

python - 在递归中使用 return 的地方

node.js - 从 Lambda 创建 Elasticsearch 映射时为 "Authorization Exception"

elasticsearch - Elasticsearch日期查询。在某个月出生的人