Skip to content

Instantly share code, notes, and snippets.

Created August 6, 2014 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4f06c9322186ce9d4708 to your computer and use it in GitHub Desktop.
Save anonymous/4f06c9322186ce9d4708 to your computer and use it in GitHub Desktop.
Nested object count
What do I need?
Retrieve all buckets which have at least 2 different products. In the example below, the query would return buckets 1 and 3.
Note: I need 2 different product names (p1 and p2) not product quantity (2x p1 or 2x p2)
My mapping definition for a bucket (product is a nested type, mapped automatically by Elasticsearch):
mappings:{
bucket:{
properties:{
id:{type:number},
...
products: {
properties: {
product_name:{ type: string},
has_discount:{type: boolean},
quantity: {type:long}
}
}
}
}
}
Search response example:
-XGET localhost:9200/orders/bucket/_search:
[
{ "_type": bucket
"_source":{
id:1
...
products:[
{
product: "p1",
has_discount: true,
quantity: 2
}
{
product: "p2",
has_discount: false,
quantity: 1
}
{
product: "p3",
has_discount: true,
quantity: 1
}
]
}
},
{ "_type": bucket
"_source":{
id:2
...
products:[
{
product: "p1",
has_discount: false,
quantity: 3
}
]
}
},
{ "_type": bucket
"_source":{
id:3
...
products:[
{
product: "p2",
has_discount: true,
quantity: 2
}
{
product: "p3",
has_discount: false,
quantity: 1
}
]
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment