简体   繁体   English

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

[英]How do I properly construct a query using the elasticsearch python API?

I've got some code that looks like this我有一些看起来像这样的代码

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()

Examining the exception检查异常

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

And further down再往下

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

The stack trace was huge so I just grabbed snippets of it, but the main error appears to be that "must" is not supported, at least the way I have constructed my query.堆栈跟踪很大,所以我只是抓取了它的片段,但主要错误似乎是不支持“必须”,至少我构建查询的方式是这样。

I was using this and this for guidance on constructing the query.我使用thisthis作为构建查询的指导。

I can post a more complete stack trace, but I was hoping someone is able to see a very obvious error that I have made inside the "body" parameter inside the "search" method.我可以发布更完整的堆栈跟踪,但我希望有人能够看到我在“search”方法内的“body”参数中犯的一个非常明显的错误。

Can anyone see anything that I have clearly done wrong as far as constructing the query body for the python API?就为 python API 构建查询主体而言,任何人都可以看到我明显做错了什么吗?

The syntax of the query doesn't look correct to me.查询的语法在我看来不正确。 Try this:尝试这个:

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM