Created
August 6, 2014 20:22
-
-
Save anonymous/4f06c9322186ce9d4708 to your computer and use it in GitHub Desktop.
Nested object count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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