Skip to content

Commit

Permalink
Support search_type in Rank Evaluation API (elastic#48542)
Browse files Browse the repository at this point in the history
Adding support for the `search_type` request parameter to the Ranking Evaluation
API since this parameter can impact the ranking and the metric score and should
be choosen in the same way when evaluating the search as later in the real
search.

Closes elastic#48503
  • Loading branch information
Christoph Büscher committed Oct 29, 2019
1 parent a036215 commit 9dd0018
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,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 @@ -1642,6 +1642,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 @@ -1651,7 +1655,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 @@ -24,6 +24,7 @@
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 @@ -43,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 @@ -51,17 +54,10 @@ public RankEvalRequest(RankEvalSpec rankingEvaluationSpec, String[] indices) {
RankEvalRequest(StreamInput in) throws IOException {
super(in);
rankingEvaluationSpec = new RankEvalSpec(in);
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
} else {
// readStringArray uses readVInt for size, we used readInt in 6.2
int indicesSize = in.readInt();
String[] indices = new String[indicesSize];
for (int i = 0; i < indicesSize; i++) {
indices[i] = in.readString();
}
// no indices options yet
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
if (in.getVersion().onOrAfter(Version.V_7_6_0)) {
searchType = SearchType.fromId(in.readByte());
}
}

Expand Down Expand Up @@ -122,20 +118,28 @@ 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 type of search to execute.
*/
public SearchType searchType() {
return searchType;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
rankingEvaluationSpec.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
} else {
// writeStringArray uses writeVInt for size, we used writeInt in 6.2
out.writeInt(indices.length);
for (String index : indices) {
out.writeString(index);
}
// no indices options yet
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
if (out.getVersion().onOrAfter(Version.V_7_6_0)) {
out.writeByte(searchType.id());
}
}

Expand All @@ -150,11 +154,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 @@ -19,6 +19,7 @@

package org.elasticsearch.index.rankeval;

import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -109,6 +110,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(SearchType.fromString(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,17 @@ 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;
}

}
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 @@ -48,6 +48,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

0 comments on commit 9dd0018

Please sign in to comment.