elasticsearch


ElasticSearch multiple terms aggregation order


I have a document structure which describes a container, some of its fields are:
containerId -> Unique Id,String
containerManufacturer -> String
containerValue -> Double
estContainerWeight ->Double
actualContainerWeight -> Double
I want to run a search aggregation which has two levels of terms aggregations on the two weight fields, but in descending order of the weight fields, like below:
{
"size": 0,
"aggs": {
"by_manufacturer": {
"terms": {
"field": "containerManufacturer",
"size": 10,
"order": {"estContainerWeight": "desc"} //Cannot do this
},
"aggs": {
"by_est_weight": {
"terms": {
"field": "estContainerWeight",
"size": 10,
"order": { "actualContainerWeight": "desc"} //Cannot do this
},
"aggs": {
"by_actual_weight": {
"terms": {
"field": "actualContainerWeight",
"size": 10
},
"aggs" : {
"container_value_sum" : {"sum" : {"field" : "containerValue"}}
}
}
}
}
}
}
}
}
Sample documents:
{"containerId":1,"containerManufacturer":"A","containerValue":12,"estContainerWeight":5.0,"actualContainerWeight":5.1}
{"containerId":2,"containerManufacturer":"A","containerValue":24,"estContainerWeight":5.0,"actualContainerWeight":5.2}
{"containerId":3,"containerManufacturer":"A","containerValue":23,"estContainerWeight":5.0,"actualContainerWeight":5.2}
{"containerId":4,"containerManufacturer":"A","containerValue":32,"estContainerWeight":6.0,"actualContainerWeight":6.2}
{"containerId":5,"containerManufacturer":"A","containerValue":26,"estContainerWeight":6.0,"actualContainerWeight":6.3}
{"containerId":6,"containerManufacturer":"A","containerValue":23,"estContainerWeight":6.0,"actualContainerWeight":6.2}
Expected Output(not complete):
{
"by_manufacturer": {
"buckets": [
{
"key": "A",
"by_est_weight": {
"buckets": [
{
"key" : 5.0,
"by_actual_weight" : {
"buckets" : [
{
"key" : 5.2,
"container_value_sum" : {
"value" : 1234 //Not actual sum
}
},
{
"key" : 5.1,
"container_value_sum" : {
"value" : 1234 //Not actual sum
}
}
]
}
},
{
"key" : 6.0,
"by_actual_weight" : {
"buckets" : [
{
"key" : 6.2,
"container_value_sum" : {
"value" : 1234 //Not actual sum
}
},
{
"key" : 6.3,
"container_value_sum" : {
"value" : 1234 //Not actual sum
}
}
]
}
}
]
}
}
]
}
}
However, I cannot order by the nested aggregations. (Error: Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation...)
For example, for the above sample output, I have no control on the buckets generated if I introduce a size on the terms aggregations (which I will have to do if my data is large),so I would like to only get the top N weights for each terms aggregation.
Is there a way to do this ?
If I understand your problem correctly, you would like to sort the manufacturer terms in decreasing order of the estimated weights of their containers and then each bucket of "estimated weight" in decreasing order of their actual weight.
{
"size": 0,
"aggs": {
"by_manufacturer": {
"terms": {
"field": "containerManufacturer",
"size": 10
},
"by_est_weight": {
"terms": {
"field": "estContainerWeight",
"size": 10,
"order": {
"_term": "desc" <--- change to this
}
},
"by_actual_weight": {
"terms": {
"field": "actualContainerWeight",
"size": 10,
"order" : {"_term" : "desc"} <----- Change to this
},
"aggs": {
"container_value_sum": {
"sum": {
"field": "containerValue"
}
}
}
}
}
}
}
}
}
}

Related Links

elasticsearch first setup - create index
Using ElasticSearch as alternative data store with applications updating both the DB and ES(with the help of Kafka). Is this a good idea?
Where Elasticsearch store the data on Mac
Elastica - multiple bool queries - subqueries
How do i add boosting to the elasticsearch mapping in ONGR?
Is it possible to use doc_count as a term in elasticsearch?
Elastic search aggregation with sort
Elasticsearhc- how to do multi search request on Sense or Head plugin
How do I configure/initialize a custom Elasticsearch Tokenizer
Combining log entries with logstash
How to import CSV data using Logstash for field type completion in Elasticsearch suggester
How can I interpret the result of percentiles aggregation type
elastic search select fields to return in aggregation
How to Focus Search On One Part of a Document
How to calculate cooccurrences on a set of texts with Elasticsearch
elasticsearch edge_ngrams analyzer is not found

Categories

HOME
opengl
rust
memory
whmcs
vsts-build
celery
bing-search
yql
dronekit-python
nsis
ipv6
openfoam
kurento
timber
tfs2010
jpa-criteria
mailing-list
stack-overflow
log4j2
reselect
robolectric
renderscript
error-logging
sqlplus
fido-u2f
multi-upload
sybase-ase
tortoisemerge
gravity-forms-plugin
adapter
dnsmasq
fusion
windows2012
argv
opnet
cloudera-manager
bean-validation
orchardcms-1.8
nanogallery
android-alarms
widestring
pyttsx
expandablelistview
supertest
maven-jaxb2-plugin
subforms
skip-lists
deedle
lvalue
instafeedjs
openerp-6
openquery
dcount
xcode8-beta4
oci
android-4.2-jelly-bean
android-sdcard
perforce-integrate
adp
codecharge
highlighting
apple-configurator
mathnet
verisign
jsr363
ui4j
gdl
linuxbrew
in-app-billing
smart-tv
ibm-was
apache-commons-digester
domino-designer-eclipse
android-handler
bootstrap-wysiwyg
textscan
actionpack
wicked-gem
webproject
subfolder
mojo
jolie
fasterxml
hp-idol-ondemand
wicket-1.5
full-text-indexing
otl
jini
csslint
sql-function
boolean-algebra
meteorite
disjoint-union
quickgraph
jacob
chefspec
onconfigurationchanged
live-wallpaper
word-frequency
turbine
suphp
gtktextview
activex-exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile