fixes for compile errors due to search request refactoring in core
Original commit: elastic/x-pack-elasticsearch@8808789912
This commit is contained in:
parent
4dd5bfe0e7
commit
4cd02142a2
|
@ -16,7 +16,11 @@ import org.elasticsearch.cluster.ClusterService;
|
|||
import org.elasticsearch.common.inject.util.Providers;
|
||||
import org.elasticsearch.common.network.NetworkAddress;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.*;
|
||||
import org.elasticsearch.common.transport.BoundTransportAddress;
|
||||
import org.elasticsearch.common.transport.DummyTransportAddress;
|
||||
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
||||
import org.elasticsearch.common.transport.LocalTransportAddress;
|
||||
import org.elasticsearch.common.transport.TransportAddress;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.index.IndexNotFoundException;
|
||||
import org.elasticsearch.index.cache.IndexCacheModule;
|
||||
|
@ -46,7 +50,11 @@ import org.junit.Test;
|
|||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.elasticsearch.shield.audit.index.IndexNameResolver.Rollover.DAILY;
|
||||
import static org.elasticsearch.shield.audit.index.IndexNameResolver.Rollover.HOURLY;
|
||||
|
@ -59,9 +67,9 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
|
|
@ -9,9 +9,9 @@ import org.elasticsearch.action.search.SearchRequest;
|
|||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -43,6 +43,7 @@ public class ExecutableSearchInput extends ExecutableInput<SearchInput, SearchIn
|
|||
this.timeout = input.getTimeout() != null ? input.getTimeout() : defaultTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchInput.Result execute(WatchExecutionContext ctx) {
|
||||
SearchRequest request = null;
|
||||
try {
|
||||
|
@ -56,8 +57,8 @@ public class ExecutableSearchInput extends ExecutableInput<SearchInput, SearchIn
|
|||
|
||||
SearchInput.Result doExecute(WatchExecutionContext ctx, SearchRequest request) throws Exception {
|
||||
if (logger.isTraceEnabled()) {
|
||||
BytesReference source = request.source() != null ? request.source() : request.templateSource();
|
||||
logger.trace("[{}] running query for [{}] [{}]", ctx.id(), ctx.watch().id(), XContentHelper.convertToJson(source, false, true));
|
||||
ToXContent source = request.source() != null ? request.source() : request.template();
|
||||
logger.trace("[{}] running query for [{}] [{}]", ctx.id(), ctx.watch().id(), XContentHelper.toString(source));
|
||||
}
|
||||
|
||||
SearchResponse response = client.search(request, timeout);
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.elasticsearch.common.ParseFieldMatcher;
|
|||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.watcher.input.Input;
|
||||
import org.elasticsearch.watcher.support.SearchRequestEquivalence;
|
||||
import org.elasticsearch.watcher.support.WatcherDateTimeUtils;
|
||||
|
@ -109,7 +110,7 @@ public class SearchInput implements Input {
|
|||
return builder;
|
||||
}
|
||||
|
||||
public static SearchInput parse(String watchId, XContentParser parser) throws IOException {
|
||||
public static SearchInput parse(String watchId, XContentParser parser, QueryParseContext context) throws IOException {
|
||||
SearchRequest request = null;
|
||||
Set<String> extract = null;
|
||||
TimeValue timeout = null;
|
||||
|
@ -122,7 +123,7 @@ public class SearchInput implements Input {
|
|||
currentFieldName = parser.currentName();
|
||||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.REQUEST)) {
|
||||
try {
|
||||
request = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE);
|
||||
request = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE, context);
|
||||
} catch (ElasticsearchParseException srpe) {
|
||||
throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. failed to parse [{}]", srpe, TYPE, watchId, currentFieldName);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ import org.elasticsearch.common.logging.Loggers;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.watcher.input.InputFactory;
|
||||
import org.elasticsearch.watcher.input.simple.ExecutableSimpleInput;
|
||||
import org.elasticsearch.watcher.support.init.proxy.ClientProxy;
|
||||
|
@ -23,11 +25,13 @@ public class SearchInputFactory extends InputFactory<SearchInput, SearchInput.Re
|
|||
|
||||
private final ClientProxy client;
|
||||
private final TimeValue defaultTimeout;
|
||||
private IndicesQueriesRegistry queryRegistry;
|
||||
|
||||
@Inject
|
||||
public SearchInputFactory(Settings settings, ClientProxy client) {
|
||||
public SearchInputFactory(Settings settings, ClientProxy client, IndicesQueriesRegistry queryRegistry) {
|
||||
super(Loggers.getLogger(ExecutableSimpleInput.class, settings));
|
||||
this.client = client;
|
||||
this.queryRegistry = queryRegistry;
|
||||
this.defaultTimeout = settings.getAsTime("watcher.input.search.default_timeout", null);
|
||||
}
|
||||
|
||||
|
@ -38,7 +42,9 @@ public class SearchInputFactory extends InputFactory<SearchInput, SearchInput.Re
|
|||
|
||||
@Override
|
||||
public SearchInput parseInput(String watchId, XContentParser parser) throws IOException {
|
||||
return SearchInput.parse(watchId, parser);
|
||||
QueryParseContext context = new QueryParseContext(queryRegistry);
|
||||
context.reset(parser);
|
||||
return SearchInput.parse(watchId, parser, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,22 +9,29 @@ import org.elasticsearch.ElasticsearchParseException;
|
|||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.ParseFieldMatcher;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.*;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.Template;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.watcher.execution.WatchExecutionContext;
|
||||
import org.elasticsearch.watcher.support.text.TextTemplate;
|
||||
import org.elasticsearch.watcher.watch.Payload;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.watcher.support.WatcherDateTimeUtils.formatDate;
|
||||
|
@ -64,62 +71,67 @@ public final class WatcherUtils {
|
|||
// Due the inconsistency with templates in ES 1.x, we maintain our own template format.
|
||||
// This template format we use now, will become the template structure in ES 2.0
|
||||
Map<String, Object> watcherContextParams = Variables.createCtxModel(ctx, payload);
|
||||
if (Strings.hasLength(requestPrototype.source())) {
|
||||
if (requestPrototype.source() != null) {
|
||||
// Here we convert a watch search request body into an inline search template,
|
||||
// this way if any Watcher related context variables are used, they will get resolved,
|
||||
// by ES search template support
|
||||
XContentBuilder builder = jsonBuilder();
|
||||
builder.startObject();
|
||||
XContentHelper.writeRawField("template", requestPrototype.source(), builder, ToXContent.EMPTY_PARAMS);
|
||||
builder.field("params", watcherContextParams);
|
||||
builder.endObject();
|
||||
requestPrototype.source().toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||
Template template = new Template(builder.string(), ScriptType.INLINE, null, builder.contentType(), watcherContextParams);
|
||||
// Unfortunately because of SearchRequest#templateSource(BytesReference, boolean) has been removed in 1.6 and
|
||||
// SearchRequest#templateSource(BytesReference) doesn't exist in 1.5, we are forced to use SearchRequest#templateSource(String)
|
||||
// that exist in both 1.5 and 1.6
|
||||
// TODO (2.0 upgrade): move back to BytesReference
|
||||
request.templateSource(builder.string());
|
||||
} else if (Strings.hasLength(requestPrototype.templateSource())) {
|
||||
request.template(template);
|
||||
} else if (requestPrototype.template() != null) {
|
||||
// Here we convert watcher template into a ES core templates. Due to the different format we use, we
|
||||
// convert to the template format used in ES core
|
||||
BytesReference templateSource = requestPrototype.templateSource();
|
||||
try (XContentParser sourceParser = XContentFactory.xContent(templateSource).createParser(templateSource)) {
|
||||
sourceParser.nextToken();
|
||||
TextTemplate template = TextTemplate.parse(sourceParser);
|
||||
// BytesReference templateSource =
|
||||
// requestPrototype.templateSource();
|
||||
// try (XContentParser sourceParser =
|
||||
// XContentFactory.xContent(templateSource).createParser(templateSource))
|
||||
// {
|
||||
// sourceParser.nextToken();
|
||||
// TextTemplate template = TextTemplate.parse(sourceParser);
|
||||
|
||||
// Convert to the ES template format:
|
||||
XContentBuilder builder = jsonBuilder();
|
||||
builder.startObject();
|
||||
switch (template.getType()) {
|
||||
case INDEXED:
|
||||
builder.startObject("template");
|
||||
builder.field("id", template.getTemplate());
|
||||
builder.endObject();
|
||||
break;
|
||||
case FILE:
|
||||
builder.startObject("template");
|
||||
builder.field("file", template.getTemplate());
|
||||
builder.endObject();
|
||||
break;
|
||||
case INLINE:
|
||||
XContentHelper.writeRawField("template", new BytesArray(template.getTemplate()), builder, ToXContent.EMPTY_PARAMS);
|
||||
break;
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.putAll(watcherContextParams);
|
||||
params.putAll(template.getParams());
|
||||
builder.field("params", params);
|
||||
builder.endObject();
|
||||
// XContentBuilder builder = jsonBuilder();
|
||||
// builder.startObject();
|
||||
// switch (template.getType()) {
|
||||
// case INDEXED:
|
||||
// builder.startObject("template");
|
||||
// builder.field("id", template.getTemplate());
|
||||
// builder.endObject();
|
||||
// break;
|
||||
// case FILE:
|
||||
// builder.startObject("template");
|
||||
// builder.field("file", template.getTemplate());
|
||||
// builder.endObject();
|
||||
// break;
|
||||
// case INLINE:
|
||||
// XContentHelper.writeRawField("template", new
|
||||
// BytesArray(template.getTemplate()), builder,
|
||||
// ToXContent.EMPTY_PARAMS);
|
||||
// break;
|
||||
// }
|
||||
// Map<String, Object> params = new HashMap<>();
|
||||
// params.putAll(watcherContextParams);
|
||||
// params.putAll(template.getParams());
|
||||
// builder.field("params", params);
|
||||
// builder.endObject();
|
||||
// Unfortunately because of SearchRequest#templateSource(BytesReference, boolean) has been removed in 1.6 and
|
||||
// SearchRequest#templateSource(BytesReference) doesn't exist in 1.5, we are forced to use SearchRequest#templateSource(String)
|
||||
// that exist in both 1.5 and 1.6
|
||||
// TODO (2.0 upgrade): move back to BytesReference
|
||||
request.templateSource(builder.string());
|
||||
}
|
||||
} else if (requestPrototype.templateName() != null) {
|
||||
// In Watcher templates on all places can be defined in one format
|
||||
// Can only be set via the Java api
|
||||
throw Exceptions.illegalArgument("SearchRequest's templateName isn't supported, templates should be defined in the request body");
|
||||
request.template(requestPrototype.template());
|
||||
// }
|
||||
}
|
||||
// else if (requestPrototype.templateName() != null) {
|
||||
// // In Watcher templates on all places can be defined in one format
|
||||
// // Can only be set via the Java api
|
||||
// throw
|
||||
// Exceptions.illegalArgument("SearchRequest's templateName isn't supported, templates should be defined in the request body");
|
||||
// }
|
||||
// falling back to an empty body
|
||||
return request;
|
||||
}
|
||||
|
@ -128,9 +140,10 @@ public final class WatcherUtils {
|
|||
/**
|
||||
* Reads a new search request instance for the specified parser.
|
||||
*/
|
||||
public static SearchRequest readSearchRequest(XContentParser parser, SearchType searchType) throws IOException {
|
||||
BytesReference searchBody = null;
|
||||
String templateBody = null;
|
||||
public static SearchRequest readSearchRequest(XContentParser parser, SearchType searchType, QueryParseContext context)
|
||||
throws IOException {
|
||||
// BytesReference searchBody = null;
|
||||
// String templateBody = null;
|
||||
IndicesOptions indicesOptions = DEFAULT_INDICES_OPTIONS;
|
||||
SearchRequest searchRequest = new SearchRequest();
|
||||
|
||||
|
@ -165,9 +178,11 @@ public final class WatcherUtils {
|
|||
}
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if (ParseFieldMatcher.STRICT.match(currentFieldName, BODY_FIELD)) {
|
||||
XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent());
|
||||
builder.copyCurrentStructure(parser);
|
||||
searchBody = builder.bytes();
|
||||
// XContentBuilder builder =
|
||||
// XContentBuilder.builder(parser.contentType().xContent());
|
||||
// builder.copyCurrentStructure(parser);
|
||||
// searchBody = builder.bytes();
|
||||
searchRequest.source(SearchSourceBuilder.parseSearchSource(parser, context));
|
||||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_OPTIONS_FIELD)) {
|
||||
boolean expandOpen = DEFAULT_INDICES_OPTIONS.expandWildcardsOpen();
|
||||
boolean expandClosed = DEFAULT_INDICES_OPTIONS.expandWildcardsClosed();
|
||||
|
@ -213,7 +228,8 @@ public final class WatcherUtils {
|
|||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, TEMPLATE_FIELD)) {
|
||||
XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent());
|
||||
builder.copyCurrentStructure(parser);
|
||||
templateBody = builder.string();
|
||||
String templateBody = builder.string();
|
||||
searchRequest.template(new Template(templateBody, ScriptType.INLINE, null, builder.contentType(), null));
|
||||
} else {
|
||||
throw new ElasticsearchParseException("could not read search request. unexpected object field [" + currentFieldName + "]");
|
||||
}
|
||||
|
@ -239,17 +255,20 @@ public final class WatcherUtils {
|
|||
}
|
||||
searchRequest.searchType(searchType);
|
||||
searchRequest.indicesOptions(indicesOptions);
|
||||
if (searchBody != null) {
|
||||
assert searchBody.hasArray();
|
||||
searchRequest.source(searchBody);
|
||||
}
|
||||
if (templateBody != null) {
|
||||
// Unfortunately because of SearchRequest#templateSource(BytesReference, boolean) has been removed in 1.6 and
|
||||
// SearchRequest#templateSource(BytesReference) doesn't exist in 1.5, we are forced to use SearchRequest#templateSource(String)
|
||||
// that exist in both 1.5 and 1.6
|
||||
// TODO (2.0 upgrade): move back to BytesReference
|
||||
searchRequest.templateSource(templateBody);
|
||||
}
|
||||
// if (searchBody != null) {
|
||||
// assert searchBody.hasArray();
|
||||
// searchRequest.source(searchBody);
|
||||
// }
|
||||
// if (templateBody != null) {
|
||||
// // Unfortunately because of
|
||||
// SearchRequest#templateSource(BytesReference, boolean) has been
|
||||
// removed in 1.6 and
|
||||
// // SearchRequest#templateSource(BytesReference) doesn't exist in 1.5,
|
||||
// we are forced to use SearchRequest#templateSource(String)
|
||||
// // that exist in both 1.5 and 1.6
|
||||
// // TODO (2.0 upgrade): move back to BytesReference
|
||||
// searchRequest.templateSource(templateBody);
|
||||
// }
|
||||
return searchRequest;
|
||||
}
|
||||
|
||||
|
@ -272,11 +291,11 @@ public final class WatcherUtils {
|
|||
if (searchRequest.types() != null) {
|
||||
builder.array(TYPES_FIELD.getPreferredName(), searchRequest.types());
|
||||
}
|
||||
if (Strings.hasLength(searchRequest.source())) {
|
||||
XContentHelper.writeRawField(BODY_FIELD.getPreferredName(), searchRequest.source(), builder, params);
|
||||
if (searchRequest.source() != null) {
|
||||
builder.field(BODY_FIELD.getPreferredName(), searchRequest.source());
|
||||
}
|
||||
if (Strings.hasLength(searchRequest.templateSource())) {
|
||||
XContentHelper.writeRawField(TEMPLATE_FIELD.getPreferredName(), searchRequest.templateSource(), builder, params);
|
||||
if (searchRequest.template() != null) {
|
||||
builder.field(TEMPLATE_FIELD.getPreferredName(), searchRequest.template());
|
||||
}
|
||||
|
||||
if (searchRequest.indicesOptions() != DEFAULT_INDICES_OPTIONS) {
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.elasticsearch.common.ParseFieldMatcher;
|
|||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.watcher.support.SearchRequestEquivalence;
|
||||
import org.elasticsearch.watcher.support.WatcherDateTimeUtils;
|
||||
import org.elasticsearch.watcher.support.WatcherUtils;
|
||||
|
@ -91,7 +92,7 @@ public class SearchTransform implements Transform {
|
|||
return builder;
|
||||
}
|
||||
|
||||
public static SearchTransform parse(String watchId, XContentParser parser) throws IOException {
|
||||
public static SearchTransform parse(String watchId, XContentParser parser, QueryParseContext context) throws IOException {
|
||||
SearchRequest request = null;
|
||||
TimeValue timeout = null;
|
||||
DateTimeZone dynamicNameTimeZone = null;
|
||||
|
@ -103,7 +104,7 @@ public class SearchTransform implements Transform {
|
|||
currentFieldName = parser.currentName();
|
||||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.REQUEST)) {
|
||||
try {
|
||||
request = WatcherUtils.readSearchRequest(parser, ExecutableSearchTransform.DEFAULT_SEARCH_TYPE);
|
||||
request = WatcherUtils.readSearchRequest(parser, ExecutableSearchTransform.DEFAULT_SEARCH_TYPE, context);
|
||||
} catch (ElasticsearchParseException srpe) {
|
||||
throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse [{}]", srpe, TYPE, watchId, currentFieldName);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ import org.elasticsearch.common.logging.Loggers;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.watcher.support.init.proxy.ClientProxy;
|
||||
import org.elasticsearch.watcher.transform.TransformFactory;
|
||||
|
||||
|
@ -22,11 +24,13 @@ public class SearchTransformFactory extends TransformFactory<SearchTransform, Se
|
|||
|
||||
protected final ClientProxy client;
|
||||
private final TimeValue defaultTimeout;
|
||||
private IndicesQueriesRegistry queryRegistry;
|
||||
|
||||
@Inject
|
||||
public SearchTransformFactory(Settings settings, ClientProxy client) {
|
||||
public SearchTransformFactory(Settings settings, ClientProxy client, IndicesQueriesRegistry queryRegistry) {
|
||||
super(Loggers.getLogger(ExecutableSearchTransform.class, settings));
|
||||
this.client = client;
|
||||
this.queryRegistry = queryRegistry;
|
||||
this.defaultTimeout = settings.getAsTime("watcher.transform.search.default_timeout", null);
|
||||
}
|
||||
|
||||
|
@ -37,7 +41,9 @@ public class SearchTransformFactory extends TransformFactory<SearchTransform, Se
|
|||
|
||||
@Override
|
||||
public SearchTransform parseTransform(String watchId, XContentParser parser) throws IOException {
|
||||
return SearchTransform.parse(watchId, parser);
|
||||
QueryParseContext context = new QueryParseContext(queryRegistry);
|
||||
context.reset(parser);
|
||||
return SearchTransform.parse(watchId, parser, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -98,8 +98,7 @@ public class IndexActionTests extends ESIntegTestCase {
|
|||
.setTypes("test-type")
|
||||
.setSource(searchSource()
|
||||
.query(matchAllQuery())
|
||||
.aggregation(terms("timestamps").field(customTimestampField ? timestampField : "_timestamp"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("timestamps").field(customTimestampField ? timestampField : "_timestamp")))
|
||||
.get();
|
||||
|
||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||
|
@ -165,8 +164,7 @@ public class IndexActionTests extends ESIntegTestCase {
|
|||
.addSort("foo", SortOrder.ASC)
|
||||
.setSource(searchSource()
|
||||
.query(matchAllQuery())
|
||||
.aggregation(terms("timestamps").field(customTimestampField ? timestampField : "_timestamp"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("timestamps").field(customTimestampField ? timestampField : "_timestamp")))
|
||||
.get();
|
||||
|
||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||
|
|
|
@ -103,8 +103,7 @@ public class HistoryTemplateEmailMappingsTests extends AbstractWatcherIntegratio
|
|||
.aggregation(terms("to").field("result.actions.email.message.to"))
|
||||
.aggregation(terms("cc").field("result.actions.email.message.cc"))
|
||||
.aggregation(terms("bcc").field("result.actions.email.message.bcc"))
|
||||
.aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("reply_to").field("result.actions.email.message.reply_to")))
|
||||
.get();
|
||||
|
||||
assertThat(response, notNullValue());
|
||||
|
|
|
@ -99,8 +99,7 @@ public class HistoryTemplateHttpMappingsTests extends AbstractWatcherIntegration
|
|||
SearchResponse response = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*").setSource(searchSource()
|
||||
.aggregation(terms("input_result_path").field("result.input.http.request.path"))
|
||||
.aggregation(terms("input_result_host").field("result.input.http.request.host"))
|
||||
.aggregation(terms("webhook_path").field("result.actions.webhook.request.path"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("webhook_path").field("result.actions.webhook.request.path")))
|
||||
.get();
|
||||
|
||||
assertThat(response, notNullValue());
|
||||
|
|
|
@ -60,8 +60,7 @@ public class HistoryTemplateIndexActionMappingsTests extends AbstractWatcherInte
|
|||
|
||||
SearchResponse response = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*").setSource(searchSource()
|
||||
.aggregation(terms("index_action_indices").field("result.actions.index.response.index"))
|
||||
.aggregation(terms("index_action_types").field("result.actions.index.response.type"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("index_action_types").field("result.actions.index.response.type")))
|
||||
.get();
|
||||
|
||||
assertThat(response, notNullValue());
|
||||
|
|
|
@ -72,8 +72,7 @@ public class HistoryTemplateSearchInputMappingsTests extends AbstractWatcherInte
|
|||
.aggregation(terms("input_search_type").field("result.input.search.request.search_type"))
|
||||
.aggregation(terms("input_indices").field("result.input.search.request.indices"))
|
||||
.aggregation(terms("input_types").field("result.input.search.request.types"))
|
||||
.aggregation(terms("input_body").field("result.input.search.request.body"))
|
||||
.buildAsBytes())
|
||||
.aggregation(terms("input_body").field("result.input.search.request.body")))
|
||||
.get();
|
||||
|
||||
assertThat(response, notNullValue());
|
||||
|
|
|
@ -8,14 +8,17 @@ package org.elasticsearch.watcher.input.search;
|
|||
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.Template;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
|
||||
|
@ -55,11 +58,10 @@ import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
|||
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.areJsonEquivalent;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.getRandomSupportedSearchType;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
/**
|
||||
|
@ -140,26 +142,36 @@ public class SearchInputTests extends ESIntegTestCase {
|
|||
public void testSearch_InlineTemplate() throws Exception {
|
||||
WatchExecutionContext ctx = createContext();
|
||||
|
||||
final String expectedQuery = "{\"template\":{\"query\":{\"filtered\":{\"query\":{\"match\":{\"event_type\":{\"query\":\"a\"," +
|
||||
final String expectedTemplateString = "{\"query\":{\"filtered\":{\"query\":{\"match\":{\"event_type\":{\"query\":\"a\","
|
||||
+
|
||||
"\"type\":\"boolean\"}}},\"filter\":{\"range\":{\"_timestamp\":" +
|
||||
"{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\"," +
|
||||
"\"include_lower\":true,\"include_upper\":true}}}}}},\"params\":{\"seconds_param\":\"30s\",\"ctx\":{\"id\":\"" + ctx.id().value() + "\",\"metadata\":null,\"vars\":{},\"watch_id\":\"test-watch\",\"trigger\":{\"triggered_time\":\"1970-01-01T00:01:00.000Z\",\"scheduled_time\":\"1970-01-01T00:01:00.000Z\"},\"execution_time\":\"1970-01-01T00:01:00.000Z\"}}}";
|
||||
"\"include_lower\":true,\"include_upper\":true}}}}}}";
|
||||
|
||||
Map<String, Object> triggerParams = new HashMap<String, Object>();
|
||||
triggerParams.put("triggered_time", "1970-01-01T00:01:00.000Z");
|
||||
triggerParams.put("scheduled_time", "1970-01-01T00:01:00.000Z");
|
||||
Map<String, Object> ctxParams = new HashMap<String, Object>();
|
||||
ctxParams.put("id", ctx.id().value());
|
||||
ctxParams.put("metadata", null);
|
||||
ctxParams.put("vars", new HashMap<String, Object>());
|
||||
ctxParams.put("watch_id", "test-watch");
|
||||
ctxParams.put("trigger", triggerParams);
|
||||
ctxParams.put("execution_time", "1970-01-01T00:01:00.000Z");
|
||||
Map<String, Object> expectedParams = new HashMap<String, Object>();
|
||||
expectedParams.put("seconds_param", "30s");
|
||||
expectedParams.put("ctx", ctxParams);
|
||||
Template expectedTemplate = new Template(expectedTemplateString, ScriptType.INLINE, null, XContentType.JSON, expectedParams);
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("seconds_param", "30s");
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.inline(TEMPLATE_QUERY).params(params).build())
|
||||
.bytes();
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.request();
|
||||
Template template = new Template(TEMPLATE_QUERY, ScriptType.INLINE, null, XContentType.JSON, params);
|
||||
|
||||
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index").setTemplate(template).request();
|
||||
|
||||
SearchInput.Result executedResult = executeSearchInput(request, ctx);
|
||||
assertThat(areJsonEquivalent(executedResult.executedRequest().templateSource().toUtf8(), expectedQuery), is(true));
|
||||
assertThat(executedResult.executedRequest().template(), equalTo(expectedTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -172,18 +184,17 @@ public class SearchInputTests extends ESIntegTestCase {
|
|||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("seconds_param", "30s");
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.indexed("test-template").params(params).build())
|
||||
.bytes();
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.request();
|
||||
Template template = new Template("test-template", ScriptType.INDEXED, null, null, params);
|
||||
|
||||
jsonBuilder().value(TextTemplate.indexed("test-template").params(params).build()).bytes();
|
||||
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index").setTemplate(template).request();
|
||||
|
||||
SearchInput.Result executedResult = executeSearchInput(request, ctx);
|
||||
assertThat(executedResult.executedRequest().templateSource().toUtf8(), startsWith("{\"template\":{\"id\":\"test-template\""));
|
||||
Template resultTemplate = executedResult.executedRequest().template();
|
||||
assertThat(resultTemplate, notNullValue());
|
||||
assertThat(resultTemplate.getScript(), equalTo("test-template"));
|
||||
assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -193,18 +204,15 @@ public class SearchInputTests extends ESIntegTestCase {
|
|||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("seconds_param", "30s");
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.file("test_disk_template").params(params).build())
|
||||
.bytes();
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.request();
|
||||
Template template = new Template("test_disk_template", ScriptType.FILE, null, null, params);
|
||||
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchInput.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index").setTemplate(template).request();
|
||||
|
||||
SearchInput.Result executedResult = executeSearchInput(request, ctx);
|
||||
assertThat(executedResult.executedRequest().templateSource().toUtf8(), startsWith("{\"template\":{\"file\":\"test_disk_template\""));
|
||||
Template resultTemplate = executedResult.executedRequest().template();
|
||||
assertThat(resultTemplate, notNullValue());
|
||||
assertThat(resultTemplate.getScript(), equalTo("test_disk_template"));
|
||||
assertThat(resultTemplate.getType(), equalTo(ScriptType.FILE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -256,7 +264,8 @@ public class SearchInputTests extends ESIntegTestCase {
|
|||
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
||||
parser.nextToken();
|
||||
|
||||
SearchInputFactory factory = new SearchInputFactory(Settings.EMPTY, ClientProxy.of(client()));
|
||||
IndicesQueriesRegistry indicesQueryRegistry = internalCluster().getInstance(IndicesQueriesRegistry.class);
|
||||
SearchInputFactory factory = new SearchInputFactory(Settings.EMPTY, ClientProxy.of(client()), indicesQueryRegistry);
|
||||
|
||||
SearchInput searchInput = factory.parseInput("_id", parser);
|
||||
assertEquals(SearchInput.TYPE, searchInput.type());
|
||||
|
|
|
@ -11,13 +11,19 @@ import org.elasticsearch.action.search.SearchType;
|
|||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.MatchAllQueryParser;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.script.Template;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.watcher.input.search.ExecutableSearchInput;
|
||||
|
@ -26,10 +32,7 @@ import org.elasticsearch.watcher.support.text.TextTemplate;
|
|||
import org.joda.time.DateTime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
|
@ -103,10 +106,7 @@ public class WatcherUtilsTests extends ESTestCase {
|
|||
expectedRequest.searchType(getRandomSupportedSearchType());
|
||||
|
||||
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery()).size(11);
|
||||
XContentBuilder searchSourceJsonBuilder = jsonBuilder();
|
||||
searchSourceBuilder.toXContent(searchSourceJsonBuilder, ToXContent.EMPTY_PARAMS);
|
||||
BytesReference expectedSource = searchSourceJsonBuilder.bytes();
|
||||
expectedRequest.source(expectedSource);
|
||||
expectedRequest.source(searchSourceBuilder);
|
||||
|
||||
if (randomBoolean()) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
|
@ -122,21 +122,24 @@ public class WatcherUtilsTests extends ESTestCase {
|
|||
TextTemplate.file(text).params(params).build(),
|
||||
TextTemplate.indexed(text).params(params).build()
|
||||
);
|
||||
expectedRequest.templateSource(jsonBuilder().startObject().field("template", template).endObject().string());
|
||||
expectedRequest.template(new Template(template.getTemplate(), template.getType(), null, template.getContentType(), template.getParams()));
|
||||
}
|
||||
|
||||
XContentBuilder builder = jsonBuilder();
|
||||
builder = WatcherUtils.writeSearchRequest(expectedRequest, builder, ToXContent.EMPTY_PARAMS);
|
||||
XContentParser parser = XContentHelper.createParser(builder.bytes());
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
|
||||
SearchRequest result = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE);
|
||||
IndicesQueriesRegistry registry = new IndicesQueriesRegistry(Settings.EMPTY, Collections.singleton(new MatchAllQueryParser()), new NamedWriteableRegistry());
|
||||
QueryParseContext context = new QueryParseContext(registry);
|
||||
context.reset(parser);
|
||||
SearchRequest result = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE, context);
|
||||
|
||||
assertThat(result.indices(), arrayContainingInAnyOrder(expectedRequest.indices()));
|
||||
assertThat(result.types(), arrayContainingInAnyOrder(expectedRequest.types()));
|
||||
assertThat(result.indicesOptions(), equalTo(expectedRequest.indicesOptions()));
|
||||
assertThat(result.searchType(), equalTo(expectedRequest.searchType()));
|
||||
assertThat(result.source().toUtf8(), equalTo(expectedSource.toUtf8()));
|
||||
assertThat(result.templateSource(), equalTo(expectedRequest.templateSource()));
|
||||
assertThat(result.source(), equalTo(searchSourceBuilder));
|
||||
assertThat(result.template(), equalTo(expectedRequest.template()));
|
||||
}
|
||||
|
||||
public void testDeserializeSearchRequest() throws Exception {
|
||||
|
@ -190,8 +193,7 @@ public class WatcherUtilsTests extends ESTestCase {
|
|||
source = searchSourceBuilder.buildAsBytes(XContentType.JSON);
|
||||
builder.rawField("body", source);
|
||||
}
|
||||
|
||||
BytesReference templateSource = null;
|
||||
Template templateSource = null;
|
||||
if (randomBoolean()) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
if (randomBoolean()) {
|
||||
|
@ -207,19 +209,22 @@ public class WatcherUtilsTests extends ESTestCase {
|
|||
TextTemplate.indexed(text).params(params).build()
|
||||
);
|
||||
builder.field("template", template);
|
||||
templateSource = jsonBuilder().value(template).bytes();
|
||||
templateSource = new Template(template.getTemplate(), template.getType(), null, template.getContentType(), template.getParams());
|
||||
}
|
||||
|
||||
XContentParser parser = XContentHelper.createParser(builder.bytes());
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
|
||||
SearchRequest result = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE);
|
||||
IndicesQueriesRegistry registry = new IndicesQueriesRegistry(Settings.EMPTY, Collections.singleton(new MatchAllQueryParser()), new NamedWriteableRegistry());
|
||||
QueryParseContext context = new QueryParseContext(registry);
|
||||
context.reset(parser);
|
||||
SearchRequest result = WatcherUtils.readSearchRequest(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE, context);
|
||||
|
||||
assertThat(result.indices(), arrayContainingInAnyOrder(indices));
|
||||
assertThat(result.types(), arrayContainingInAnyOrder(types));
|
||||
assertThat(result.indicesOptions(), equalTo(indicesOptions));
|
||||
assertThat(result.searchType(), equalTo(searchType));
|
||||
assertThat(result.source(), equalTo(source));
|
||||
assertThat(result.templateSource(), equalTo(templateSource));
|
||||
assertThat(result.template(), equalTo(templateSource));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,8 +32,15 @@ import org.elasticsearch.shield.crypto.InternalCryptoService;
|
|||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
|
||||
import org.elasticsearch.test.TestCluster;
|
||||
import org.elasticsearch.watcher.*;
|
||||
import org.elasticsearch.watcher.actions.email.service.*;
|
||||
import org.elasticsearch.watcher.WatcherLifeCycleService;
|
||||
import org.elasticsearch.watcher.WatcherModule;
|
||||
import org.elasticsearch.watcher.WatcherPlugin;
|
||||
import org.elasticsearch.watcher.WatcherService;
|
||||
import org.elasticsearch.watcher.WatcherState;
|
||||
import org.elasticsearch.watcher.actions.email.service.Authentication;
|
||||
import org.elasticsearch.watcher.actions.email.service.Email;
|
||||
import org.elasticsearch.watcher.actions.email.service.EmailService;
|
||||
import org.elasticsearch.watcher.actions.email.service.Profile;
|
||||
import org.elasticsearch.watcher.client.WatcherClient;
|
||||
import org.elasticsearch.watcher.execution.ExecutionService;
|
||||
import org.elasticsearch.watcher.execution.ExecutionState;
|
||||
|
@ -60,14 +67,25 @@ import java.io.OutputStream;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
|
||||
import static org.elasticsearch.watcher.WatcherModule.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.elasticsearch.watcher.WatcherModule.HISTORY_TEMPLATE_NAME;
|
||||
import static org.elasticsearch.watcher.WatcherModule.TRIGGERED_TEMPLATE_NAME;
|
||||
import static org.elasticsearch.watcher.WatcherModule.WATCHES_TEMPLATE_NAME;
|
||||
import static org.hamcrest.Matchers.emptyArray;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
|
@ -258,7 +276,7 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
|
|||
|
||||
protected long docCount(String index, String type, SearchSourceBuilder source) {
|
||||
SearchRequestBuilder builder = client().prepareSearch(index)
|
||||
.internalBuilder(source).setSize(0);
|
||||
.setSource(source).setSize(0);
|
||||
if (type != null) {
|
||||
builder.setTypes(type);
|
||||
}
|
||||
|
@ -266,7 +284,7 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
|
|||
}
|
||||
|
||||
protected SearchResponse searchHistory(SearchSourceBuilder builder) {
|
||||
return client().prepareSearch(HistoryStore.INDEX_PREFIX + "*").setSource(builder.buildAsBytes()).get();
|
||||
return client().prepareSearch(HistoryStore.INDEX_PREFIX + "*").setSource(builder).get();
|
||||
}
|
||||
|
||||
protected <T> T getInstanceFromMaster(Class<T> type) {
|
||||
|
|
|
@ -117,7 +117,7 @@ public final class WatcherTestUtils {
|
|||
|
||||
public static SearchRequest matchAllRequest(IndicesOptions indicesOptions) {
|
||||
SearchRequest request = new SearchRequest(Strings.EMPTY_ARRAY)
|
||||
.source(SearchSourceBuilder.searchSource().query(matchAllQuery()).buildAsBytes(XContentType.JSON));
|
||||
.source(SearchSourceBuilder.searchSource().query(matchAllQuery()));
|
||||
if (indicesOptions != null) {
|
||||
request.indicesOptions(indicesOptions);
|
||||
}
|
||||
|
|
|
@ -10,11 +10,12 @@ import org.elasticsearch.ElasticsearchParseException;
|
|||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.Template;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.junit.annotations.TestLogging;
|
||||
import org.elasticsearch.watcher.client.WatchSourceBuilder;
|
||||
|
@ -22,7 +23,6 @@ import org.elasticsearch.watcher.client.WatcherClient;
|
|||
import org.elasticsearch.watcher.condition.ConditionBuilders;
|
||||
import org.elasticsearch.watcher.support.WatcherUtils;
|
||||
import org.elasticsearch.watcher.support.clock.SystemClock;
|
||||
import org.elasticsearch.watcher.support.text.TextTemplate;
|
||||
import org.elasticsearch.watcher.support.xcontent.XContentSource;
|
||||
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
|
||||
import org.elasticsearch.watcher.transport.actions.delete.DeleteWatchResponse;
|
||||
|
@ -36,7 +36,11 @@ import org.elasticsearch.watcher.watch.WatchStore;
|
|||
import org.junit.Test;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
|
@ -50,8 +54,14 @@ import static org.elasticsearch.watcher.input.InputBuilders.simpleInput;
|
|||
import static org.elasticsearch.watcher.test.WatcherTestUtils.newInputSearchRequest;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentSource;
|
||||
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.daily;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.hourly;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.interval;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.monthly;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.weekly;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
*/
|
||||
|
@ -312,12 +322,10 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
|
|||
.get();
|
||||
refresh();
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.indexed("my-template").build())
|
||||
.bytes();
|
||||
Template template = new Template("my-template", ScriptType.INDEXED, null, null, null);
|
||||
SearchRequest searchRequest = newInputSearchRequest("events");
|
||||
// TODO (2.0 upgrade): move back to BytesReference instead of coverting to a string
|
||||
searchRequest.templateSource(templateSource.toUtf8());
|
||||
searchRequest.template(template);
|
||||
testConditionSearch(searchRequest);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,13 @@ import org.elasticsearch.common.settings.Settings;
|
|||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.Template;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
|
||||
|
@ -63,7 +68,6 @@ import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
|||
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
|
||||
import static org.elasticsearch.watcher.support.WatcherDateTimeUtils.parseDate;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.EMPTY_PAYLOAD;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.areJsonEquivalent;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.getRandomSupportedSearchType;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.simplePayload;
|
||||
|
@ -72,7 +76,6 @@ import static org.hamcrest.Matchers.containsString;
|
|||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
/**
|
||||
|
@ -131,11 +134,7 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
ensureGreen("idx");
|
||||
refresh();
|
||||
|
||||
SearchRequest request = Requests.searchRequest("idx").source(jsonBuilder().startObject()
|
||||
.startObject("query")
|
||||
.startObject("match_all").endObject()
|
||||
.endObject()
|
||||
.endObject().bytes());
|
||||
SearchRequest request = Requests.searchRequest("idx").source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
|
||||
SearchTransform searchTransform = TransformBuilders.searchTransform(request).build();
|
||||
ExecutableSearchTransform transform = new ExecutableSearchTransform(searchTransform, logger, ClientProxy.of(client()), null);
|
||||
|
||||
|
@ -168,11 +167,9 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
refresh();
|
||||
|
||||
// create a bad request
|
||||
SearchRequest request = Requests.searchRequest("idx").source(jsonBuilder().startObject()
|
||||
.startObject("query")
|
||||
.startObject("_unknown_query_").endObject()
|
||||
.endObject()
|
||||
.endObject().bytes());
|
||||
SearchRequest request = Requests.searchRequest("idx").source(
|
||||
new SearchSourceBuilder().query(QueryBuilders.wrapperQuery(jsonBuilder().startObject().startObject("query")
|
||||
.startObject("_unknown_query_").endObject().endObject().endObject().bytes())));
|
||||
SearchTransform searchTransform = TransformBuilders.searchTransform(request).build();
|
||||
ExecutableSearchTransform transform = new ExecutableSearchTransform(searchTransform, logger, ClientProxy.of(client()), null);
|
||||
|
||||
|
@ -183,7 +180,7 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
assertThat(result.type(), is(SearchTransform.TYPE));
|
||||
assertThat(result.status(), is(Transform.Result.Status.FAILURE));
|
||||
assertThat(result.reason(), notNullValue());
|
||||
assertThat(result.executedRequest().templateSource().toUtf8(), containsString("_unknown_query_"));
|
||||
assertThat(result.executedRequest().template().getScript(), containsString("_unknown_query_"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -293,7 +290,8 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
||||
parser.nextToken();
|
||||
|
||||
SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, ClientProxy.of(client()));
|
||||
IndicesQueriesRegistry indicesQueryRegistry = internalCluster().getInstance(IndicesQueriesRegistry.class);
|
||||
SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, ClientProxy.of(client()), indicesQueryRegistry);
|
||||
ExecutableSearchTransform executable = transformFactory.parseExecutable("_id", parser);
|
||||
|
||||
assertThat(executable, notNullValue());
|
||||
|
@ -306,9 +304,10 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
assertThat(executable.transform().getRequest().searchType(), is(searchType));
|
||||
}
|
||||
if (templateName != null) {
|
||||
assertThat(executable.transform().getRequest().templateSource().toUtf8(), equalTo("{\"file\":\"template1\"}"));
|
||||
assertThat(executable.transform().getRequest().template(),
|
||||
equalTo(new Template("template1", ScriptType.FILE, null, null, null)));
|
||||
}
|
||||
assertThat(executable.transform().getRequest().source().toBytes(), equalTo(source.toBytes()));
|
||||
assertThat(executable.transform().getRequest().source(), equalTo(source));
|
||||
assertThat(executable.transform().getTimeout(), equalTo(readTimeout));
|
||||
}
|
||||
|
||||
|
@ -321,30 +320,37 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
"{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\"," +
|
||||
"\"include_lower\":true,\"include_upper\":true}}}]}}}";
|
||||
|
||||
final String expectedQuery = "{\"template\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"event_type\":{\"query\":\"a\"," +
|
||||
"\"type\":\"boolean\"}}},{\"range\":{\"_timestamp\":" +
|
||||
"{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\"," +
|
||||
"\"include_lower\":true,\"include_upper\":true}}}]}}},\"params\":{\"seconds_param\":\"30s\",\"ctx\":{" +
|
||||
"\"id\":\"" + ctx.id().value() + "\",\"metadata\":null,\"vars\":{},\"watch_id\":\"test-watch\",\"payload\":{}," +
|
||||
"\"trigger\":{\"triggered_time\":\"1970-01-01T00:01:00.000Z\",\"scheduled_time\":\"1970-01-01T00:01:00.000Z\"}," +
|
||||
"\"execution_time\":\"1970-01-01T00:01:00.000Z\"}}}";
|
||||
final String expectedTemplateString = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"event_type\":{\"query\":\"a\","
|
||||
+ "\"type\":\"boolean\"}}},{\"range\":{\"_timestamp\":"
|
||||
+ "{\"from\":\"{{ctx.trigger.scheduled_time}}||-{{seconds_param}}\",\"to\":\"{{ctx.trigger.scheduled_time}}\","
|
||||
+ "\"include_lower\":true,\"include_upper\":true}}}]}}}";
|
||||
|
||||
Map<String, Object> triggerParams = new HashMap<String, Object>();
|
||||
triggerParams.put("triggered_time", "1970-01-01T00:01:00.000Z");
|
||||
triggerParams.put("scheduled_time", "1970-01-01T00:01:00.000Z");
|
||||
Map<String, Object> ctxParams = new HashMap<String, Object>();
|
||||
ctxParams.put("id", ctx.id().value());
|
||||
ctxParams.put("metadata", null);
|
||||
ctxParams.put("vars", new HashMap<String, Object>());
|
||||
ctxParams.put("watch_id", "test-watch");
|
||||
ctxParams.put("payload", new HashMap<String, Object>());
|
||||
ctxParams.put("trigger", triggerParams);
|
||||
ctxParams.put("execution_time", "1970-01-01T00:01:00.000Z");
|
||||
Map<String, Object> expectedParams = new HashMap<String, Object>();
|
||||
expectedParams.put("seconds_param", "30s");
|
||||
expectedParams.put("ctx", ctxParams);
|
||||
Template expectedTemplate = new Template(expectedTemplateString, ScriptType.INLINE, null, XContentType.JSON, expectedParams);
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("seconds_param", "30s");
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.inline(templateQuery).params(params).build())
|
||||
.bytes();
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchTransform.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.request();
|
||||
Template template = new Template(templateQuery, ScriptType.INLINE, null, XContentType.JSON, params);
|
||||
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchTransform.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index").setTemplate(template).request();
|
||||
|
||||
SearchTransform.Result executedResult = executeSearchTransform(request, ctx);
|
||||
|
||||
assertThat(areJsonEquivalent(executedResult.executedRequest().templateSource().toUtf8(), expectedQuery), is(true));
|
||||
assertThat(executedResult.executedRequest().template(), equalTo(expectedTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -365,16 +371,21 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.indexed("test-script").params(params).build())
|
||||
.bytes();
|
||||
Template template = new Template("test-script", ScriptType.INDEXED, null, null, null);
|
||||
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchTransform.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.setTemplate(template)
|
||||
.request();
|
||||
|
||||
SearchTransform.Result result = executeSearchTransform(request, ctx);
|
||||
assertNotNull(result.executedRequest());
|
||||
assertThat(result.executedRequest().templateSource().toUtf8(), startsWith("{\"template\":{\"id\":\"test-script\""));
|
||||
Template resultTemplate = result.executedRequest().template();
|
||||
assertThat(resultTemplate, notNullValue());
|
||||
assertThat(resultTemplate.getScript(), equalTo("test-script"));
|
||||
assertThat(resultTemplate.getType(), equalTo(ScriptType.INDEXED));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -384,19 +395,16 @@ public class SearchTransformTests extends ESIntegTestCase {
|
|||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("seconds_param", "30s");
|
||||
|
||||
BytesReference templateSource = jsonBuilder()
|
||||
.value(TextTemplate.file("test_disk_template").params(params).build())
|
||||
.bytes();
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(ExecutableSearchTransform.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index")
|
||||
.setTemplateSource(templateSource)
|
||||
.request();
|
||||
Template template = new Template("test_disk_template", ScriptType.FILE, null, null, null);
|
||||
SearchRequest request = client().prepareSearch().setSearchType(ExecutableSearchTransform.DEFAULT_SEARCH_TYPE)
|
||||
.setIndices("test-search-index").setTemplate(template).request();
|
||||
|
||||
SearchTransform.Result result = executeSearchTransform(request, ctx);
|
||||
assertNotNull(result.executedRequest());
|
||||
assertThat(result.executedRequest().templateSource().toUtf8(), startsWith("{\"template\":{\"file\":\"test_disk_template\""));
|
||||
Template resultTemplate = result.executedRequest().template();
|
||||
assertThat(resultTemplate, notNullValue());
|
||||
assertThat(resultTemplate.getScript(), equalTo("test_disk_template"));
|
||||
assertThat(resultTemplate.getType(), equalTo(ScriptType.FILE));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,12 +7,15 @@ package org.elasticsearch.watcher.watch;
|
|||
|
||||
import org.elasticsearch.ElasticsearchParseException;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.query.MatchAllQueryParser;
|
||||
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.watcher.actions.ActionFactory;
|
||||
import org.elasticsearch.watcher.actions.ActionRegistry;
|
||||
|
@ -347,7 +350,8 @@ public class WatchTests extends ESTestCase {
|
|||
Map<String, InputFactory> parsers = new HashMap<>();
|
||||
switch (input.type()) {
|
||||
case SearchInput.TYPE:
|
||||
parsers.put(SearchInput.TYPE, new SearchInputFactory(settings, client));
|
||||
IndicesQueriesRegistry queryRegistry = new IndicesQueriesRegistry(Settings.EMPTY, Collections.singleton(new MatchAllQueryParser()), new NamedWriteableRegistry());
|
||||
parsers.put(SearchInput.TYPE, new SearchInputFactory(settings, client, queryRegistry));
|
||||
return new InputRegistry(parsers);
|
||||
default:
|
||||
parsers.put(SimpleInput.TYPE, new SimpleInputFactory(settings));
|
||||
|
@ -406,11 +410,12 @@ public class WatchTests extends ESTestCase {
|
|||
}
|
||||
|
||||
private TransformRegistry transformRegistry() {
|
||||
IndicesQueriesRegistry queryRegistry = new IndicesQueriesRegistry(Settings.EMPTY, Collections.singleton(new MatchAllQueryParser()), new NamedWriteableRegistry());
|
||||
Map<String, TransformFactory> factories = new HashMap<>();
|
||||
ChainTransformFactory parser = new ChainTransformFactory();
|
||||
factories.put(ChainTransform.TYPE, parser);
|
||||
factories.put(ScriptTransform.TYPE, new ScriptTransformFactory(settings, scriptService));
|
||||
factories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, client));
|
||||
factories.put(SearchTransform.TYPE, new SearchTransformFactory(settings, client, queryRegistry));
|
||||
TransformRegistry registry = new TransformRegistry(unmodifiableMap(factories));
|
||||
parser.init(registry);
|
||||
return registry;
|
||||
|
|
Loading…
Reference in New Issue