Adressing review comments and adding test for failed request

This commit is contained in:
Christoph Büscher 2016-08-09 18:50:15 +02:00
parent 8856565b89
commit a2e6dc2750
5 changed files with 43 additions and 26 deletions

View File

@ -43,5 +43,4 @@ public class RankEvalAction extends Action<RankEvalRequest, RankEvalResponse, Ra
public RankEvalResponse newResponse() {
return new RankEvalResponse();
}
}

View File

@ -51,10 +51,6 @@ public class RankEvalResponse extends ActionResponse implements ToXContent {
public RankEvalResponse() {
}
public RankEvalResponse(StreamInput in) throws IOException {
this.readFrom(in);
}
public RankEvalResponse(String specId, double qualityLevel, Map<String, Collection<RatedDocumentKey>> unknownDocs) {
this.specId = specId;
this.qualityLevel = qualityLevel;
@ -62,7 +58,7 @@ public class RankEvalResponse extends ActionResponse implements ToXContent {
}
public Object getSpecId() {
public String getSpecId() {
return specId;
}
@ -82,8 +78,8 @@ public class RankEvalResponse extends ActionResponse implements ToXContent {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(specId);
out.writeOptionalDouble(qualityLevel);
out.writeString(specId);
out.writeDouble(qualityLevel);
out.writeGenericValue(getUnknownDocs());
}
@ -91,9 +87,9 @@ public class RankEvalResponse extends ActionResponse implements ToXContent {
@SuppressWarnings("unchecked")
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
this.specId = in.readOptionalString();
this.qualityLevel = in.readOptionalDouble();
this.unknownDocs = (Map<String, Collection<String>>) in.readGenericValue();
this.specId = in.readString();
this.qualityLevel = in.readDouble();
this.unknownDocs = (Map<String, Collection<RatedDocumentKey>>) in.readGenericValue();
}
@Override

View File

@ -109,6 +109,7 @@ public class ReciprocalRank extends RankedListQualityMetric {
int firstRelevant = -1;
boolean found = false;
for (int i = 0; i < hits.length; i++) {
// TODO here we use index/type/id triple not for a rated document but an unrated document in the search hits. Maybe rename?
RatedDocumentKey id = new RatedDocumentKey(hits[i].getIndex(), hits[i].getType(), hits[i].getId());
if (relevantDocIds.contains(id)) {
if (found == false && i < maxAcceptableRank) {

View File

@ -63,16 +63,15 @@ public class TransportRankEvalAction extends HandledTransportAction<RankEvalRequ
@Override
protected void doExecute(RankEvalRequest request, ActionListener<RankEvalResponse> listener) {
RankEvalSpec qualityTask = request.getRankEvalSpec();
RankedListQualityMetric metric = qualityTask.getEvaluator();
Map<String, Collection<RatedDocumentKey>> unknownDocs = new HashMap<>();
Collection<QuerySpec> specifications = qualityTask.getSpecifications();
AtomicInteger numberOfEvaluationQueries = new AtomicInteger(specifications.size());
AtomicInteger responseCounter = new AtomicInteger(specifications.size());
Map<String, EvalQueryQuality> partialResults = new ConcurrentHashMap<>(specifications.size());
for (QuerySpec querySpecification : specifications) {
final RankEvalActionListener searchListener = new RankEvalActionListener(listener, qualityTask, querySpecification,
partialResults, unknownDocs, numberOfEvaluationQueries);
partialResults, unknownDocs, responseCounter);
SearchSourceBuilder specRequest = querySpecification.getTestRequest();
String[] indices = new String[querySpecification.getIndices().size()];
querySpecification.getIndices().toArray(indices);
@ -111,7 +110,7 @@ public class TransportRankEvalAction extends HandledTransportAction<RankEvalRequ
partialResults.put(specification.getSpecId(), queryQuality);
unknownDocs.put(specification.getSpecId(), queryQuality.getUnknownDocs());
if (responseCounter.decrementAndGet() == 0) {
if (responseCounter.decrementAndGet() < 1) {
// TODO add other statistics like micro/macro avg?
listener.onResponse(
new RankEvalResponse(task.getTaskId(), task.getEvaluator().combine(partialResults.values()), unknownDocs));
@ -120,7 +119,6 @@ public class TransportRankEvalAction extends HandledTransportAction<RankEvalRequ
@Override
public void onFailure(Exception exception) {
// TODO this fails the complete request. Investigate if maybe we want to collect errors and still return partial result.
this.listener.onFailure(exception);
}
}

View File

@ -19,7 +19,9 @@
package org.elasticsearch.index.rankeval;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.rankeval.PrecisionAtN.Rating;
import org.elasticsearch.index.rankeval.QuerySpec;
import org.elasticsearch.index.rankeval.RankEvalAction;
@ -36,6 +38,7 @@ import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Before;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
@ -74,24 +77,19 @@ public class RankEvalRequestTests extends ESIntegTestCase {
}
public void testPrecisionAtRequest() {
ArrayList<String> indices = new ArrayList<>();
indices.add("test");
ArrayList<String> types = new ArrayList<>();
types.add("testtype");
List<String> indices = Arrays.asList(new String[] { "test" });
List<String> types = Arrays.asList(new String[] { "testtype" });
String specId = randomAsciiOfLength(10);
List<QuerySpec> specifications = new ArrayList<>();
SearchSourceBuilder testQuery = new SearchSourceBuilder();
testQuery.query(new MatchAllQueryBuilder());
specifications.add(new QuerySpec("amsterdam_query", testQuery, indices, types, createRelevant("2", "3", "4", "5")));
specifications.add(new QuerySpec("berlin_query", testQuery, indices, types, createRelevant("1")));
specifications.add(new QuerySpec("amsterdam_query", testQuery, indices, types, createRelevant("2", "3", "4", "5")));
specifications.add(new QuerySpec("berlin_query", testQuery, indices, types, createRelevant("1")));
RankEvalSpec task = new RankEvalSpec(specId, specifications, new PrecisionAtN(10));
RankEvalRequestBuilder builder = new RankEvalRequestBuilder(
client(),
RankEvalAction.INSTANCE,
new RankEvalRequest());
RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, new RankEvalRequest());
builder.setRankEvalSpec(task);
RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet();
@ -109,6 +107,31 @@ public class RankEvalRequestTests extends ESIntegTestCase {
}
}
/**
* test that running a bad query (e.g. one that will target a non existing field) will error
*/
public void testBadQuery() {
List<String> indices = Arrays.asList(new String[] { "test" });
List<String> types = Arrays.asList(new String[] { "testtype" });
String specId = randomAsciiOfLength(10);
List<QuerySpec> specifications = new ArrayList<>();
SearchSourceBuilder amsterdamQuery = new SearchSourceBuilder();
amsterdamQuery.query(new MatchAllQueryBuilder());
specifications.add(new QuerySpec("amsterdam_query", amsterdamQuery, indices, types, createRelevant("2", "3", "4", "5")));
SearchSourceBuilder brokenQuery = new SearchSourceBuilder();
RangeQueryBuilder brokenRangeQuery = new RangeQueryBuilder("text").timeZone("CET");
brokenQuery.query(brokenRangeQuery);
specifications.add(new QuerySpec("broken_query", brokenQuery, indices, types, createRelevant("1")));
RankEvalSpec task = new RankEvalSpec(specId, specifications, new PrecisionAtN(10));
RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, new RankEvalRequest());
builder.setRankEvalSpec(task);
expectThrows(SearchPhaseExecutionException.class, () -> client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet());
}
private static List<RatedDocument> createRelevant(String... docs) {
List<RatedDocument> relevant = new ArrayList<>();
for (String doc : docs) {