Added simple test for minScore

This commit is contained in:
Luca Cavanna 2013-11-29 17:19:02 +01:00
parent bc393b6d79
commit de206e72f1
1 changed files with 53 additions and 47 deletions

View File

@ -54,6 +54,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.count.query.SimpleQueryTests.rangeFilter; import static org.elasticsearch.count.query.SimpleQueryTests.rangeFilter;
import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -2000,34 +2001,28 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
client().prepareIndex("index3", "type3").setId("3").setSource("text", "value3").get(); client().prepareIndex("index3", "type3").setId("3").setSource("text", "value3").get();
refresh(); refresh();
SearchResponse response = client().prepareSearch("index1", "index2", "index3") SearchResponse searchResponse = client().prepareSearch("index1", "index2", "index3")
.setQuery(indicesQuery(matchQuery("text", "value1"), "index1") .setQuery(indicesQuery(matchQuery("text", "value1"), "index1")
.noMatchQuery(matchQuery("text", "value2"))).get(); .noMatchQuery(matchQuery("text", "value2"))).get();
assertHitCount(response, 2l); assertHitCount(searchResponse, 2l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"))); assertSearchHits(searchResponse, "1", "2");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
//default no match query is match_all //default no match query is match_all
response = client().prepareSearch("index1", "index2", "index3") searchResponse = client().prepareSearch("index1", "index2", "index3")
.setQuery(indicesQuery(matchQuery("text", "value1"), "index1")).get(); .setQuery(indicesQuery(matchQuery("text", "value1"), "index1")).get();
assertHitCount(response, 3l); assertHitCount(searchResponse, 3l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3"))); assertSearchHits(searchResponse, "1", "2", "3");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3"))); searchResponse = client().prepareSearch("index1", "index2", "index3")
assertThat(response.getHits().getAt(2).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
response = client().prepareSearch("index1", "index2", "index3")
.setQuery(indicesQuery(matchQuery("text", "value1"), "index1") .setQuery(indicesQuery(matchQuery("text", "value1"), "index1")
.noMatchQuery("all")).get(); .noMatchQuery("all")).get();
assertHitCount(response, 3l); assertHitCount(searchResponse, 3l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3"))); assertSearchHits(searchResponse, "1", "2", "3");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
assertThat(response.getHits().getAt(2).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
response = client().prepareSearch("index1", "index2", "index3") searchResponse = client().prepareSearch("index1", "index2", "index3")
.setQuery(indicesQuery(matchQuery("text", "value1"), "index1") .setQuery(indicesQuery(matchQuery("text", "value1"), "index1")
.noMatchQuery("none")).get(); .noMatchQuery("none")).get();
assertHitCount(response, 1l); assertHitCount(searchResponse, 1l);
assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertFirstHit(searchResponse, hasId("1"));
} }
@Test @Test
@ -2040,34 +2035,29 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
client().prepareIndex("index3", "type3").setId("3").setSource("text", "value3").get(); client().prepareIndex("index3", "type3").setId("3").setSource("text", "value3").get();
refresh(); refresh();
SearchResponse response = client().prepareSearch("index1", "index2", "index3") SearchResponse searchResponse = client().prepareSearch("index1", "index2", "index3")
.setFilter(indicesFilter(termFilter("text", "value1"), "index1") .setFilter(indicesFilter(termFilter("text", "value1"), "index1")
.noMatchFilter(termFilter("text", "value2"))).get(); .noMatchFilter(termFilter("text", "value2"))).get();
assertHitCount(response, 2l); assertHitCount(searchResponse, 2l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"))); assertSearchHits(searchResponse, "1", "2");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
//default no match filter is "all" //default no match filter is "all"
response = client().prepareSearch("index1", "index2", "index3") searchResponse = client().prepareSearch("index1", "index2", "index3")
.setFilter(indicesFilter(termFilter("text", "value1"), "index1")).get(); .setFilter(indicesFilter(termFilter("text", "value1"), "index1")).get();
assertHitCount(response, 3l); assertHitCount(searchResponse, 3l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3"))); assertSearchHits(searchResponse, "1", "2", "3");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
assertThat(response.getHits().getAt(2).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
response = client().prepareSearch("index1", "index2", "index3") searchResponse = client().prepareSearch("index1", "index2", "index3")
.setFilter(indicesFilter(termFilter("text", "value1"), "index1") .setFilter(indicesFilter(termFilter("text", "value1"), "index1")
.noMatchFilter("all")).get(); .noMatchFilter("all")).get();
assertHitCount(response, 3l); assertHitCount(searchResponse, 3l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3"))); assertSearchHits(searchResponse, "1", "2", "3");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
assertThat(response.getHits().getAt(2).getId(), anyOf(equalTo("1"), equalTo("2"), equalTo("3")));
response = client().prepareSearch("index1", "index2", "index3") searchResponse = client().prepareSearch("index1", "index2", "index3")
.setFilter(indicesFilter(termFilter("text", "value1"), "index1") .setFilter(indicesFilter(termFilter("text", "value1"), "index1")
.noMatchFilter("none")).get(); .noMatchFilter("none")).get();
assertHitCount(response, 1l); assertHitCount(searchResponse, 1l);
assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertFirstHit(searchResponse, hasId("1"));
} }
@Test // https://github.com/elasticsearch/elasticsearch/issues/2416 @Test // https://github.com/elasticsearch/elasticsearch/issues/2416
@ -2075,8 +2065,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
createIndex("simple"); createIndex("simple");
client().admin().indices().prepareCreate("related") client().admin().indices().prepareCreate("related")
.addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent") .addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent")
.endObject().endObject().endObject()) .endObject().endObject().endObject()).get();
.get();
ensureGreen(); ensureGreen();
client().prepareIndex("simple", "lone").setId("1").setSource("text", "value1").get(); client().prepareIndex("simple", "lone").setId("1").setSource("text", "value1").get();
@ -2097,12 +2086,11 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
} }
//has_child doesn't get parsed for "simple" index //has_child doesn't get parsed for "simple" index
SearchResponse response = client().prepareSearch("related", "simple") SearchResponse searchResponse = client().prepareSearch("related", "simple")
.setQuery(indicesQuery(hasChildQuery("child", matchQuery("text", "value2")), "related") .setQuery(indicesQuery(hasChildQuery("child", matchQuery("text", "value2")), "related")
.noMatchQuery(matchQuery("text", "value1"))).get(); .noMatchQuery(matchQuery("text", "value1"))).get();
assertHitCount(response, 2l); assertHitCount(searchResponse, 2l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"))); assertSearchHits(searchResponse, "1", "2");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
} }
@Test // https://github.com/elasticsearch/elasticsearch/issues/2416 @Test // https://github.com/elasticsearch/elasticsearch/issues/2416
@ -2110,8 +2098,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
createIndex("simple"); createIndex("simple");
client().admin().indices().prepareCreate("related") client().admin().indices().prepareCreate("related")
.addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent") .addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent")
.endObject().endObject().endObject()) .endObject().endObject().endObject()).get();
.get();
ensureGreen(); ensureGreen();
client().prepareIndex("simple", "lone").setId("1").setSource("text", "value1").get(); client().prepareIndex("simple", "lone").setId("1").setSource("text", "value1").get();
@ -2131,11 +2118,30 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
} }
} }
SearchResponse response = client().prepareSearch("related", "simple") SearchResponse searchResponse = client().prepareSearch("related", "simple")
.setFilter(indicesFilter(hasChildFilter("child", termFilter("text", "value2")), "related") .setFilter(indicesFilter(hasChildFilter("child", termFilter("text", "value2")), "related")
.noMatchFilter(termFilter("text", "value1"))).get(); .noMatchFilter(termFilter("text", "value1"))).get();
assertHitCount(response, 2l); assertHitCount(searchResponse, 2l);
assertThat(response.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("2"))); assertSearchHits(searchResponse, "1", "2");
assertThat(response.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("2"))); }
@Test
public void testMinScore() {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "test", "1").setSource("score", 1.5).get();
client().prepareIndex("test", "test", "2").setSource("score", 1).get();
client().prepareIndex("test", "test", "3").setSource("score", 2).get();
client().prepareIndex("test", "test", "4").setSource("score", 0.5).get();
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(
functionScoreQuery(scriptFunction("_doc['score'].value"))).setMinScore(1.5f).get();
assertHitCount(searchResponse, 2);
assertFirstHit(searchResponse, hasId("3"));
assertSecondHit(searchResponse, hasId("1"));
} }
} }