簡體   English   中英

如何使用 elasticsearch python API 正確構建查詢?

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

我有一些看起來像這樣的代碼

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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM