Hello,
to make my question clear i am attaching steps to reproduce:
PUT /test
{
"mappings": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"custom_rules" : {
"type" : "nested"
}
}
}
}
PUT /test/_doc/1
{
"order_data": {
"user_id": "user-1",
"tenant_id": "tenant-id-1",
"price": 1000,
"tags": [
"a",
"b"
],
"custom_rules": [
{
"rule_id": "111",
"rule_categories": [
"demo1",
"demo2"
]
}
]
}
}
PUT /test/_doc/2
{
"order_data": {
"user_id": "user-1",
"tenant_id": "tenant-id-1",
"price": 500,
"tags": [
"a",
"c"
],
"custom_rules": [
{
"rule_id": "111",
"rule_categories": [
"demo1",
"demo2"
]
},
{
"rule_id": "222",
"rule_categories": [
"demo1",
"demo3"
]
}
]
}
}
PUT /test/_doc/3
{
"order_data": {
"user_id": "user-2",
"tenant_id": "tenant-id-1",
"price": 50,
"tags": [
"a",
"b"
],
"custom_rules": [
{
"rule_id": "111",
"rule_categories": [
"demo1",
"demo2"
]
}
]
}
}
GET test/_search?size=0
{
"aggs": {
"tenant": {
"terms": {
"field": "order_data.tenant_id",
"size": 10
},
"aggs": {
"user": {
"terms": {
"field": "order_data.user_id",
"size": 10
},
"aggs": {
"price": {
"sum": {
"field": "order_data.price"
}
},
"tags": {
"terms": {
"field": "order_data.tags",
"size": 10
}
},
"custom_rules": {
"terms": {
"field": "order_data.custom_rules",
"size": 10
}
}
}
}
}
}
}
}
For each user i get the sum of all price fields, and list of unique tag names. This works as expected in the query above.
I would also like to get the the "unique" set (i.e. something like Hashset in java) of custom rules for each user. So for example, for user-1 I would like to get:
"custom_rules": [
{
"rule_id": "111",
"rule_categories": [
"demo1",
"demo2"
]
},
{
"rule_id": "222",
"rule_categories": [
"demo1",
"demo3"
]
}
]
The query i pasted above is not returning any custom_rules.
How can i accomplish this?
your guidance is much appreciated