Previously I have asked this question.
The example document there was a simplified document. That was good for me to understand the differences in aggregation over non-nested type versus nested type. However, the simplification was hiding further complexity and so I have to expand on the question here.
So my actual documents are closer to the following:
"_source": {
"keyword": "my keyword",
"response": [
{
"results": [
{
"items": [
{
"prop": [
{
"item_property_1": ["A"],
}
]
( ... other properties )
},
{
"prop": [
{
"item_property_1": ["B"],
}
]
( ... other properties )
},
( ... other items )
]
}
],
( ... other properties )
}
]
}
So I kept the crucial properties keyword
, items
, and item_property_1
, but hid lots of other things that complicate the situation. First, notice that compared to the referenced question there is lots of extra nesting: between the root and "items", and between "items" and "item_property_1". Additionally, notice also that the properties response
and results
are both arrays with a single element. It's weird, but that's how it is :-)
Now, the reason why this question is different from the one cited above is that I tried the accepted answer (which does work for the example there), and it doesn't work here. That is, if I use a mapping with:
"items": {
"type":"nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
},
}
}
}
}
then the aggregation doesn't work. It returns zero hits.
I will edit later and provider a ready to use sample bulk insert.
EDIT: Alright, below I show three queries which are respectively: mapping, bulk insert and aggregation (with zero hits)
Mapping (with "type":"nested"
as indicated in the previous answered question)
PUT /test2/_mapping/test3
{
"test3": {
"properties": {
"keyword": {
"type": "string",
"index": "not_analyzed"
},
"response": {
"properties": {
"results": {
"properties": {
"items": {
"type": "nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
}
}
}
Bulk put:
PUT /test2/test3/_bulk
{ "index": {}}
{ "keyword": "my keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["B"]} ] }, { "prop": [ {"item_property_1": ["A"]} ] } ] } ] } ]}
{ "index": {}}
{ "keyword": "different keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["C"]} ] } ] } ] } ]}
Aggregation (zero hits):
POST /test2/test3/_search
{
"size":0,
"aggregations": {
"item_property_1_count": {
"terms":{
"field":"item_property_1"
}
}
}
}