Search in sources :

Example 6 with SearchTemplateRequest

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

Example 7 with SearchTemplateRequest

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

Example 8 with 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);
    }
}
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 9 with SearchTemplateRequest

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

Example 10 with SearchTemplateRequest

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) CountRequest(org.elasticsearch.client.core.CountRequest) IndexTemplatesExistRequest(org.elasticsearch.client.indices.IndexTemplatesExistRequest) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) GetRequest(org.elasticsearch.action.get.GetRequest) RankEvalRequest(org.elasticsearch.index.rankeval.RankEvalRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AbstractBulkByScrollRequest(org.elasticsearch.index.reindex.AbstractBulkByScrollRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) PutStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) DeleteStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) GetFieldMappingsRequest(org.elasticsearch.client.indices.GetFieldMappingsRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Request(org.elasticsearch.client.Request) AnalyzeRequest(org.elasticsearch.client.indices.AnalyzeRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) RethrottleRequest(org.elasticsearch.client.RethrottleRequest) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest)

Aggregations

SearchTemplateRequest (org.elasticsearch.script.mustache.SearchTemplateRequest)14 SearchRequest (org.elasticsearch.action.search.SearchRequest)10 HashMap (java.util.HashMap)7 SearchTemplateResponse (org.elasticsearch.script.mustache.SearchTemplateResponse)6 IOException (java.io.IOException)4 List (java.util.List)3 MultiSearchTemplateRequest (org.elasticsearch.script.mustache.MultiSearchTemplateRequest)3 AbstractEsClientInstrumentationTest (co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest)2 Span (co.elastic.apm.agent.impl.transaction.Span)2 LinkedHashMap (java.util.LinkedHashMap)2 PutMappingRequest (org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)2