Last active
August 21, 2022 14:53
-
-
Save Weiyuan-Lane/6fe146ac133df44aebd285e48060038b to your computer and use it in GitHub Desktop.
Painless scripting, simple aggregation example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET /_search | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"query_string": { | |
"query": "message: \"This is a genre log entry!\"", | |
"analyze_wildcard": true | |
} | |
} | |
] | |
} | |
}, | |
"aggs" : { | |
"genres" : { | |
"terms": { | |
"script": { | |
"lang": "painless", | |
"source": """ | |
String message = params._source.message; | |
def matchedInfo = /(?:genre:)(\w+)/.matcher(message); | |
String id; | |
if (matchedInfo.find()) { | |
id = matchedInfo.group(1); | |
} | |
else { | |
id = "no match" | |
} | |
return id; | |
""" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment