python - 如何使用 elasticsearch python API 正确构造查询?

标签 python elasticsearch indexing elasticsearch-py

我有一些看起来像这样的代码

from elasticsearch import Elasticsearch

client = Elasticsearch(hosts = [myhost])
try:
    results = es_client.search(
        body = {
            'query' : {
                'bool' : {
                    'must' : {
                        'term' : {
                            'foo' : 'bar',
                            'hello' : 'world'
                        }
                    }
                }
            }
        },
        index = 'index_A,index_B',
        size = 10,
        from_ = 0
    )
except Exception as e:
    ## my code stops here, as there is an exception
    import pdb
    pdb.set_trace()

检查异常

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;

再往下

Parse Failure [Failed to parse source [{"query": {"bool": {"must": {"term": {"foo": "bar", "hello": "world"}}}}}]]]; nested: QueryParsingException[[index_A] [bool] query does not support [must]];

堆栈跟踪很大,所以我只抓取了它的片段,但主要错误似乎是不支持“必须”,至少我构建查询的方式是这样。

我正在使用 thisthis构建查询的指导。

我可以发布更完整的堆栈跟踪,但我希望有人能够看到我在“search”方法的“body”参数中犯的一个非常明显的错误。

就构建 python API 的查询主体而言,任何人都可以看出我明显做错了什么吗?

最佳答案

我觉得查询的语法不正确。试试这个:

results = es_client.search(
    body = {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "foo": {
                  "value": "bar"
                }
              }
            },
            {
              "term": {
                "hello": {
                  "value": "world"
                }
              }
            }
          ]
        }
      }
    },
    index = 'index_A,index_B',
    size = 10,
    from_ = 0
)

关于python - 如何使用 elasticsearch python API 正确构造查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946897/

相关文章:

python - 使用 Python 打印属于文档中最常用词的句子

elasticsearch - 在Elasticsearch中返回不同的值

elasticsearch - 如何通过AWS VPC使用Elastic Cloud的Logstash从MySQL(AWS RDS)导入数据?

Elasticsearch 未分析且小写

mysql - 为什么仅使用索引不能解决这个结果?

Python C 扩展 - 接收字典作为参数

python - 通过 Python 调用的 C 函数确定错误的数组大小

python - 将所有构造函数参数作为实例属性添加到 PyCharm 中的类

matlab - 从具有特定索引的向量创建矩阵,无需循环

bash - 在 csv 文件中查找列的索引号,但命令不显示结果?