RankEval: Check for duplicate keys in rated documents
When multiple ratings for the same document (identified by _index, _type, _id) are specified in the request we should throw an error. This change adds a check for this in the RatedRequest setter (and ctor that uses that setter). Closes #20997
This commit is contained in:
parent
51102ee91c
commit
25565b9baa
|
@ -33,8 +33,10 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
@ -68,7 +70,7 @@ public class RatedRequest extends ToXContentToBytes implements Writeable {
|
||||||
this.testRequest = testRequest;
|
this.testRequest = testRequest;
|
||||||
this.indices = indices;
|
this.indices = indices;
|
||||||
this.types = types;
|
this.types = types;
|
||||||
this.ratedDocs = ratedDocs;
|
setRatedDocs(ratedDocs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RatedRequest(StreamInput in) throws IOException {
|
public RatedRequest(StreamInput in) throws IOException {
|
||||||
|
@ -159,8 +161,19 @@ public class RatedRequest extends ToXContentToBytes implements Writeable {
|
||||||
return ratedDocs;
|
return ratedDocs;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set a list of rated documents for this query. */
|
/**
|
||||||
|
* Set a list of rated documents for this query.
|
||||||
|
* No documents with same _index/_type/id allowed.
|
||||||
|
**/
|
||||||
public void setRatedDocs(List<RatedDocument> ratedDocs) {
|
public void setRatedDocs(List<RatedDocument> ratedDocs) {
|
||||||
|
Set<DocumentKey> docKeys = new HashSet<>();
|
||||||
|
for (RatedDocument doc : ratedDocs) {
|
||||||
|
if (docKeys.add(doc.getKey()) == false) {
|
||||||
|
String docKeyToString = doc.getKey().toString().replaceAll("\n", "").replaceAll(" ", " ");
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Found duplicate rated document key [" + docKeyToString + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
this.ratedDocs = ratedDocs;
|
this.ratedDocs = ratedDocs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.junit.BeforeClass;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -131,6 +132,16 @@ public class RatedRequestsTests extends ESTestCase {
|
||||||
assertEquals(testItem.hashCode(), parsedItem.hashCode());
|
assertEquals(testItem.hashCode(), parsedItem.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDuplicateRatedDocThrowsException() {
|
||||||
|
RatedRequest request = createTestItem(Arrays.asList("index"), Arrays.asList("type"));
|
||||||
|
List<RatedDocument> ratedDocs = Arrays.asList(new RatedDocument(new DocumentKey("index1", "type1", "id1"), 1),
|
||||||
|
new RatedDocument(new DocumentKey("index1", "type1", "id1"), 5));
|
||||||
|
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> request.setRatedDocs(ratedDocs));
|
||||||
|
assertEquals(
|
||||||
|
"Found duplicate rated document key [{ \"_index\" : \"index1\", \"_type\" : \"type1\", \"_id\" : \"id1\"}]",
|
||||||
|
ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
public void testParseFromXContent() throws IOException {
|
public void testParseFromXContent() throws IOException {
|
||||||
// we modify the order of index/type/docId to make sure it doesn't matter for parsing xContent
|
// we modify the order of index/type/docId to make sure it doesn't matter for parsing xContent
|
||||||
String querySpecString = " {\n"
|
String querySpecString = " {\n"
|
||||||
|
|
Loading…
Reference in New Issue