[TEST] Add tests that combines highlighting and NOW parsing

Relates to #21295
This commit is contained in:
Simon Willnauer 2016-11-03 11:22:18 +01:00
parent 6902eafbfe
commit 1fb8723323
1 changed files with 36 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import org.elasticsearch.Version;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
@ -40,15 +41,19 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.search.MatchQuery; import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.indices.IndicesRequestCache;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.test.InternalSettingsPlugin;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
@ -84,6 +89,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHigh
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNotHighlighted; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNotHighlighted;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.elasticsearch.test.hamcrest.RegexMatcher.matches; import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
@ -2948,4 +2954,34 @@ public class HighlighterSearchIT extends ESIntegTestCase {
equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog")); equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog"));
} }
} }
public void testHighlightQueryRewriteDatesWithNow() throws Exception {
assertAcked(client().admin().indices().prepareCreate("index-1").addMapping("type", "d", "type=date",
"field", "type=text,store=true,term_vector=with_positions_offsets")
.setSettings("index.number_of_replicas", 0, "index.number_of_shards", 2)
.get());
DateTime now = new DateTime(ISOChronology.getInstanceUTC());
indexRandom(true, client().prepareIndex("index-1", "type", "1").setSource("d", now, "field", "hello world"),
client().prepareIndex("index-1", "type", "2").setSource("d", now.minusDays(1), "field", "hello"),
client().prepareIndex("index-1", "type", "3").setSource("d", now.minusDays(2), "field", "world"));
ensureSearchable("index-1");
for (int i = 0; i < 5; i++) {
final SearchResponse r1 = client().prepareSearch("index-1")
.addSort("d", SortOrder.DESC)
.setTrackScores(true)
.highlighter(highlight()
.field("field")
.preTags("<x>")
.postTags("</x>")
).setQuery(QueryBuilders.boolQuery().must(
QueryBuilders.rangeQuery("d").gte("now-7d/d").lte("now").includeLower(true).includeUpper(true).boost(1.0f))
.should(QueryBuilders.termQuery("field", "hello")))
.get();
assertSearchResponse(r1);
assertThat(r1.getHits().getTotalHits(), equalTo(3L));
assertHighlight(r1, 0, "field", 0, 1,
equalTo("<x>hello</x> world"));
}
}
} }