Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Last active August 21, 2022 14:53
Show Gist options
  • Save Weiyuan-Lane/6fe146ac133df44aebd285e48060038b to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/6fe146ac133df44aebd285e48060038b to your computer and use it in GitHub Desktop.
Painless scripting, simple aggregation example
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