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

Support search_type in Rank Evaluation API #48542

Merged
merged 4 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -520,6 +520,7 @@ static Request rankEval(RankEvalRequest rankEvalRequest) throws IOException {

Params params = new Params();
params.withIndicesOptions(rankEvalRequest.indicesOptions());
params.putParam("search_type", rankEvalRequest.searchType().name().toLowerCase(Locale.ROOT));
request.addParameters(params.asMap());
request.setEntity(createEntity(rankEvalRequest.getRankEvalSpec(), REQUEST_BODY_CONTENT_TYPE));
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,10 @@ public void testRankEval() throws Exception {
RankEvalRequest rankEvalRequest = new RankEvalRequest(spec, indices);
Map<String, String> expectedParams = new HashMap<>();
setRandomIndicesOptions(rankEvalRequest::indicesOptions, rankEvalRequest::indicesOptions, expectedParams);
if (randomBoolean()) {
rankEvalRequest.searchType(randomFrom(SearchType.CURRENTLY_SUPPORTED));
}
expectedParams.put("search_type", rankEvalRequest.searchType().name().toLowerCase(Locale.ROOT));

Request request = RequestConverters.rankEval(rankEvalRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
Expand All @@ -1475,7 +1479,7 @@ public void testRankEval() throws Exception {
}
endpoint.add(RestRankEvalAction.ENDPOINT);
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(4, request.getParameters().size());
assertEquals(5, request.getParameters().size());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(spec, request.getEntity());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package org.elasticsearch.index.rankeval;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -42,6 +44,8 @@ public class RankEvalRequest extends ActionRequest implements IndicesRequest.Rep
private IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS;
private String[] indices = Strings.EMPTY_ARRAY;

private SearchType searchType = SearchType.DEFAULT;

public RankEvalRequest(RankEvalSpec rankingEvaluationSpec, String[] indices) {
this.rankingEvaluationSpec = Objects.requireNonNull(rankingEvaluationSpec, "ranking evaluation specification must not be null");
indices(indices);
Expand All @@ -52,6 +56,9 @@ public RankEvalRequest(RankEvalSpec rankingEvaluationSpec, String[] indices) {
rankingEvaluationSpec = new RankEvalSpec(in);
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
searchType = SearchType.fromId(in.readByte());
}
}

RankEvalRequest() {
Expand Down Expand Up @@ -111,12 +118,38 @@ public void indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = Objects.requireNonNull(indicesOptions, "indicesOptions must not be null");
}

/**
* The search type to execute, defaults to {@link SearchType#DEFAULT}.
*/
public void searchType(SearchType searchType) {
this.searchType = Objects.requireNonNull(searchType, "searchType must not be null");
}

/**
* The tye of search to execute.
*/
public SearchType searchType() {
return searchType;
}

/**
* The a string representation search type to execute, defaults to {@link SearchType#DEFAULT}. Can be one of
* "dfs_query_then_fetch"/"dfsQueryThenFetch", "dfs_query_and_fetch"/"dfsQueryAndFetch", "query_then_fetch"/"queryThenFetch", and
* "query_and_fetch"/"queryAndFetch".
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed ? It should be enough to use the SearchType enum ? If not, then the comment should be changed since the types are restricted to dfs_query_then_fetch and query_then_fetch.

public void searchType(String searchType) {
searchType(SearchType.fromString(searchType));
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
rankingEvaluationSpec.writeTo(out);
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
out.writeByte(searchType.id());
}
}

@Override
Expand All @@ -130,11 +163,12 @@ public boolean equals(Object o) {
RankEvalRequest that = (RankEvalRequest) o;
return Objects.equals(indicesOptions, that.indicesOptions) &&
Arrays.equals(indices, that.indices) &&
Objects.equals(rankingEvaluationSpec, that.rankingEvaluationSpec);
Objects.equals(rankingEvaluationSpec, that.rankingEvaluationSpec) &&
Objects.equals(searchType, that.searchType);
}

@Override
public int hashCode() {
return Objects.hash(indicesOptions, Arrays.hashCode(indices), rankingEvaluationSpec);
return Objects.hash(indicesOptions, Arrays.hashCode(indices), rankingEvaluationSpec, searchType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
private static void parseRankEvalRequest(RankEvalRequest rankEvalRequest, RestRequest request, XContentParser parser) {
rankEvalRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
rankEvalRequest.indicesOptions(IndicesOptions.fromRequest(request, rankEvalRequest.indicesOptions()));
if (request.hasParam("search_type")) {
rankEvalRequest.searchType(request.param("search_type"));
}
RankEvalSpec spec = RankEvalSpec.parse(parser);
rankEvalRequest.setRankEvalSpec(spec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ LoggingDeprecationHandler.INSTANCE, new BytesArray(resolvedRequest), XContentTyp
}
SearchRequest searchRequest = new SearchRequest(request.indices(), evaluationRequest);
searchRequest.indicesOptions(request.indicesOptions());
searchRequest.searchType(request.searchType());
msearchRequest.add(searchRequest);
}
assert ratedRequestsInSearch.size() == msearchRequest.requests().size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.rankeval;

import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable.Reader;
Expand Down Expand Up @@ -62,6 +63,7 @@ protected RankEvalRequest createTestInstance() {
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
randomBoolean());
rankEvalRequest.indicesOptions(indicesOptions);
rankEvalRequest.searchType(randomFrom(SearchType.DFS_QUERY_THEN_FETCH, SearchType.QUERY_THEN_FETCH));
return rankEvalRequest;
}

Expand All @@ -77,8 +79,22 @@ protected RankEvalRequest mutateInstance(RankEvalRequest instance) throws IOExce
mutators.add(() -> mutation.indices(ArrayUtils.concat(instance.indices(), new String[] { randomAlphaOfLength(10) })));
mutators.add(() -> mutation.indicesOptions(randomValueOtherThan(instance.indicesOptions(),
() -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()))));
mutators.add(() -> {
if (instance.searchType() == SearchType.DFS_QUERY_THEN_FETCH) {
mutation.searchType(SearchType.QUERY_THEN_FETCH);
} else {
mutation.searchType(SearchType.DFS_QUERY_THEN_FETCH);
}
});
mutators.add(() -> mutation.setRankEvalSpec(RankEvalSpecTests.mutateTestItem(instance.getRankEvalSpec())));
mutators.add(() -> mutation.setRankEvalSpec(RankEvalSpecTests.mutateTestItem(instance.getRankEvalSpec())));
randomFrom(mutators).run();
return mutation;
}

public void testInvalidSearchType() {
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
() -> new RankEvalRequest(RankEvalSpecTests.createTestItem(), new String[] { "test" }).searchType("invalid"));
assertEquals("No search type for [invalid]", ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.index.rankeval;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.TransportService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.mock;

public class TransportRankEvalActionTests extends ESTestCase {

private Settings settings = Settings.builder().put("path.home", createTempDir().toString()).put("node.name", "test-" + getTestName())
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();

/**
* Test that request parameters like indicesOptions or searchType from ranking evaluation request are transfered to msearch request
*/
public void testTransferRequestParameters() throws Exception {
String indexName = "test_index";
List<RatedRequest> specifications = new ArrayList<>();
specifications
.add(new RatedRequest("amsterdam_query", Arrays.asList(new RatedDocument(indexName, "1", 3)), new SearchSourceBuilder()));
RankEvalRequest rankEvalRequest = new RankEvalRequest(new RankEvalSpec(specifications, new DiscountedCumulativeGain()),
new String[] { indexName });
SearchType expectedSearchType = randomFrom(SearchType.CURRENTLY_SUPPORTED);
rankEvalRequest.searchType(expectedSearchType);
IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(),
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
rankEvalRequest.indicesOptions(expectedIndicesOptions);

NodeClient client = new NodeClient(settings, null) {
@Override
public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) {
assertEquals(1, request.requests().size());
assertEquals(expectedSearchType, request.requests().get(0).searchType());
assertArrayEquals(new String[]{indexName}, request.requests().get(0).indices());
assertEquals(expectedIndicesOptions, request.requests().get(0).indicesOptions());
}
};

TransportRankEvalAction action = new TransportRankEvalAction(mock(ActionFilters.class), client, mock(TransportService.class),
mock(ScriptService.class), NamedXContentRegistry.EMPTY);
action.doExecute(null, rankEvalRequest, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ setup:
- do:
rank_eval:
index: foo,
search_type: query_then_fetch
body: {
"requests" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
],
"default":"open",
"description":"Whether to expand wildcard expression to concrete indices that are open, closed or both."
},
"search_type":{
"type":"enum",
"options":[
"query_then_fetch",
"dfs_query_then_fetch"
],
"description":"Search operation type"
}
},
"body":{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@
import org.elasticsearch.tasks.Task;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem to be for this pr ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I pulled this out to #48523 and didn't delete it here.

import org.elasticsearch.tasks.TaskManager;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.TransportService;
import org.junit.After;
import org.junit.Before;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -63,22 +60,6 @@

public class TransportMultiSearchActionTests extends ESTestCase {

protected ThreadPool threadPool;

@Before
@Override
public void setUp() throws Exception {
super.setUp();
threadPool = new TestThreadPool(getTestName());
}

@After
@Override
public void tearDown() throws Exception {
threadPool.shutdown();
super.tearDown();
}

public void testParentTaskId() throws Exception {
// Initialize dependencies of TransportMultiSearchAction
Settings settings = Settings.builder()
Expand Down