test: replaced integration tests with unit test

This commit is contained in:
Martijn van Groningen 2016-02-15 21:58:17 +01:00
parent b17a92c911
commit 3290cfbd31
3 changed files with 22 additions and 47 deletions

View File

@ -355,6 +355,10 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
public Query getInnerQuery() {
return innerQuery;
}
public Similarity getSimilarity() {
return similarity;
}
}
@Override

View File

@ -28,17 +28,22 @@ import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.similarities.DFISimilarity;
import org.apache.lucene.search.similarities.PerFieldSimilarityWrapper;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.fielddata.IndexFieldDataService;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.script.Script.ScriptParseException;
import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder;
import org.elasticsearch.search.fetch.innerhits.InnerHitsContext;
@ -48,6 +53,7 @@ import org.elasticsearch.test.TestSearchContext;
import org.junit.BeforeClass;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.equalTo;
@ -58,8 +64,11 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
protected static final String PARENT_TYPE = "parent";
protected static final String CHILD_TYPE = "child";
private static String similarity;
@BeforeClass
public static void before() throws Exception {
similarity = randomFrom("classic", "BM25");
MapperService mapperService = queryShardContext().getMapperService();
mapperService.merge(PARENT_TYPE, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(PARENT_TYPE,
STRING_FIELD_NAME, "type=text",
@ -72,6 +81,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
mapperService.merge(CHILD_TYPE, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(CHILD_TYPE,
"_parent", "type=" + PARENT_TYPE,
STRING_FIELD_NAME, "type=text",
"custom_string", "type=text,similarity=" + similarity,
INT_FIELD_NAME, "type=integer",
DOUBLE_FIELD_NAME, "type=double",
BOOLEAN_FIELD_NAME, "type=boolean",
@ -300,4 +310,12 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
}
}
}
public void testNonDefaultSimilarity() throws Exception {
QueryShardContext shardContext = createShardContext();
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(CHILD_TYPE, new TermQueryBuilder("custom_string", "value"));
HasChildQueryBuilder.LateParsingQuery query = (HasChildQueryBuilder.LateParsingQuery) hasChildQueryBuilder.toQuery(shardContext);
Similarity expected = SimilarityService.BUILT_IN.get(similarity).apply(similarity, Settings.EMPTY).get();
assertThat(((PerFieldSimilarityWrapper) query.getSimilarity()).get("custom_string"), instanceOf(expected.getClass()));
}
}

View File

@ -1927,51 +1927,4 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"))).get();
assertSearchHits(searchResponse, "parent-id");
}
// Tests #16550
public void testHasChildWithNonDefaultGlobalSimilarity() {
assertAcked(prepareCreate("test").setSettings(settingsBuilder().put(indexSettings())
.put("index.similarity.default.type", "BM25"))
.addMapping("parent")
.addMapping("child", "_parent", "type=parent", "c_field", "type=string"));
ensureGreen();
verifyNonDefaultSimilarity();
}
// Tests #16550
public void testHasChildWithNonDefaultFieldSimilarity() {
assertAcked(prepareCreate("test")
.addMapping("parent")
.addMapping("child", "_parent", "type=parent", "c_field", "type=string,similarity=BM25"));
ensureGreen();
verifyNonDefaultSimilarity();
}
// Tests #16550
private void verifyNonDefaultSimilarity() {
client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").get();
client().prepareIndex("test", "child", "c1").setSource("c_field", "c_value").setParent("p1").get();
client().prepareIndex("test", "child", "c2").setSource("c_field", "c_value").setParent("p1").get();
refresh();
// baseline: sum of scores of matching child docs outside of has_child query
SearchResponse searchResponse = client().prepareSearch("test")
.setTypes("child")
.setQuery(matchQuery("c_field", "c_value"))
.get();
assertSearchHits(searchResponse, "c1", "c2");
Float childSum = (float) Arrays.asList(searchResponse.getHits().getHits())
.stream()
.mapToDouble(hit -> hit.getScore())
.sum();
// compare baseline to has_child with 'total' score_mode
searchResponse = client().prepareSearch("test")
.setQuery(hasChildQuery("child", matchQuery("c_field", "c_value")).scoreMode(ScoreMode.Total))
.get();
assertSearchHits(searchResponse, "p1");
assertThat(searchResponse.getHits().hits()[0].score(), equalTo(childSum));
}
}