[TEST] Beef up MoreLikeThisActionTests#testCompareMoreLikeThisDSLWithAPI
This commit is contained in:
parent
91b74931a3
commit
d9441747e8
|
@ -401,47 +401,50 @@ public class MoreLikeThisActionTests extends ElasticsearchIntegrationTest {
|
||||||
builders.add(client().prepareIndex("test", "type1").setSource("text", texts[i]).setId(String.valueOf(i)));
|
builders.add(client().prepareIndex("test", "type1").setSource("text", texts[i]).setId(String.valueOf(i)));
|
||||||
}
|
}
|
||||||
indexRandom(true, builders);
|
indexRandom(true, builders);
|
||||||
|
int iters = between(10, 20);
|
||||||
|
for (int j = 0; j < iters; j++) {
|
||||||
|
logger.info("Running MoreLikeThis DSL with IDs");
|
||||||
|
String id = String.valueOf(getRandom().nextInt(texts.length));
|
||||||
|
Client client = client();
|
||||||
|
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").ids(id).minTermFreq(1).minDocFreq(1);
|
||||||
|
SearchResponse mltResponseDSL = client.prepareSearch()
|
||||||
|
.setSearchType(SearchType.QUERY_THEN_FETCH)
|
||||||
|
.setTypes("type1")
|
||||||
|
.setQuery(queryBuilder)
|
||||||
|
.setSize(texts.length)
|
||||||
|
.execute().actionGet();
|
||||||
|
assertSearchResponse(mltResponseDSL);
|
||||||
|
|
||||||
logger.info("Running MoreLikeThis DSL with IDs");
|
logger.info("Running MoreLikeThis API");
|
||||||
Client client = client();
|
MoreLikeThisRequest mltRequest = moreLikeThisRequest("test").type("type1").searchSize(texts.length).id(id).minTermFreq(1).minDocFreq(1);
|
||||||
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").ids("0").minTermFreq(1).minDocFreq(1);
|
SearchResponse mltResponseAPI = client.moreLikeThis(mltRequest).actionGet();
|
||||||
SearchResponse mltResponseDSL = client.prepareSearch()
|
assertSearchResponse(mltResponseAPI);
|
||||||
.setSearchType(SearchType.QUERY_THEN_FETCH)
|
|
||||||
.setTypes("type1")
|
|
||||||
.setQuery(queryBuilder)
|
|
||||||
.setSize(texts.length)
|
|
||||||
.execute().actionGet();
|
|
||||||
assertSearchResponse(mltResponseDSL);
|
|
||||||
|
|
||||||
logger.info("Running MoreLikeThis API");
|
logger.info("Ensure the documents and scores returned are the same.");
|
||||||
MoreLikeThisRequest mltRequest = moreLikeThisRequest("test").type("type1").searchSize(texts.length).id("0").minTermFreq(1).minDocFreq(1);
|
SearchHit[] hitsDSL = mltResponseDSL.getHits().hits();
|
||||||
SearchResponse mltResponseAPI = client.moreLikeThis(mltRequest).actionGet();
|
SearchHit[] hitsAPI = mltResponseAPI.getHits().hits();
|
||||||
assertSearchResponse(mltResponseAPI);
|
|
||||||
|
|
||||||
logger.info("Ensure the documents and scores returned are the same.");
|
// we have to resort since the results might come from
|
||||||
SearchHit[] hitsDSL = mltResponseDSL.getHits().hits();
|
// different shards and docIDs that are used for tie-breaking might not be the same on the shards
|
||||||
SearchHit[] hitsAPI = mltResponseAPI.getHits().hits();
|
Comparator<SearchHit> cmp = new Comparator<SearchHit>() {
|
||||||
|
|
||||||
// we have to resort since the results might come from
|
@Override
|
||||||
// different shards and docIDs that are used for tie-breaking might not be the same on the shards
|
public int compare(SearchHit o1, SearchHit o2) {
|
||||||
Comparator<SearchHit> cmp = new Comparator<SearchHit>() {
|
if (Float.compare(o1.getScore(), o2.getScore()) == 0) {
|
||||||
|
return o1.getId().compareTo(o2.getId());
|
||||||
@Override
|
}
|
||||||
public int compare(SearchHit o1, SearchHit o2) {
|
return Float.compare(o1.getScore(), o2.getScore());
|
||||||
if (Float.compare(o1.getScore(), o2.getScore()) == 0) {
|
|
||||||
return o1.getId().compareTo(o2.getId());
|
|
||||||
}
|
}
|
||||||
return Float.compare(o1.getScore(), o2.getScore());
|
};
|
||||||
|
ArrayUtil.timSort(hitsDSL, cmp);
|
||||||
|
ArrayUtil.timSort(hitsAPI, cmp);
|
||||||
|
assertThat("Not the same number of results.", hitsAPI.length, equalTo(hitsDSL.length));
|
||||||
|
for (int i = 0; i < hitsDSL.length; i++) {
|
||||||
|
assertThat("Expected id: " + hitsDSL[i].getId() + " at position " + i + " but wasn't.",
|
||||||
|
hitsAPI[i].getId(), equalTo(hitsDSL[i].getId()));
|
||||||
|
assertThat("Expected score: " + hitsDSL[i].getScore() + " at position " + i + " but wasn't.",
|
||||||
|
hitsAPI[i].getScore(), equalTo(hitsDSL[i].getScore()));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
ArrayUtil.timSort(hitsDSL, cmp);
|
|
||||||
ArrayUtil.timSort(hitsAPI, cmp);
|
|
||||||
assertThat("Not the same number of results.", hitsAPI.length, equalTo(hitsDSL.length));
|
|
||||||
for (int i = 0; i < hitsDSL.length; i++) {
|
|
||||||
assertThat("Expected id: " + hitsDSL[i].getId() + " at position " + i + " but wasn't.",
|
|
||||||
hitsAPI[i].getId(), equalTo(hitsDSL[i].getId()));
|
|
||||||
assertThat("Expected score: " + hitsDSL[i].getScore() + " at position " + i + " but wasn't.",
|
|
||||||
hitsAPI[i].getScore(), equalTo(hitsDSL[i].getScore()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue