Search in sources :

Example 11 with SearchTemplateRequest

use of org.elasticsearch.script.mustache.SearchTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClient method searchTemplate.

/**
 * Executes a {@link SearchTemplateRequest} against the {@literal search template} API.
 *
 * @param consumer must not be {@literal null}.
 * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html">Search Template
 *      API on elastic.co</a>
 * @return the {@link Flux} emitting {@link SearchHit hits} one by one.
 */
default Flux<SearchHit> searchTemplate(Consumer<SearchTemplateRequest> consumer) {
    SearchTemplateRequest request = new SearchTemplateRequest();
    consumer.accept(request);
    return searchTemplate(request);
}
Also used : SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest)

Example 12 with SearchTemplateRequest

use of org.elasticsearch.script.mustache.SearchTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientIntegrationTests method storedSearchTemplateShouldFindMatchingDocuments.

// #1725
@Test
public void storedSearchTemplateShouldFindMatchingDocuments() {
    addSourceDocument().to(INDEX_I);
    addSourceDocument().to(INDEX_I);
    Map<String, Object> testDoc = new LinkedHashMap<>();
    testDoc.put("firstname", "stored");
    testDoc.put("lastname", "template");
    add(testDoc).to(INDEX_I);
    client.execute(c -> c.post().uri(builder -> builder.path("_scripts/searchbyfirstname").build()).contentType(MediaType.APPLICATION_JSON).bodyValue("{\"script\":{\"lang\":\"mustache\",\"source\":{\"query\":{\"match\":{\"firstname\":\"{{firstname}}\"}}}}}").retrieve().bodyToMono(Void.class)).block();
    client.indices().refreshIndex(request -> request.indices(INDEX_I)).block();
    SearchTemplateRequest request = new SearchTemplateRequest(new SearchRequest(INDEX_I));
    request.setScriptType(ScriptType.STORED);
    request.setScript("searchbyfirstname");
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("firstname", "stored");
    request.setScriptParams(params);
    // 
    client.searchTemplate(request).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) StepVerifier(reactor.test.StepVerifier) ReactiveIndexOperations(org.springframework.data.elasticsearch.core.ReactiveIndexOperations) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) ClientConfiguration(org.springframework.data.elasticsearch.client.ClientConfiguration) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Duration(java.time.Duration) Map(java.util.Map) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) Assertions(org.assertj.core.api.Assertions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TimeValue(org.elasticsearch.core.TimeValue) GetRequest(org.elasticsearch.action.get.GetRequest) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) List(java.util.List) RestStatus(org.elasticsearch.rest.RestStatus) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Document(org.springframework.data.elasticsearch.core.document.Document) IntStream(java.util.stream.IntStream) ReactiveElasticsearchOperations(org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations) ScriptType(org.elasticsearch.script.ScriptType) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) ReactiveElasticsearchRestTemplateConfiguration(org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration) LinkedHashMap(java.util.LinkedHashMap) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Script(org.elasticsearch.script.Script) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Mono(reactor.core.publisher.Mono) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) DisplayName(org.junit.jupiter.api.DisplayName) AfterEach(org.junit.jupiter.api.AfterEach) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) StepVerifier(reactor.test.StepVerifier) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 13 with SearchTemplateRequest

use of org.elasticsearch.script.mustache.SearchTemplateRequest in project skywalking-java by apache.

the class RestHighLevelClientSearchTemplateMethodsInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    SearchTemplateRequest searchTemplateRequest = (SearchTemplateRequest) allArguments[0];
    RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
    AbstractSpan span = ContextManager.createExitSpan(Constants.SEARCH_TEMPLATE_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
    span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
    Tags.DB_TYPE.set(span, DB_TYPE);
    if (searchTemplateRequest.getRequest() != null) {
        Tags.DB_INSTANCE.set(span, Arrays.asList(searchTemplateRequest.getRequest().indices()).toString());
    }
    if (TRACE_DSL) {
        Tags.DB_STATEMENT.set(span, searchTemplateRequest.getScript());
    }
    SpanLayer.asDB(span);
}
Also used : SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) RestClientEnhanceInfo(org.apache.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 14 with SearchTemplateRequest

use of org.elasticsearch.script.mustache.SearchTemplateRequest in project skywalking-java by apache.

the class RestHighLevelClientCase method searchTemplate.

private void searchTemplate(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);
    SearchTemplateResponse searchTemplateResponse = client.searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT);
    if (!(searchTemplateResponse.getResponse().getHits().totalHits > 0)) {
        String message = "elasticsearch searchTemplateAsync data fail.";
        LOGGER.error(message);
        throw new RuntimeException(message);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse)

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