【问题标题】:How do I properly construct a query using the elasticsearch python API?如何使用 elasticsearch python API 正确构建查询?
【发布时间】:2016-08-25 02:53:03
【问题描述】:

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

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]];

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

我使用thisthis 来指导构建查询。

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

在为 python API 构建查询主体方面,任何人都可以看到我明显做错了什么吗?

【问题讨论】:

    标签: python elasticsearch indexing elasticsearch-py


    【解决方案1】:

    查询的语法对我来说看起来不正确。试试这个:

    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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2017-05-10
      • 2023-04-08
      相关资源
      最近更新 更多