我能夠在彈性搜索中查詢索引。而且,現在我想將資料縮小到某些特定欄位。但是,我不斷收到錯誤。
這是我的查詢:
es = Elasticsearch(hosts="myhost", "port":0000)
search_body={
"bool":{
"filter":[
{"exists": {"field": "customer_name"}},
{"match_phrase": {"city": "chicago"}},
]
}
}
results = es.search(index="some_index", query=search_body)
到目前為止,我很容易得到結果。但是,由于回傳的欄位太多,我想在將其轉換為資料框之前只檢索特定欄位。我可以將其轉換為資料框,然后進行過濾,但這不是最佳的。
我嘗試添加_source和field方法:
search_body={
"bool":{
"filter":[
{"exists": {"field": "customer_name"}},
{"match_phrase": {"city": "chicago"}},
]
},
"_source":{"fields": {"includes":["customer_name", "city", "company", "company_address"] }}
}
和其他變體,例如,
"fields": {"includes":["customer_name", "city", "company", "company_address"] }
# or
"_source":{"includes":["customer_name", "city", "company", "company_address"] }
# and several others.
我不斷收到錯誤:
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')
我跟著:
- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html
- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html#source-filtering
- 幾個stackoverflow答案
- 甚至嘗試過命名查詢
我在這里想念什么?
uj5u.com熱心網友回復:
從您提供的 JSON 看來,您的_source欄位正在進入您的bool查詢。相反,您應該將其結構如下:
search_body = {
"query": {
"bool": [
{...}
]
},
"_source": {
"includes": [...]
}
}
(免責宣告:我是 Python Elasticsearch 客戶端的維護者并為 Elastic 作業)
uj5u.com熱心網友回復:
試試這個:
results = es.search(index="some_index", query=search_body, source_includes=[...])
代碼是最好的檔案(有時!)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/414872.html
標籤:
上一篇:ElasticSearch-按優先級組織搜索,帶/不帶空格
下一篇:同時執行兩個查詢(反應式)
