简体   繁体   中英

Python Elasticsearch aggregated query

I am struggling to get an aggregated response based on a query... for example, in Kibana visualization, after creating a table with the aggregations and query I need, I can view the request by going inside the visualization and Inspect -> View:Requests -> Request. This gives me the entire request, structured like this:

{
 "aggs":{...},
  "size": 0,
  "fields":[...],
  "script_fields": {},
  "stored_fields": [
    "*"
  ],
  "runtime_mappings": {},
  "_source": {
    "excludes": []
  },
  "query": {
    "bool":{...}
  }
}

How can i obtain the same dataset that is displayed in Kibana Visualization with the API? I tried translating the API Request fields to the es.search() function like this:

result = es.search(
index=index,
fields=[ ...],
script_fields={},
stored_fields=[
    "*"
],
runtime_mappings={},
_source={
    "excludes": []
},
query={...},
aggs={...},

size = 0,
scroll = '5m'
)

But the data is not aggregated correctly, like in the Kibana Visualization. Another strange behaviour is that if i completely remove the aggs = {...} from es.search(), it gives me the same dataset.

Note: I am using elasticsearch module 7.16.1

Solved. The query is working fine, the aggregated data is returned in the "aggregations" section of the response, which is separate from the "hits" section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM