Elastic search polymerization on different keys

I want to aggregate my documents on different keys in the “Category” field.
These are two files:

"date": 1470271301,
"categories": {
"1": [blabla],
"2": [blala]
}


"date": 144343545,
"categories": {
"1": [blabla],
"2": [coco]
"3": [rat, saouth]
}

Category mapping:

"categories": {
"properties ": {
"1": {
"type": "long"

I want something like this:

"buckets": [{
"key": "1",
"doc_count": 2
}, {
"key": "2",
"doc_count": 2
{
"key": "3",
"doc_count": 1
}

Is there a good one Way to do this without changing the mapping of my document?

You can use meta field _field_names to achieve this purpose.

As shown in the example below, running the aggregation here will provide you with a document count.

Example:

put test/test/1 
{
"date": 1470271301,
"categories": {
"1": ["blabla"],
"2": ["blala"]
}
}
put test/test/2
{
"date": 144343545,
"categories": {
"1": ["blabla"],
"2": ["coco"],
"3": ["rat", "saouth"]
}
}

POST test/_search
{
"size": 0,
"aggs": {
"field_documents": {
"terms": {
"field": "_field_names",
"include": "categories.*",
"size": 0
}
}
}
}

Result:

"aggregations": {
"field_documents": {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count": 0,
"buck ets": [
{
"key": "categories",
"doc_count": 2
},
{
"key": " categories.1",
"doc_count": 2
},
{
"key": "categories.2",
"doc_count": 2
},
{
"key": "categories.3",
"doc_count": 1
}
]
}
}

I want to aggregate my documents on different keys in the “Category” field.
These are two files:

"date": 1470271301,
"categories": {
"1": [blabla],
"2": [blala]
}


"date": 144343545,
"categories": {
"1": [blabla],
"2": [coco]
"3": [rat, saouth]
}

Category mapping:

"categories": { 
"properties": {
"1": {
"type": "long"

I hope to get something like this:

"buckets": [{
"key": "1",
"doc_count ": 2
}, {
"key": "2",
"doc_count": 2
{
"key": "3",
"doc_count": 1
}

Is there a good way to do this without changing the mapping of my document?

The meta field _field_names can be used for this purpose.

As shown in the following example, run the aggregation here Provide you with a document count.

Example:

put test/test/1 
{
"date": 1470271301,
"categories": {
"1": ["blabla"],
"2": ["blala"]
}
}
put test/test/2
{
"date": 144343545,
"categories": {
"1": ["blabla"],
"2 ": ["coco"],
"3": ["rat", "saouth"]
}
}

POST test/_search
{
"size": 0,
"aggs": {
"field_documents": {
"terms": {
"field": "_field_names" ,
"include": "categories.*",
"size": 0
}
}
}
}

Result:

"aggregations": {
"field_documents": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0 ,
"buckets": [
{
"key": "cate gories",
"doc_count": 2
},
{
"key": "categories.1",
"doc_count": 2
},
{
"key": "categories.2",
"doc_count": 2
},
{
"key": " categories.3",
"doc_count": 1
}
]
}
}

Leave a Comment

Your email address will not be published.