'How to get min/max and avg lengths of text field?
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 ?
Solution 1:[1]
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.
Solution 2:[2]
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();
"""
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Val |
| Solution 2 | YD9 |
