I have an index with the following mapping:
"properties": {
"content": {
"type": "text",
},
"ar_name": {
"type": "text"
}
}
I want to get statistics (min length, max length and average length) to the content
field.
How can I do it ?
CodePudding user response:
The string_stats
aggregation provides that support but only for keyword
fields. Since your content field is of type text
, I assume it contains free-text that is not really suitable as keyword
.
If that's the case, I would suggest that you stored the length of the content
field in another integer field (e.g. content_length
) that you can then use in a stats
aggregation that would return you the metrics you expect.
CodePudding user response:
If you want to keep field as text or you don't want to change the mapping, you can achieve this by a script in aggregation. Here is an example query:
{
"size": 0,
"aggs": {
"min": {
"min": {
"script": {
"source": """
return doc['content'].value.length();
"""
}
}
},
"max": {
"max": {
"script": {
"source": """
return doc['content'].value.length();
"""
}
}
},
"avg": {
"avg": {
"script": {
"source": """
return doc['content'].value.length();
"""
}
}
}
}
}