diff --git a/rest-api-spec/api/mlt.json b/rest-api-spec/api/mlt.json index 83e0a2fd1b6..bb15dc18017 100644 --- a/rest-api-spec/api/mlt.json +++ b/rest-api-spec/api/mlt.json @@ -71,10 +71,6 @@ "type" : "list", "description" : "A comma-separated list of indices to perform the query against (default: the index containing the document)" }, - "search_query_hint": { - "type" : "string", - "description" : "The search query hint" - }, "search_scroll": { "type" : "string", "description" : "A scroll search request definition" diff --git a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java index b3aff50a09a..1d04b354b0b 100644 --- a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java +++ b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java @@ -80,7 +80,6 @@ public class MoreLikeThisRequest extends ActionRequest impl private SearchType searchType = SearchType.DEFAULT; private int searchSize = 0; private int searchFrom = 0; - private String searchQueryHint; private String[] searchIndices; private String[] searchTypes; private Scroll searchScroll; @@ -506,21 +505,6 @@ public class MoreLikeThisRequest extends ActionRequest impl return this.searchTypes; } - /** - * Optional search query hint. - */ - public MoreLikeThisRequest searchQueryHint(String searchQueryHint) { - this.searchQueryHint = searchQueryHint; - return this; - } - - /** - * Optional search query hint. - */ - public String searchQueryHint() { - return this.searchQueryHint; - } - /** * An optional search scroll request to be able to continue and scroll the search * operation. @@ -616,8 +600,11 @@ public class MoreLikeThisRequest extends ActionRequest impl } searchType = SearchType.fromId(in.readByte()); - if (in.readBoolean()) { - searchQueryHint = in.readString(); + if (in.getVersion().before(Version.V_1_4_0)) { + //searchQueryHint was unused and removed in 1.4 + if (in.readBoolean()) { + in.readString(); + } } size = in.readVInt(); if (size == 0) { @@ -689,11 +676,9 @@ public class MoreLikeThisRequest extends ActionRequest impl } out.writeByte(searchType.id()); - if (searchQueryHint == null) { + if (out.getVersion().before(Version.V_1_4_0)) { + //searchQueryHint was unused and removed in 1.4 out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeString(searchQueryHint); } if (searchIndices == null) { out.writeVInt(0); diff --git a/src/main/java/org/elasticsearch/rest/action/mlt/RestMoreLikeThisAction.java b/src/main/java/org/elasticsearch/rest/action/mlt/RestMoreLikeThisAction.java index 6b43352c7b0..8a201218384 100644 --- a/src/main/java/org/elasticsearch/rest/action/mlt/RestMoreLikeThisAction.java +++ b/src/main/java/org/elasticsearch/rest/action/mlt/RestMoreLikeThisAction.java @@ -70,7 +70,6 @@ public class RestMoreLikeThisAction extends BaseRestHandler { mltRequest.searchType(SearchType.fromString(request.param("search_type"))); mltRequest.searchIndices(request.paramAsStringArray("search_indices", null)); mltRequest.searchTypes(request.paramAsStringArray("search_types", null)); - mltRequest.searchQueryHint(request.param("search_query_hint")); mltRequest.searchSize(request.paramAsInt("search_size", mltRequest.searchSize())); mltRequest.searchFrom(request.paramAsInt("search_from", mltRequest.searchFrom())); String searchScroll = request.param("search_scroll"); diff --git a/src/test/java/org/elasticsearch/action/mlt/MoreLikeThisRequestTests.java b/src/test/java/org/elasticsearch/action/mlt/MoreLikeThisRequestTests.java new file mode 100644 index 00000000000..7d8d3675523 --- /dev/null +++ b/src/test/java/org/elasticsearch/action/mlt/MoreLikeThisRequestTests.java @@ -0,0 +1,165 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.mlt; + +import org.elasticsearch.Version; +import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.io.stream.BytesStreamInput; +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.Scroll; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.test.ElasticsearchTestCase; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.*; + +public class MoreLikeThisRequestTests extends ElasticsearchTestCase { + + @Test + public void testSerialization() throws IOException { + + MoreLikeThisRequest mltRequest = new MoreLikeThisRequest(randomAsciiOfLength(randomIntBetween(1, 20))) + .id(randomAsciiOfLength(randomIntBetween(1, 20))).type(randomAsciiOfLength(randomIntBetween(1, 20))); + + if (randomBoolean()) { + mltRequest.boostTerms(randomFloat()); + } + if (randomBoolean()) { + mltRequest.maxDocFreq(randomInt()); + } + if (randomBoolean()) { + mltRequest.minDocFreq(randomInt()); + } + if (randomBoolean()) { + mltRequest.maxQueryTerms(randomInt()); + } + if (randomBoolean()) { + mltRequest.minWordLength(randomInt()); + } + if (randomBoolean()) { + mltRequest.maxWordLength(randomInt()); + } + if (randomBoolean()) { + mltRequest.percentTermsToMatch(randomFloat()); + } + if (randomBoolean()) { + mltRequest.searchTypes(randomStrings(5)); + } + if (randomBoolean()) { + mltRequest.searchType(randomFrom(SearchType.values())); + } + if (randomBoolean()) { + mltRequest.searchIndices(randomStrings(5)); + } + if (randomBoolean()) { + mltRequest.routing(randomAsciiOfLength(randomIntBetween(1, 20))); + } + if (randomBoolean()) { + mltRequest.searchFrom(randomInt()); + } + if (randomBoolean()) { + mltRequest.searchSize(randomInt()); + } + if (randomBoolean()) { + mltRequest.searchScroll(new Scroll(TimeValue.timeValueNanos(randomLong()))); + } + if (randomBoolean()) { + mltRequest.searchSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("term", "value"))); + } + if(randomBoolean()) { + mltRequest.include(randomBoolean()); + } + if (randomBoolean()) { + mltRequest.stopWords(randomStrings(10)); + } + if (randomBoolean()) { + mltRequest.fields(randomStrings(5)); + } + + BytesStreamOutput out = new BytesStreamOutput(); + out.setVersion(randomVersion()); + mltRequest.writeTo(out); + + BytesStreamInput in = new BytesStreamInput(out.bytes()); + in.setVersion(out.getVersion()); + MoreLikeThisRequest mltRequest2 = new MoreLikeThisRequest(); + mltRequest2.readFrom(in); + + assertThat(mltRequest2.index(), equalTo(mltRequest.index())); + assertThat(mltRequest2.type(), equalTo(mltRequest.type())); + assertThat(mltRequest2.id(), equalTo(mltRequest.id())); + assertThat(mltRequest2.boostTerms(), equalTo(mltRequest.boostTerms())); + assertThat(mltRequest2.maxDocFreq(), equalTo(mltRequest.maxDocFreq())); + assertThat(mltRequest2.minDocFreq(), equalTo(mltRequest.minDocFreq())); + assertThat(mltRequest2.maxQueryTerms(), equalTo(mltRequest.maxQueryTerms())); + assertThat(mltRequest2.minWordLength(), equalTo(mltRequest.minWordLength())); + assertThat(mltRequest2.maxWordLength(), equalTo(mltRequest.maxWordLength())); + assertThat(mltRequest2.percentTermsToMatch(), equalTo(mltRequest.percentTermsToMatch())); + assertThat(mltRequest2.searchTypes(), equalTo(mltRequest.searchTypes())); + assertThat(mltRequest2.searchType(), equalTo(mltRequest.searchType())); + assertThat(mltRequest2.searchIndices(), equalTo(mltRequest.searchIndices())); + assertThat(mltRequest2.routing(), equalTo(mltRequest.routing())); + assertThat(mltRequest2.searchFrom(), equalTo(mltRequest.searchFrom())); + assertThat(mltRequest2.searchSize(), equalTo(mltRequest.searchSize())); + if (mltRequest.searchScroll() == null) { + assertThat(mltRequest2.searchScroll(), nullValue()); + } else { + assertThat(mltRequest2.searchFrom(), notNullValue()); + assertThat(mltRequest2.searchScroll().keepAlive(), equalTo(mltRequest.searchScroll().keepAlive())); + } + + if (mltRequest.searchSource() == null) { + assertThat(mltRequest2.searchSource().length(), equalTo(0)); + } else { + assertThat(mltRequest2.searchSource().length(), equalTo(mltRequest.searchSource().length())); + assertThat(mltRequest2.searchSourceUnsafe(), equalTo(mltRequest.searchSourceUnsafe())); + } + + if (mltRequest.stopWords() != null && mltRequest.stopWords().length > 0) { + assertThat(mltRequest2.stopWords(), equalTo(mltRequest.stopWords())); + } else { + assertThat(mltRequest2.stopWords(), nullValue()); + } + if (mltRequest.fields() == null) { + assertThat(mltRequest2.fields(), equalTo(Strings.EMPTY_ARRAY)); + } else { + assertThat(mltRequest2.fields(), equalTo(mltRequest.fields())); + } + if (out.getVersion().onOrAfter(Version.V_1_2_0)) { + assertThat(mltRequest2.include(), equalTo(mltRequest.include())); + } else { + assertThat(mltRequest2.include(), is(false)); + } + } + + private static String[] randomStrings(int max) { + int count = randomIntBetween(0, max); + String[] strings = new String[count]; + for (int i = 0; i < strings.length; i++) { + strings[i] = randomAsciiOfLength(randomIntBetween(1, 20)); + } + return strings; + } +}