Last active
August 22, 2020 01:39
-
-
Save jpotts/48e42e35bc64be9a203e to your computer and use it in GitHub Desktop.
Code snippets that go along with an ecmarchitect.com blog post on Elasticsearch search templates.
This file contains 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
POST /_search/template/tweets | |
{ | |
"template": { | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"message": "{{ search_term }}" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains 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
POST /twitter/tweet/_search/template | |
{ | |
"file": "tweets", | |
"params": { | |
"search_term": "elasticsearch" | |
} | |
} |
This file contains 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
POST /twitter/tweet/_search/template | |
{ | |
"id": "tweets", | |
"params": { | |
"search_term": "elasticsearch" | |
} | |
} |
This file contains 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
POST /twitter/tweet/_search/template | |
{ | |
"file": "tweets", | |
"params": { | |
"search_term": "elasticsearch", | |
"since": "now-30d" | |
} | |
} |
This file contains 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
Map<String, Object> params = new HashMap<String, Object>(); | |
params.put("search_term", "elasticsearch"); | |
params.put("since", "now-30d"); | |
Template template = new Template("tweets", ScriptService.ScriptType.FILE, MustacheScriptEngineService.NAME, null, params); | |
SearchRequestBuilder request = client.prepareSearch(INDEX).setTemplate(template); | |
SearchResponse response = request.execute().actionGet(); |
This file contains 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
{ | |
"search_term": "elasticsearch", | |
"since": "now-30d", | |
"userList": { | |
"users": [ | |
{ | |
"name": "elastic" | |
}, | |
{ | |
"name": "jeffpotts01", | |
"isLast": true | |
} | |
] | |
} | |
} |
This file contains 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
{ | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [{ | |
"and": [{ | |
"term": { | |
"message": "elasticsearch" | |
} | |
}, { | |
"range": { | |
"@timestamp": { | |
"gte": "now-30d" | |
} | |
} | |
}, { | |
"or": [{ | |
"term": { | |
"user": "elastic" | |
} | |
}, { | |
"term": { | |
"user": "jeffpotts01" | |
} | |
}] | |
}] | |
}] | |
} | |
} | |
} | |
} | |
} |
This file contains 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
{ | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"and": [ | |
{ | |
"term": { | |
"message": "{{ search_term }}" | |
} | |
} | |
{{#since}} | |
, | |
{ | |
"range": { | |
"@timestamp": { | |
"gte": "{{ since }}" | |
} | |
} | |
} | |
{{/since}} | |
{{#userList}} | |
, | |
{ | |
"or": [ | |
{{#users}} | |
{ | |
"term": { | |
"user": "{{ name }}" | |
} | |
} | |
{{^isLast}},{{/isLast}} | |
{{/users}} | |
] | |
} | |
{{/userList}} | |
] | |
} | |
] | |
} | |
} | |
} | |
} | |
} |
This file contains 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
{ | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"and": [ | |
{ | |
"term": { | |
"message": "{{ search_term }}" | |
} | |
} | |
{{#since}} | |
, | |
{ | |
"range": { | |
"@timestamp": { | |
"gte": "{{ since }}" | |
} | |
} | |
} | |
{{/since}} | |
{{^since}} | |
, | |
{ | |
"range": { | |
"@timestamp": { | |
"gte": "now-1d" | |
} | |
} | |
} | |
{{/since}} | |
] | |
} | |
] | |
} | |
} | |
} | |
} | |
} |
This file contains 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
POST /twitter/tweet/_search/template | |
{ | |
"inline": { | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"message": "{{ search_term }}" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
}, | |
"params": { | |
"search_term": "elasticsearch" | |
} | |
} |
This file contains 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
{ | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"and": [ | |
{ | |
"term": { | |
"message": "{{ search_term }}" | |
} | |
} | |
{{#since}} | |
, | |
{ | |
"range": { | |
"@timestamp": { | |
"gte": "{{ since }}" | |
} | |
} | |
} | |
{{/since}} | |
] | |
} | |
] | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment