Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove types from WatcherSearchTemplateRequest #51112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.watcher.support.search;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
Expand All @@ -14,7 +13,6 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand All @@ -38,21 +36,15 @@
public class WatcherSearchTemplateRequest implements ToXContentObject {

private final String[] indices;
private final String[] types;
private final SearchType searchType;
private final IndicesOptions indicesOptions;
private final Script template;
private final BytesReference searchSource;
private boolean restTotalHitsAsInt = true;

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(WatcherSearchTemplateRequest.class));
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in a watcher search request is deprecated.";

public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType searchType, IndicesOptions indicesOptions,
public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions,
BytesReference searchSource) {
this.indices = indices;
this.types = types;
this.searchType = searchType;
this.indicesOptions = indicesOptions;
// Here we convert a watch search request body into an inline search template,
Expand All @@ -61,10 +53,9 @@ public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType
this.searchSource = BytesArray.EMPTY;
}

public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType searchType, IndicesOptions indicesOptions,
public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions,
Script template) {
this.indices = indices;
this.types = types;
this.searchType = searchType;
this.indicesOptions = indicesOptions;
this.template = template;
Expand All @@ -73,18 +64,16 @@ public WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType

public WatcherSearchTemplateRequest(WatcherSearchTemplateRequest original, BytesReference source) {
this.indices = original.indices;
this.types = original.types;
this.searchType = original.searchType;
this.indicesOptions = original.indicesOptions;
this.searchSource = source;
this.template = original.template;
this.restTotalHitsAsInt = original.restTotalHitsAsInt;
}

private WatcherSearchTemplateRequest(String[] indices, String[] types, SearchType searchType, IndicesOptions indicesOptions,
private WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions,
BytesReference searchSource, Script template) {
this.indices = indices;
this.types = types;
this.searchType = searchType;
this.indicesOptions = indicesOptions;
this.template = template;
Expand All @@ -100,10 +89,6 @@ public String[] getIndices() {
return indices;
}

public String[] getTypes() {
return types;
}

public SearchType getSearchType() {
return searchType;
}
Expand Down Expand Up @@ -146,9 +131,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (indices != null) {
builder.array(INDICES_FIELD.getPreferredName(), indices);
}
if (types != null) {
builder.array(TYPES_FIELD.getPreferredName(), types);
}
if (restTotalHitsAsInt) {
builder.field(REST_TOTAL_HITS_AS_INT_FIELD.getPreferredName(), restTotalHitsAsInt);
}
Expand Down Expand Up @@ -186,7 +168,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
*/
public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, SearchType searchType) throws IOException {
List<String> indices = new ArrayList<>();
List<String> types = new ArrayList<>();
IndicesOptions indicesOptions = DEFAULT_INDICES_OPTIONS;
BytesReference searchSource = null;
Script template = null;
Expand All @@ -208,16 +189,6 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
currentFieldName + "] field, but instead found [" + token + "]");
}
}
} else if (TYPES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
deprecationLogger.deprecatedAndMaybeLog("watcher_search_input", TYPES_DEPRECATION_MESSAGE);
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.VALUE_STRING) {
types.add(parser.textOrNull());
} else {
throw new ElasticsearchParseException("could not read search request. expected string values in [" +
currentFieldName + "] field, but instead found [" + token + "]");
}
}
} else {
throw new ElasticsearchParseException("could not read search request. unexpected array field [" +
currentFieldName + "]");
Expand Down Expand Up @@ -284,10 +255,6 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
if (INDICES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
String indicesStr = parser.text();
indices.addAll(Arrays.asList(Strings.delimitedListToStringArray(indicesStr, ",", " \t")));
} else if (TYPES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
deprecationLogger.deprecatedAndMaybeLog("watcher_search_input", TYPES_DEPRECATION_MESSAGE);
String typesStr = parser.text();
types.addAll(Arrays.asList(Strings.delimitedListToStringArray(typesStr, ",", " \t")));
} else if (SEARCH_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
searchType = SearchType.fromString(parser.text().toLowerCase(Locale.ROOT));
} else if (REST_TOTAL_HITS_AS_INT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
Expand All @@ -313,7 +280,7 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
}

WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest(indices.toArray(new String[0]),
types.size() == 0 ? null : types.toArray(new String[0]), searchType, indicesOptions, searchSource, template);
searchType, indicesOptions, searchSource, template);
request.setRestTotalHitsAsInt(totalHitsAsInt);
return request;
}
Expand All @@ -325,7 +292,6 @@ public boolean equals(Object o) {

WatcherSearchTemplateRequest other = (WatcherSearchTemplateRequest) o;
return Arrays.equals(indices, other.indices) &&
Arrays.equals(types, other.types) &&
Objects.equals(searchType, other.searchType) &&
Objects.equals(indicesOptions, other.indicesOptions) &&
Objects.equals(searchSource, other.searchSource) &&
Expand All @@ -336,11 +302,10 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(indices, types, searchType, indicesOptions, searchSource, template, restTotalHitsAsInt);
return Objects.hash(indices, searchType, indicesOptions, searchSource, template, restTotalHitsAsInt);
}

private static final ParseField INDICES_FIELD = new ParseField("indices");
private static final ParseField TYPES_FIELD = new ParseField("types");
private static final ParseField BODY_FIELD = new ParseField("body");
private static final ParseField SEARCH_TYPE_FIELD = new ParseField("search_type");
private static final ParseField INDICES_OPTIONS_FIELD = new ParseField("indices_options");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ public class HistoryTemplateSearchInputMappingsTests extends AbstractWatcherInte

public void testHttpFields() throws Exception {
String index = "the-index";
String type = "the-type";
createIndex(index);
indexDoc(index, "{}");
flush();
refresh();

WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest(
new String[]{index}, new String[]{type}, SearchType.QUERY_THEN_FETCH,
new String[]{index}, SearchType.QUERY_THEN_FETCH,
WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, new BytesArray("{}")
);
PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder()
Expand All @@ -64,7 +63,6 @@ WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, new BytesArray("{}")
SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource()
.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")))
.get();

Expand All @@ -85,12 +83,6 @@ WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, new BytesArray("{}")
assertThat(terms.getBucketByKey(index), notNullValue());
assertThat(terms.getBucketByKey(index).getDocCount(), is(1L));

terms = aggs.get("input_types");
assertThat(terms, notNullValue());
assertThat(terms.getBuckets().size(), is(1));
assertThat(terms.getBucketByKey(type), notNullValue());
assertThat(terms.getBucketByKey(type).getDocCount(), is(1L));

terms = aggs.get("input_body");
assertThat(terms, notNullValue());
assertThat(terms.getBuckets().size(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void testResponseToData() throws Exception {

public void testSerializeSearchRequest() throws Exception {
String[] expectedIndices = generateRandomStringArray(5, 5, true);
String[] expectedTypes = generateRandomStringArray(2, 5, true, false);
IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(),
randomBoolean(), WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS);
SearchType expectedSearchType = getRandomSupportedSearchType();
Expand All @@ -111,14 +110,14 @@ public void testSerializeSearchRequest() throws Exception {
ScriptType scriptType = randomFrom(ScriptType.values());
stored = scriptType == ScriptType.STORED;
expectedTemplate = new Script(scriptType, stored ? null : "mustache", text, params);
request = new WatcherSearchTemplateRequest(expectedIndices, expectedTypes, expectedSearchType,
request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType,
expectedIndicesOptions, expectedTemplate);
} else {
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery()).size(11);
XContentBuilder builder = jsonBuilder();
builder.value(sourceBuilder);
expectedSource = BytesReference.bytes(builder);
request = new WatcherSearchTemplateRequest(expectedIndices, expectedTypes, expectedSearchType,
request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType,
expectedIndicesOptions, expectedSource);
}

Expand All @@ -142,12 +141,6 @@ public void testSerializeSearchRequest() throws Exception {
assertThat(result.getTemplate().getIdOrCode(), equalTo(expectedSource.utf8ToString()));
assertThat(result.getTemplate().getType(), equalTo(ScriptType.INLINE));
}
if (expectedTypes == null) {
assertNull(result.getTypes());
} else {
assertThat(result.getTypes(), arrayContainingInAnyOrder(expectedTypes));
assertWarnings(WatcherSearchTemplateRequest.TYPES_DEPRECATION_MESSAGE);
}
}

public void testDeserializeSearchRequest() throws Exception {
Expand All @@ -164,16 +157,6 @@ public void testDeserializeSearchRequest() throws Exception {
}
}

String[] types = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
types = generateRandomStringArray(2, 5, false, false);
if (randomBoolean()) {
builder.array("types", types);
} else {
builder.field("types", Strings.arrayToCommaDelimitedString(types));
}
}

IndicesOptions indicesOptions = WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS;
if (randomBoolean()) {
indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(),
Expand Down Expand Up @@ -240,12 +223,6 @@ public void testDeserializeSearchRequest() throws Exception {
assertThat(result.getTemplate().getParams(), equalTo(template.getParams()));
assertThat(result.getTemplate().getLang(), equalTo(stored ? null : "mustache"));
}
if (types == Strings.EMPTY_ARRAY) {
assertNull(result.getTypes());
} else {
assertThat(result.getTypes(), arrayContainingInAnyOrder(types));
assertWarnings(WatcherSearchTemplateRequest.TYPES_DEPRECATION_MESSAGE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ public void testDefaultHitCountsConfigured() throws IOException {
assertHitCount(source, hitCountsAsInt);
}

public void testDeprecationForSingleType() throws IOException {
String source = "{\"types\":\"mytype\"}";
try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
parser.nextToken();
WatcherSearchTemplateRequest.fromXContent(parser, SearchType.QUERY_THEN_FETCH);
}
assertWarnings(WatcherSearchTemplateRequest.TYPES_DEPRECATION_MESSAGE);
}

public void testDeprecationForMultiType() throws IOException {
String source = "{\"types\":[\"mytype1\",\"mytype2\"]}";
try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
parser.nextToken();
WatcherSearchTemplateRequest.fromXContent(parser, SearchType.QUERY_THEN_FETCH);
}
assertWarnings(WatcherSearchTemplateRequest.TYPES_DEPRECATION_MESSAGE);
}

private void assertHitCount(String source, boolean expectedHitCountAsInt) throws IOException {
try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
parser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static WatcherSearchTemplateRequest templateRequest(SearchSourceBuilder s
try {
XContentBuilder xContentBuilder = jsonBuilder();
xContentBuilder.value(sourceBuilder);
return new WatcherSearchTemplateRequest(indices, null, searchType,
return new WatcherSearchTemplateRequest(indices, searchType,
WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, BytesReference.bytes(xContentBuilder));
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void testConditionSearchWithIndexedTemplate() throws Exception {
.get());

Script template = new Script(ScriptType.STORED, null, "my-template", Collections.emptyMap());
WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest(new String[]{"events"}, new String[0],
WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest(new String[]{"events"},
SearchType.DEFAULT, WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, template);
testConditionSearch(searchRequest);
}
Expand Down