Elasticsearch - 来自工作 curl 示例的 python 语法



使用 curl,以下请求有效:

curl -XGET 'localhost:9200/dumperino/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "id_1":  "4000000029898186" }},
{ "match": { "id_2":  "4000000029898188"   }}
]
}
}
}
'

我现在正在尝试通过python使用elasticsearch。

from elasticsearch import helpers
es = elasticsearch.Elasticsearch()
qu={
"query": {
"bool": {
"should": [
{ "match": { "id_1":  "4000000029898186" }},
{ "match": { "id_2": "4000000029898188"   }}
]
}
}
}
result = es.search(index= "dumperino",q=qu)

错误:elasticsearch.exceptions.RequestError: TransportError(400, u'search_phase_execution_exception', u"Failed to parse query [{'query': {'bool': {'should': [{'match': {'id_1': '4000000029898186'}}, {'match': {'id_2': '4000000029898188'}}]}}}]")

我以前曾成功使用过这种格式,尽管之前使用过更简单的字符串查询。

我需要在 elasticsearch 的 JSON 查询中进行哪些更改才能在 Python 中正确解析它?

请尝试以下操作:result = es.search(index= "dumperino",body=qu)