Merge RankEvalResult with Response

The current response object only serves as a wrapper around the result object.
This change merges the two classes into one.
This commit is contained in:
Christoph Büscher 2016-08-08 13:29:44 +02:00
parent 4385b29f80
commit 0578a96483
4 changed files with 43 additions and 114 deletions

View File

@ -38,42 +38,64 @@ import java.util.Map;
* Documents of unknown quality - i.e. those that haven't been supplied in the set of annotated documents but have been returned
* by the search are not taken into consideration when computing precision at n - they are ignored.
*
* TODO get rid of either this or RankEvalResult
**/
//TODO instead of just returning averages over complete results, think of other statistics, micro avg, macro avg, partial results
public class RankEvalResponse extends ActionResponse implements ToXContent {
private RankEvalResult qualityResult;
/**ID of QA specification this result was generated for.*/
private String specId;
/**Average precision observed when issuing query intents with this specification.*/
private double qualityLevel;
/**Mapping from intent id to all documents seen for this intent that were not annotated.*/
private Map<String, Collection<String>> unknownDocs;
public RankEvalResponse() {
}
@SuppressWarnings("unchecked")
public RankEvalResponse(StreamInput in) throws IOException {
super.readFrom(in);
this.qualityResult = new RankEvalResult(in);
this.specId = in.readString();
this.qualityLevel = in.readDouble();
this.unknownDocs = (Map<String, Collection<String>>) in.readGenericValue();
}
public RankEvalResponse(String specId, double qualityLevel, Map<String, Collection<String>> unknownDocs) {
this.specId = specId;
this.qualityLevel = qualityLevel;
this.unknownDocs = unknownDocs;
}
public String getSpecId() {
return specId;
}
public double getQualityLevel() {
return qualityLevel;
}
public Map<String, Collection<String>> getUnknownDocs() {
return unknownDocs;
}
@Override
public String toString() {
return "RankEvalResult, ID :[" + specId + "], quality: " + qualityLevel + ", unknown docs: " + unknownDocs;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
qualityResult.writeTo(out);
}
public void setRankEvalResult(RankEvalResult result) {
this.qualityResult = result;
}
public RankEvalResult getRankEvalResult() {
return qualityResult;
out.writeString(specId);
out.writeDouble(qualityLevel);
out.writeGenericValue(getUnknownDocs());
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("rank_eval");
builder.field("spec_id", qualityResult.getSpecId());
builder.field("quality_level", qualityResult.getQualityLevel());
builder.field("spec_id", specId);
builder.field("quality_level", qualityLevel);
builder.startArray("unknown_docs");
Map<String, Collection<String>> unknownDocs = qualityResult.getUnknownDocs();
for (String key : unknownDocs.keySet()) {
builder.startObject();
builder.field(key, unknownDocs.get(key));

View File

@ -1,80 +0,0 @@
/*
* 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.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
/**
* For each precision at n computation the id of the search request specification used to generate search requests is returned
* for reference. In addition the averaged precision and the ids of all documents returned but not found annotated is returned.
* */
// TODO do we need an extra class for this or it RankEvalResponse enough?
// TODO instead of just returning averages over complete results, think of other statistics, micro avg, macro avg, partial results
public class RankEvalResult implements Writeable {
/**ID of QA specification this result was generated for.*/
private String specId;
/**Average precision observed when issuing query intents with this specification.*/
private double qualityLevel;
/**Mapping from intent id to all documents seen for this intent that were not annotated.*/
private Map<String, Collection<String>> unknownDocs;
@SuppressWarnings("unchecked")
public RankEvalResult(StreamInput in) throws IOException {
this.specId = in.readString();
this.qualityLevel = in.readDouble();
this.unknownDocs = (Map<String, Collection<String>>) in.readGenericValue();
}
public RankEvalResult(String specId, double quality, Map<String, Collection<String>> unknownDocs) {
this.specId = specId;
this.qualityLevel = quality;
this.unknownDocs = unknownDocs;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(specId);
out.writeDouble(qualityLevel);
out.writeGenericValue(getUnknownDocs());
}
public String getSpecId() {
return specId;
}
public double getQualityLevel() {
return qualityLevel;
}
public Map<String, Collection<String>> getUnknownDocs() {
return unknownDocs;
}
@Override
public String toString() {
return "RankEvalResult, ID :[" + specId + "], quality: " + qualityLevel + ", unknown docs: " + unknownDocs;
}
}

View File

@ -105,10 +105,8 @@ public class TransportRankEvalAction extends HandledTransportAction<RankEvalRequ
unknownDocs.put(spec.getSpecId(), queryQuality.getUnknownDocs());
}
RankEvalResponse response = new RankEvalResponse();
// TODO add other statistics like micro/macro avg?
RankEvalResult result = new RankEvalResult(qualityTask.getTaskId(), metric.combine(partialResults), unknownDocs);
response.setRankEvalResult(result);
RankEvalResponse response = new RankEvalResponse(qualityTask.getTaskId(), metric.combine(partialResults), unknownDocs);
listener.onResponse(response);
}
}

View File

@ -20,17 +20,7 @@
package org.elasticsearch.index.rankeval;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.rankeval.PrecisionAtN;
import org.elasticsearch.index.rankeval.PrecisionAtN.Rating;
import org.elasticsearch.index.rankeval.QuerySpec;
import org.elasticsearch.index.rankeval.RankEvalAction;
import org.elasticsearch.index.rankeval.RankEvalPlugin;
import org.elasticsearch.index.rankeval.RankEvalRequest;
import org.elasticsearch.index.rankeval.RankEvalRequestBuilder;
import org.elasticsearch.index.rankeval.RankEvalResponse;
import org.elasticsearch.index.rankeval.RankEvalResult;
import org.elasticsearch.index.rankeval.RankEvalSpec;
import org.elasticsearch.index.rankeval.RatedDocument;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESIntegTestCase;
@ -97,10 +87,9 @@ public class RankEvalRequestTests extends ESIntegTestCase {
builder.setRankEvalSpec(task);
RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet();
RankEvalResult result = response.getRankEvalResult();
assertEquals(specId, result.getSpecId());
assertEquals(1.0, result.getQualityLevel(), Double.MIN_VALUE);
Set<Entry<String, Collection<String>>> entrySet = result.getUnknownDocs().entrySet();
assertEquals(specId, response.getSpecId());
assertEquals(1.0, response.getQualityLevel(), Double.MIN_VALUE);
Set<Entry<String, Collection<String>>> entrySet = response.getUnknownDocs().entrySet();
assertEquals(2, entrySet.size());
for (Entry<String, Collection<String>> entry : entrySet) {
if (entry.getKey() == "amsterdam_query") {