use of org.elasticsearch.script.mustache.SearchTemplateResponse in project apm-agent-java by elastic.
the class AbstractEs6_4ClientInstrumentationTest method testSearchTemplateRequest_validateSpanContentAndDbContext.
@Test
public void testSearchTemplateRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
createDocument();
reporter.reset();
SearchTemplateRequest searchTemplateRequest = prepareSearchTemplateRequest();
SearchTemplateResponse response = doSearchTemplate(searchTemplateRequest);
verifyTotalHits(response.getResponse().getHits());
List<Span> spans = reporter.getSpans();
assertThat(spans).hasSize(1);
Span span = spans.get(0);
validateSpanContent(span, String.format("Elasticsearch: GET /%s/_search/template", INDEX), 200, "GET");
validateDbContextContent(span, "{\"source\":\"{ \\\"query\\\": { \\\"term\\\" : { \\\"{{field}}\\\" : \\\"{{value}}\\\" } }, \\\"size\\\" : \\\"{{size}}\\\"}\",\"params\":{\"field\":\"foo\",\"size\":5,\"value\":\"bar\"},\"explain\":false,\"profile\":false}");
deleteDocument();
}
use of org.elasticsearch.script.mustache.SearchTemplateResponse in project apiman by apiman.
the class EsRegistry method listClientVersions.
@Override
@SuppressWarnings("nls")
public void listClientVersions(String organizationId, String clientId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [" + " {" + " \"term\": {" + // organizationId
" \"organizationId\": \"{{organizationId}}\"" + " }" + " }," + " {" + " \"term\": {" + // clientId
" \"clientId\": \"{{clientId}}\"" + " }" + " }" + " ]" + " }" + " }," + " \"aggs\": {" + " \"client_versions\": {" + " \"terms\": {" + // only return version fields of clients
" \"field\": \"version\"" + " }" + " }" + " }" + "}";
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(getIndexPrefix() + EsConstants.INDEX_CLIENTS));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript(query);
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("organizationId", organizationId);
scriptParams.put("clientId", clientId);
searchTemplateRequest.setScriptParams(scriptParams);
try {
SearchTemplateResponse response = getClient().searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
List terms = ((ParsedTerms) searchResponse.getAggregations().asMap().get("client_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.SearchTemplateResponse in project apiman by apiman.
the class EsRegistry method lookupClient.
/**
* Searches for a client by its orgid:clientId:version and returns it.
* @param orgId the organization id
* @param clientId the client id
* @param version the version
*/
// Do beans need escaping or will that be done 'automatically'. Test it. Strings do, but probably only quotes?
@SuppressWarnings("nls")
private Client lookupClient(String orgId, String clientId, String version) {
String query = "{" + " \"query\": {" + " \"bool\": {" + " \"filter\": [{" + " \"term\": {" + // orgId
" \"organizationId\": \"{{organizationId}}\" " + " }" + " }," + " {" + " \"term\": {" + // clientId
" \"clientId\": \"{{clientId}}\" " + " }" + " }," + " {" + " \"term\": {" + // version
" \"version\": \"{{version}}\" " + " }" + " }" + " ]" + " }" + " }" + "}";
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(getIndexPrefix() + EsConstants.INDEX_CLIENTS));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript(query);
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("organizationId", orgId);
scriptParams.put("clientId", clientId);
scriptParams.put("version", version);
searchTemplateRequest.setScriptParams(scriptParams);
Client client;
try {
SearchTemplateResponse response = getClient().searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
SearchHits hits = searchResponse.getHits();
if (hits.getTotalHits().value == 0) {
throw new IOException();
}
String sourceAsString = response.getResponse().getHits().getAt(0).getSourceAsString();
client = JSON_MAPPER.readValue(sourceAsString, Client.class);
} catch (IOException e) {
// $NON-NLS-1$
throw new ClientNotFoundException(Messages.i18n.format("EsRegistry.ClientNotFound"), e);
}
return client;
}
use of org.elasticsearch.script.mustache.SearchTemplateResponse in project skywalking-java by apache.
the class RestHighLevelClientCase method searchTemplateAsync.
private void searchTemplateAsync(RestHighLevelClient client, String indexName) throws IOException {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest(indexName));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript("{\n" + " \"from\": \"{{from}}\",\n" + " \"size\": \"{{size}}\",\n" + " \"query\": {\n" + " \"term\": {\n" + " \"{{field}}\": {\n" + " \"value\": \"{{value}}\"\n" + " }\n" + " }\n" + " }\n" + "}");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "author");
scriptParams.put("value", "Marker");
scriptParams.put("size", 10);
scriptParams.put("from", 0);
searchTemplateRequest.setScriptParams(scriptParams);
client.searchTemplateAsync(searchTemplateRequest, RequestOptions.DEFAULT, new ActionListener<SearchTemplateResponse>() {
@Override
public void onResponse(final SearchTemplateResponse searchTemplateResponse) {
if (!(searchTemplateResponse.getResponse().getHits().totalHits > 0)) {
String message = "elasticsearch searchTemplateAsync data fail.";
LOGGER.error(message);
throw new RuntimeException(message);
}
}
@Override
public void onFailure(final Exception e) {
LOGGER.error(e);
throw new RuntimeException();
}
});
}
use of org.elasticsearch.script.mustache.SearchTemplateResponse 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);
}
}
Aggregations