use of org.elasticsearch.script.mustache.SearchTemplateRequest in project apm-agent-java by elastic.
the class AbstractEs6_4ClientInstrumentationTest method testMultisearchTemplateRequest_validateSpanContentAndDbContext.
@Test
public void testMultisearchTemplateRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
createDocument();
reporter.reset();
SearchTemplateRequest searchTemplateRequest = prepareSearchTemplateRequest();
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
multiRequest.add(searchTemplateRequest);
MultiSearchTemplateResponse response = doMultiSearchTemplate(multiRequest);
MultiSearchTemplateResponse.Item[] items = response.getResponses();
assertThat(items.length).isEqualTo(1);
verifyTotalHits(items[0].getResponse().getResponse().getHits());
List<Span> spans = reporter.getSpans();
assertThat(spans).hasSize(1);
Span span = spans.get(0);
validateSpanContent(span, String.format("Elasticsearch: POST /_msearch/template", INDEX), 200, "POST");
verifyMultiSearchTemplateSpanContent(span);
deleteDocument();
}
use of org.elasticsearch.script.mustache.SearchTemplateRequest in project apm-agent-java by elastic.
the class AbstractEs6_4ClientInstrumentationTest method prepareSearchTemplateRequest.
private SearchTemplateRequest prepareSearchTemplateRequest() {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(INDEX));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript("{" + " \"query\": { \"term\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + "}");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", FOO);
scriptParams.put("value", BAR);
scriptParams.put("size", 5);
searchTemplateRequest.setScriptParams(scriptParams);
return searchTemplateRequest;
}
use of org.elasticsearch.script.mustache.SearchTemplateRequest in project apiman by apiman.
the class EsRegistry method listApiVersions.
@Override
@SuppressWarnings("nls")
public void listApiVersions(String organizationId, String apiId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [" + " {" + " \"term\": {" + // organizationId
" \"organizationId\": \"{{organizationId}}\"" + " }" + " }," + " {" + " \"term\": {" + // apiId
" \"apiId\": \"{{apiId}}\"" + " }" + " }" + " ]" + " }" + " }," + " \"aggs\": {" + " \"api_versions\": {" + " \"terms\": {" + // only return version fields of APIs
" \"field\": \"version\"" + " }" + " }" + " }" + "}";
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(getIndexPrefix() + EsConstants.INDEX_APIS));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript(query);
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("organizationId", organizationId);
scriptParams.put("apiId", apiId);
searchTemplateRequest.setScriptParams(scriptParams);
try {
SearchTemplateResponse response = getClient().searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
List terms = ((ParsedTerms) searchResponse.getAggregations().asMap().get("api_versions")).getBuckets();
// Grab only the name of each aggregation (we don't care about count
List<String> results = (List<String>) terms.stream().map(o -> ((ParsedTerms.ParsedBucket) o).getKey()).collect(Collectors.toList());
handler.handle(AsyncResultImpl.create(results));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.elasticsearch.script.mustache.SearchTemplateRequest in project apiman by apiman.
the class EsMetricsAccessor method doSearchTemplateRequest.
/**
* Does a request against elasticsearch
* @param query the query template to execute
* @param params the params for the query template
* @return SearchResponse of elasticsearch
* @throws IOException
*/
private SearchResponse doSearchTemplateRequest(String query, Map<String, Object> params) throws IOException {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(INDEX_NAME));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript(query);
searchTemplateRequest.setScriptParams(params);
return getClient().searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT).getResponse();
}
use of org.elasticsearch.script.mustache.SearchTemplateRequest in project spring-data-elasticsearch by spring-projects.
the class RequestConverters method searchTemplate.
public static Request searchTemplate(SearchTemplateRequest templateRequest) {
SearchRequest searchRequest = templateRequest.getRequest();
String endpoint = new EndpointBuilder().addCommaSeparatedPathParts(templateRequest.getRequest().indices()).addPathPart("_search").addPathPart("template").build();
Request request = new Request(HttpMethod.GET.name(), endpoint);
Params params = new Params(request);
addSearchRequestParams(params, searchRequest);
request.setEntity(createEntity(templateRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
Aggregations