|
@@ -17,35 +17,26 @@ import org.elasticsearch.cluster.service.ClusterService;
|
|
|
import org.elasticsearch.common.inject.Inject;
|
|
|
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
|
|
import org.elasticsearch.common.util.BigArrays;
|
|
|
-import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
|
|
import org.elasticsearch.license.XPackLicenseState;
|
|
|
import org.elasticsearch.logging.LogManager;
|
|
|
import org.elasticsearch.logging.Logger;
|
|
|
-import org.elasticsearch.script.Script;
|
|
|
import org.elasticsearch.script.ScriptService;
|
|
|
-import org.elasticsearch.script.TemplateScript;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.elasticsearch.transport.TransportService;
|
|
|
import org.elasticsearch.xcontent.NamedXContentRegistry;
|
|
|
-import org.elasticsearch.xcontent.XContentFactory;
|
|
|
-import org.elasticsearch.xcontent.XContentParser;
|
|
|
-import org.elasticsearch.xcontent.XContentParserConfiguration;
|
|
|
-import org.elasticsearch.xcontent.XContentType;
|
|
|
+import org.elasticsearch.xpack.application.search.SearchApplicationTemplateService;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
public class TransportQuerySearchApplicationAction extends SearchApplicationTransportAction<
|
|
|
- QuerySearchApplicationAction.Request,
|
|
|
+ SearchApplicationSearchRequest,
|
|
|
SearchResponse> {
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger(TransportQuerySearchApplicationAction.class);
|
|
|
|
|
|
private final Client client;
|
|
|
- private final ScriptService scriptService;
|
|
|
|
|
|
- private final NamedXContentRegistry xContentRegistry;
|
|
|
+ private final SearchApplicationTemplateService templateService;
|
|
|
|
|
|
@Inject
|
|
|
public TransportQuerySearchApplicationAction(
|
|
@@ -54,16 +45,16 @@ public class TransportQuerySearchApplicationAction extends SearchApplicationTran
|
|
|
Client client,
|
|
|
ClusterService clusterService,
|
|
|
NamedWriteableRegistry namedWriteableRegistry,
|
|
|
- NamedXContentRegistry xContentRegistry,
|
|
|
BigArrays bigArrays,
|
|
|
+ XPackLicenseState licenseState,
|
|
|
ScriptService scriptService,
|
|
|
- XPackLicenseState licenseState
|
|
|
+ NamedXContentRegistry xContentRegistry
|
|
|
) {
|
|
|
super(
|
|
|
QuerySearchApplicationAction.NAME,
|
|
|
transportService,
|
|
|
actionFilters,
|
|
|
- QuerySearchApplicationAction.Request::new,
|
|
|
+ SearchApplicationSearchRequest::new,
|
|
|
client,
|
|
|
clusterService,
|
|
|
namedWriteableRegistry,
|
|
@@ -71,17 +62,14 @@ public class TransportQuerySearchApplicationAction extends SearchApplicationTran
|
|
|
licenseState
|
|
|
);
|
|
|
this.client = client;
|
|
|
- this.scriptService = scriptService;
|
|
|
- this.xContentRegistry = xContentRegistry;
|
|
|
+ this.templateService = new SearchApplicationTemplateService(scriptService, xContentRegistry);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void doExecute(QuerySearchApplicationAction.Request request, ActionListener<SearchResponse> listener) {
|
|
|
+ protected void doExecute(SearchApplicationSearchRequest request, ActionListener<SearchResponse> listener) {
|
|
|
systemIndexService.getSearchApplication(request.name(), listener.delegateFailure((l, searchApplication) -> {
|
|
|
- final Script script = searchApplication.searchApplicationTemplate().script();
|
|
|
-
|
|
|
try {
|
|
|
- final SearchSourceBuilder sourceBuilder = renderTemplate(script, mergeTemplateParams(request, script));
|
|
|
+ SearchSourceBuilder sourceBuilder = templateService.renderQuery(searchApplication, request.queryParams());
|
|
|
SearchRequest searchRequest = new SearchRequest(searchApplication.indices()).source(sourceBuilder);
|
|
|
|
|
|
client.execute(
|
|
@@ -94,24 +82,4 @@ public class TransportQuerySearchApplicationAction extends SearchApplicationTran
|
|
|
}
|
|
|
}));
|
|
|
}
|
|
|
-
|
|
|
- private static Map<String, Object> mergeTemplateParams(QuerySearchApplicationAction.Request request, Script script) {
|
|
|
- Map<String, Object> mergedTemplateParams = new HashMap<>(script.getParams());
|
|
|
- mergedTemplateParams.putAll(request.queryParams());
|
|
|
-
|
|
|
- return mergedTemplateParams;
|
|
|
- }
|
|
|
-
|
|
|
- private SearchSourceBuilder renderTemplate(Script script, Map<String, Object> templateParams) throws IOException {
|
|
|
- TemplateScript compiledTemplate = scriptService.compile(script, TemplateScript.CONTEXT).newInstance(templateParams);
|
|
|
- String requestSource = compiledTemplate.execute();
|
|
|
-
|
|
|
- XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
|
|
|
- .withDeprecationHandler(LoggingDeprecationHandler.INSTANCE);
|
|
|
- try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(parserConfig, requestSource)) {
|
|
|
- SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
|
|
|
- builder.parseXContent(parser, false);
|
|
|
- return builder;
|
|
|
- }
|
|
|
- }
|
|
|
}
|