Search in sources :

Example 1 with SearchTemplateResponse

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();
}
Also used : SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.elasticsearch.script.mustache.MultiSearchTemplateRequest) MultiSearchTemplateResponse(org.elasticsearch.script.mustache.MultiSearchTemplateResponse) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) Span(co.elastic.apm.agent.impl.transaction.Span) AbstractEsClientInstrumentationTest(co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest) Test(org.junit.Test)

Example 2 with SearchTemplateResponse

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);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ParsedTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) List(java.util.List) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with SearchTemplateResponse

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) Client(io.apiman.gateway.engine.beans.Client) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with SearchTemplateResponse

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();
        }
    });
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) IOException(java.io.IOException)

Example 5 with SearchTemplateResponse

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);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ParsedTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) List(java.util.List) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchTemplateRequest (org.elasticsearch.script.mustache.SearchTemplateRequest)6 SearchTemplateResponse (org.elasticsearch.script.mustache.SearchTemplateResponse)6 HashMap (java.util.HashMap)5 SearchRequest (org.elasticsearch.action.search.SearchRequest)5 IOException (java.io.IOException)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 List (java.util.List)2 ParsedTerms (org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms)2 AbstractEsClientInstrumentationTest (co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest)1 Span (co.elastic.apm.agent.impl.transaction.Span)1 Client (io.apiman.gateway.engine.beans.Client)1 ClientNotFoundException (io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException)1 MultiSearchTemplateRequest (org.elasticsearch.script.mustache.MultiSearchTemplateRequest)1 MultiSearchTemplateResponse (org.elasticsearch.script.mustache.MultiSearchTemplateResponse)1 SearchHits (org.elasticsearch.search.SearchHits)1 Test (org.junit.Test)1