Here is an aggregation query that works as expected when I use dev tools in on Elastic Search :
search_query = {
"aggs": {
"SHAID": {
"terms": {
"field": "identiferid",
"order": {
"sort": "desc"
},
# "size": 100000
},
"aggs": {
"update": {
"date_histogram": {
"field": "endTime",
"calendar_interval": "1d"
},
"aggs": {
"update1": {
"sum": {
"script": {
"lang": "painless",
"source":"""
if (doc['distanceIndex.att'].size()!=0) {
return doc['distanceIndex.att'].value;
}
else {
if (doc['distanceIndex.att2'].size()!=0) {
return doc['distanceIndex.att2'].value;
}
return null;
}
"""
}
}
},
"update2": {
"sum": {
"script": {
"lang": "painless",
"source":"""
if (doc['distanceIndex.att3'].size()!=0) {
return doc['distanceIndex.att3'].value;
}
else {
if (doc['distanceIndex.at4'].size()!=0) {
return doc['distanceIndex.att4'].value;
}
return null;
}
"""
}
}
},
}
},
"sort": {
"sum": {
"field": "time2"
}
}
}
}
},
"size": 0,
"query": {
"bool": {
"filter": [
{
"match_all": {}
},
{
"range": {
"endTime": {
"gte": "2021-11-01T00:00:00Z",
"lt": "2021-11-03T00:00:00Z"
}
}
}
]
}
}
}
當我嘗試使用 Python ElasticSearch 客戶端 ( https://elasticsearch-py.readthedocs.io/en/v7.15.1/ )執行此聚合時,我收到例外:
exception search() got multiple values for keyword argument 'size'
如果我洗掉屬性:
"size": 0,
從查詢中,不會拋出例外,但聚合不會按照聚合的"size": 0,要求運行。
我應該使用不同的查詢格式來使用 Python ElasticSearch 客戶端執行聚合嗎?
更新 :
這是用于呼叫查詢的代碼:
import elasticsearch
from elasticsearch import Elasticsearch, helpers
es_client = Elasticsearch(
["https://test-elastic.com"],
scheme="https",
port=443,
http_auth=("test-user", "test-password"),
maxsize=400,
timeout=120,
max_retries=10,
retry_on_timeout=True
)
query_response = helpers.scan(client=es_client,
query=search_query,
index="test_index",
clear_scroll=False,
request_timeout=1500)
rows = []
try:
for row in query_response:
rows.append(row)
except Exception as e:
print('exception' , e)
使用es_client:
es_client.search(index="test_index", query=search_query)
導致錯誤:
/opt/oss/conda3/lib/python3.7/site-packages/elasticsearch/connection/base.py in _raise_error(self, status_code, raw_data)
336
337 raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
--> 338 status_code, error_message, additional_info
339 )
340
RequestError: RequestError(400, 'parsing_exception', 'unknown query [aggs]')
是aggs有效的搜索API?
uj5u.com熱心網友回復:
helpers.scan 是一個
scroll() api 之上的簡單抽象 - 一個簡單的迭代器,它產生所有命中,并通過下劃線滾動請求回傳。
它旨在遍歷大型結果集并帶有默認關鍵字引數 size=1000
要運行聚合,請直接使用該es_client.search() 方法,將您的查詢作為 傳遞body,并包含"size": 0在查詢中應該沒問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/352730.html
下一篇:Spring啟動Elasticsearch獲取ClassNotFoundException:org.elasticsearch.client.RequestOptions
