Build elasticsearch using a script to convert a field value

I currently have something like:

"aggs": {
        "group_by_myfield": {
            "terms": {
                "field": "myfield"
            }
        }
    }

However, the value for myfield is alpha 1.0, alpha 2.0, beta 1.0. Now I want to aggregate only by the values ​​of "alpha", "beta". How should I do it? I have tried:

"aggs": {
        "group_by_myfield": {
            "terms": {
                "field": "myfield"
                "script": "_value.split()[0]"
            }
        }
    }

But I think there is no shared functionality. Any suggestions are welcome!

I found a similar question here , which also remains unanswered.

+7
source share
1 answer

I managed to do this using the link inserted in my question:

GET _search
{
    "size": 0, 
    "aggs": {
        "group_by_myfield": {
            "terms": {
                "field": "myfield",
                "script": "_value.replaceAll('\\\\s+.*','')"
            }
        }
    }
}
+6
source

Source: https://habr.com/ru/post/1537793/


All Articles