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);
}
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();
}
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);
}
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);
}
}
Aggregations