Hi all,
I'am trying to use nested aggregation with min_doc_count=0
My schema:
PUT /example
{
"mappings": {
"doc": {
"properties": {
"id": {
"type": "text"
},
"properties": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
}
}
}
}
}
}
My documents are like this:
{
"id": "1",
"properties": [
{
"key": "color",
"value": [
"red"
]
},
{
"key": "size",
"value": [
"big"
]
},
{
"key": "shape",
"value": [
"circle"
]
}
]
}
and this is my values:
id=1 color=red size=big shape=circle
id=2 color=blue size=big shape=square
id=3 color=red size=small shape=circle
id=4 color=yellow size=medium shape=rectangle
This is my search request example, I want only document with red color, and with aggregations I want to know results properties and associated count :
{
"_source": [
"id"
],
"query": {
"bool": {
"must": [
{
"nested": {
"path": "properties",
"query": {
"bool": {
"must": [
{
"match": {
"properties.key": "color"
}
},
{
"match": {
"properties.value": "red"
}
}
]
}
}
}
}
]
}
},
"aggregations": {
"filters": {
"nested": {
"path": "properties"
},
"aggregations": {
"key": {
"terms": {
"field": "properties.key"
},
"aggregations": {
"value": {
"terms": {
"field": "properties.value"
}
}
}
}
}
}
}
}
The result is OK, I retrieve documents with id=1 and id=3, and with aggregations I know the 2 documents are circle, and one is small and the other is big:
"aggregations": {
"filters": {
"doc_count": 6,
"key": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "color",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 2
}
]
}
},
{
"key": "shape",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "circle",
"doc_count": 2
}
]
}
},
{
"key": "size",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "big",
"doc_count": 1
},
{
"key": "small",
"doc_count": 1
}
]
}
}
]
}
}
}
Now the problem is if I want to know the others possiblity of properties and to display the properties values with no document associated.
I changed my search request by adding "min_doc_count: 0
on the "field": "properties.values"
aggregation.
Result, all properties values are mixed and not sorting by key:
"aggregations": {
"filters": {
"doc_count": 4,
"filters": {
"doc_count": 2,
"properties": {
"doc_count": 6,
"key": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "color",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 2
},
{
"key": "big",
"doc_count": 0
},
{
"key": "circle",
"doc_count": 0
},
{
"key": "small",
"doc_count": 0
}
]
}
},
{
"key": "shape",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "circle",
"doc_count": 2
},
{
"key": "big",
"doc_count": 0
},
{
"key": "red",
"doc_count": 0
},
{
"key": "small",
"doc_count": 0
}
]
}
},
{
...
It is possible to obtain only values by key in the result aggregation ?
Thank's for your help.
Julien