我有一个名为“demoadmin”的 Eleaticsearch 索引,类型为“billing”。我已经使用 POST 成功执行了以下查询正文。
{"query": { "filtered":{ "query" : {"match_all": {}}, "filter": { "bool": { "must":[{ "term": { "Month": "01" }}, { "term": { "Year": "2018" }}] } } } }, "size": 0, "aggregations": { "Total Cost": { "sum": { "field": "UnBlendedCost" } } } }
此查询返回如下输出
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 503206,
"max_score": 0,
"hits": [ ]
},
"aggregations": {
"Total Cost": {
"value": 5849.867677253019
}
}
}
查询和输出在 Elasticsearch-head 中如下所示
当我尝试使用 elasticsearch python API 将此代码转换为 python 时,代码如下所示,它返回相同的输出。
ADMIN_INDEX_NAME ='demoadmin'
es_client = get_es_client()
def get_aggregated_total_cost_for_current_month(month,year):
'''Get Aggregated total cost for the current month'''
body = '{"query": { "filtered":{ "query" : {"match_all": {}}, "filter": { "bool": { "must":[{ "term": { "Month": "' + month + '" }}, { "term": { "Year": "' + year + '" }}] } } } }, "size": 0, "aggregations": { "Total Cost": { "sum": { "field": "UnBlendedCost" } } } }'
total_cost_result = es_client.search(index=ADMIN_INDEX_NAME, doc_type="billing", body=body)
raw_total_cost = total_cost_result['aggregations']['Total Cost']['value']
total_cost = float("%.2f" % raw_total_cost)
print(str(total_cost))
return total_cost
我正在尝试在 elasticsearch-dsl 中转换代码并卡住了。我已经完成了应用过滤器,但之后对要做什么感到困惑。到目前为止,我已经在 elasticsearch-dsl 中实现了代码,如下所示
def get_aggregated_total_cost_for_current_month_new(month,year):
'''Get Aggregated total cost for the current month'''
s = Search(using=es_client, index=ADMIN_INDEX_NAME, doc_type="billing") \
.filter("term", Month=month).filter("term", Year=year)
response = s.execute()
不知道如何从这里开始。一些机构可以在聚合部分帮助我吗?