[TEST] speed up HighlightSearchTests a bit

Randomize rewrite methods instead of trying them all when highlighting multi term queries with postings highlighter
Rely on search type randomization and remove all the explicit setSearchType calls as they are not needed anymore
Remove explicit `.from`, `.size` and `.explain`, not needed and might slow tests down (especially explain)
This commit is contained in:
javanna 2014-05-31 16:29:06 +02:00 committed by Luca Cavanna
parent 01ca8491cf
commit e8995ecaa7
1 changed files with 65 additions and 135 deletions

View File

@ -26,7 +26,6 @@ import org.elasticsearch.ElasticsearchException;
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.common.settings.ImmutableSettings.Builder; import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -46,7 +45,6 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH;
import static org.elasticsearch.client.Requests.searchRequest; import static org.elasticsearch.client.Requests.searchRequest;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -493,12 +491,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1 and field2 produces different tags"); logger.info("--> highlighting and searching on field1 and field2 produces different tags");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(termQuery("field1", "test")) .query(termQuery("field1", "test"))
.from(0).size(60).explain(true)
.highlight(highlight().order("score").preTags("<global>").postTags("</global>").fragmentSize(1).numOfFragments(1) .highlight(highlight().order("score").preTags("<global>").postTags("</global>").fragmentSize(1).numOfFragments(1)
.field(new HighlightBuilder.Field("field1").numOfFragments(2)) .field(new HighlightBuilder.Field("field1").numOfFragments(2))
.field(new HighlightBuilder.Field("field2").preTags("<field2>").postTags("</field2>").fragmentSize(50))); .field(new HighlightBuilder.Field("field2").preTags("<field2>").postTags("</field2>").fragmentSize(50)));
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field1", 0, 2, equalTo(" <global>test</global>")); assertHighlight(searchResponse, 0, "field1", 0, 2, equalTo(" <global>test</global>"));
assertHighlight(searchResponse, 0, "field1", 1, 2, equalTo(" <global>test</global>")); assertHighlight(searchResponse, 0, "field1", 1, 2, equalTo(" <global>test</global>"));
@ -599,60 +596,54 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(termQuery("field1", "test")) .query(termQuery("field1", "test"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field1").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field1").order("score").preTags("<xxx>").postTags("</xxx>"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>"));
logger.info("--> searching on _all, highlighting on field1"); logger.info("--> searching on _all, highlighting on field1");
source = searchSource() source = searchSource()
.query(termQuery("_all", "test")) .query(termQuery("_all", "test"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field1").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field1").order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>"));
logger.info("--> searching on _all, highlighting on field2"); logger.info("--> searching on _all, highlighting on field2");
source = searchSource() source = searchSource()
.query(termQuery("_all", "quick")) .query(termQuery("_all", "quick"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
logger.info("--> searching on _all, highlighting on field2"); logger.info("--> searching on _all, highlighting on field2");
source = searchSource() source = searchSource()
.query(prefixQuery("_all", "qui")) .query(prefixQuery("_all", "qui"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
logger.info("--> searching on _all with constant score, highlighting on field2"); logger.info("--> searching on _all with constant score, highlighting on field2");
source = searchSource() source = searchSource()
.query(constantScoreQuery(prefixQuery("_all", "qui"))) .query(constantScoreQuery(prefixQuery("_all", "qui")))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
logger.info("--> searching on _all with constant score, highlighting on field2"); logger.info("--> searching on _all with constant score, highlighting on field2");
source = searchSource() source = searchSource()
.query(boolQuery().should(constantScoreQuery(prefixQuery("_all", "qui")))) .query(boolQuery().should(constantScoreQuery(prefixQuery("_all", "qui"))))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2").order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
} }
@ -700,20 +691,18 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(termQuery("field1", "test")) .query(termQuery("field1", "test"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field1", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field1", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>"));
logger.info("--> searching on _all, highlighting on field1"); logger.info("--> searching on _all, highlighting on field1");
source = searchSource() source = searchSource()
.query(termQuery("_all", "test")) .query(termQuery("_all", "test"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field1", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field1", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
// LUCENE 3.1 UPGRADE: Caused adding the space at the end... // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>"));
@ -721,10 +710,9 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> searching on _all, highlighting on field2"); logger.info("--> searching on _all, highlighting on field2");
source = searchSource() source = searchSource()
.query(termQuery("_all", "quick")) .query(termQuery("_all", "quick"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
// LUCENE 3.1 UPGRADE: Caused adding the space at the end... // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
@ -732,10 +720,9 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> searching on _all, highlighting on field2"); logger.info("--> searching on _all, highlighting on field2");
source = searchSource() source = searchSource()
.query(prefixQuery("_all", "qui")) .query(prefixQuery("_all", "qui"))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("field2", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>"));
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
// LUCENE 3.1 UPGRADE: Caused adding the space at the end... // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
@ -948,18 +935,6 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
assertHighlight(searchResponse, i, "field1", 0, 1, equalTo("<em>test</em> " + hit.id())); assertHighlight(searchResponse, i, "field1", 0, 1, equalTo("<em>test</em> " + hit.id()));
} }
logger.info("--> searching explicitly on field1 and highlighting on it, with DFS");
searchResponse = client().prepareSearch()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(COUNT)
.setQuery(termQuery("field1", "test"))
.addHighlightedField("field1", 100, 0)
.get();
for (int i = 0; i < COUNT; i++) {
SearchHit hit = searchResponse.getHits().getHits()[i];
assertHighlight(searchResponse, i, "field1", 0, 1, equalTo("<em>test</em> " + hit.id()));
}
logger.info("--> searching explicitly _all and highlighting on _all"); logger.info("--> searching explicitly _all and highlighting on _all");
searchResponse = client().prepareSearch() searchResponse = client().prepareSearch()
.setSize(COUNT) .setSize(COUNT)
@ -1316,10 +1291,9 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f)) .query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>")); .highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog"));
} }
@ -1335,11 +1309,9 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f)) .query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>")); .highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>"));
SearchResponse searchResponse = client().search( SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog"));
} }
@ -1357,10 +1329,9 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(commonTerms("field2", "quick brown").cutoffFrequency(100)) .query(commonTerms("field2", "quick brown").cutoffFrequency(100))
.from(0).size(60).explain(true)
.highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>")); .highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet(); SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"));
} }
@ -1372,11 +1343,10 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog").get(); client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog").get();
refresh(); refresh();
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource().query(commonTerms("field2", "quick brown").cutoffFrequency(100)).from(0).size(60) SearchSourceBuilder source = searchSource().query(commonTerms("field2", "quick brown").cutoffFrequency(100))
.explain(true).highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>")); .highlight(highlight().field("field2").order("score").preTags("<x>").postTags("</x>"));
SearchResponse searchResponse = client().search( SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"));
} }
@ -2371,13 +2341,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(prefixQuery("field2", "qui").rewrite(randomFrom(REWRITE_METHODS)))
SearchSourceBuilder source = searchSource().query(prefixQuery("field2", "qui").rewrite(rewriteMethod))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
}
} }
@Test @Test
@ -2390,8 +2358,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
SearchSourceBuilder source = searchSource().query(fuzzyQuery("field2", "quck")) SearchSourceBuilder source = searchSource().query(fuzzyQuery("field2", "quck"))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
} }
@ -2404,15 +2371,12 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get(); client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
refresh(); refresh();
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(regexpQuery("field2", "qu[a-l]+k").rewrite(randomFrom(REWRITE_METHODS)))
SearchSourceBuilder source = searchSource().query(regexpQuery("field2", "qu[a-l]+k").rewrite(rewriteMethod))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
} }
}
@Test @Test
public void testPostingsHighlighterWildcardQuery() throws Exception { public void testPostingsHighlighterWildcardQuery() throws Exception {
@ -2422,23 +2386,19 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get(); client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
refresh(); refresh();
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(wildcardQuery("field2", "qui*").rewrite(randomFrom(REWRITE_METHODS)))
SearchSourceBuilder source = searchSource().query(wildcardQuery("field2", "qui*").rewrite(rewriteMethod))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
source = searchSource().query(wildcardQuery("field2", "qu*k").rewrite(rewriteMethod)) source = searchSource().query(wildcardQuery("field2", "qu*k").rewrite(randomFrom(REWRITE_METHODS)))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
searchResponse = client().search(searchRequest("test").source(source) searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHitCount(searchResponse, 1l); assertHitCount(searchResponse, 1l);
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
} }
}
@Test @Test
public void testPostingsHighlighterTermRangeQuery() throws Exception { public void testPostingsHighlighterTermRangeQuery() throws Exception {
@ -2450,8 +2410,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
SearchSourceBuilder source = searchSource().query(rangeQuery("field2").gte("aaaa").lt("zzzz")) SearchSourceBuilder source = searchSource().query(rangeQuery("field2").gte("aaaa").lt("zzzz"))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("<em>aaab</em>")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("<em>aaab</em>"));
} }
@ -2464,15 +2423,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get(); client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
refresh(); refresh();
logger.info("--> highlighting and searching on field2"); logger.info("--> highlighting and searching on field2");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(queryString("qui*").defaultField("field2").rewrite(randomFrom(REWRITE_METHODS)))
SearchSourceBuilder source = searchSource().query(queryString("qui*").defaultField("field2").rewrite(rewriteMethod))
.highlight(highlight().field("field2")); .highlight(highlight().field("field2"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!")); assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
} }
}
@Test @Test
public void testPostingsHighlighterRegexpQueryWithinConstantScoreQuery() throws Exception { public void testPostingsHighlighterRegexpQueryWithinConstantScoreQuery() throws Exception {
@ -2484,14 +2439,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(constantScoreQuery(regexpQuery("field1", "pho[a-z]+").rewrite(randomFrom(REWRITE_METHODS))))
SearchSourceBuilder source = searchSource().query(constantScoreQuery(regexpQuery("field1", "pho[a-z]+").rewrite(rewriteMethod)))
.highlight(highlight().field("field1")); .highlight(highlight().field("field1"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
} }
}
@Test @Test
public void testPostingsHighlighterMultiTermQueryMultipleLevels() throws Exception { public void testPostingsHighlighterMultiTermQueryMultipleLevels() throws Exception {
@ -2503,17 +2455,14 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
for (String rewriteMethod : REWRITE_METHODS) {
SearchSourceBuilder source = searchSource().query(boolQuery() SearchSourceBuilder source = searchSource().query(boolQuery()
.should(constantScoreQuery(FilterBuilders.missingFilter("field1"))) .should(constantScoreQuery(FilterBuilders.missingFilter("field1")))
.should(matchQuery("field1", "test")) .should(matchQuery("field1", "test"))
.should(filteredQuery(queryString("field1:photo*").rewrite(rewriteMethod), null))) .should(filteredQuery(queryString("field1:photo*").rewrite(randomFrom(REWRITE_METHODS)), null)))
.highlight(highlight().field("field1")); .highlight(highlight().field("field1"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
} }
}
@Test @Test
public void testPostingsHighlighterPrefixQueryWithinBooleanQuery() throws Exception { public void testPostingsHighlighterPrefixQueryWithinBooleanQuery() throws Exception {
@ -2525,14 +2474,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(boolQuery().must(prefixQuery("field1", "photo").rewrite(randomFrom(REWRITE_METHODS))).should(matchQuery("field1", "test").minimumShouldMatch("0")))
SearchSourceBuilder source = searchSource().query(boolQuery().must(prefixQuery("field1", "photo").rewrite(rewriteMethod)).should(matchQuery("field1", "test").minimumShouldMatch("0")))
.highlight(highlight().field("field1")); .highlight(highlight().field("field1"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
} }
}
@Test @Test
public void testPostingsHighlighterQueryStringWithinFilteredQuery() throws Exception { public void testPostingsHighlighterQueryStringWithinFilteredQuery() throws Exception {
@ -2544,14 +2490,11 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
logger.info("--> highlighting and searching on field1"); logger.info("--> highlighting and searching on field1");
for (String rewriteMethod : REWRITE_METHODS) { SearchSourceBuilder source = searchSource().query(filteredQuery(queryString("field1:photo*").rewrite(randomFrom(REWRITE_METHODS)), missingFilter("field_null")))
SearchSourceBuilder source = searchSource().query(filteredQuery(queryString("field1:photo*").rewrite(rewriteMethod), missingFilter("field_null")))
.highlight(highlight().field("field1")); .highlight(highlight().field("field1"));
SearchResponse searchResponse = client().search(searchRequest("test").source(source) SearchResponse searchResponse = client().prepareSearch("test").setSource(source.buildAsBytes()).get();
.searchType(randomBoolean() ? SearchType.DFS_QUERY_THEN_FETCH : SearchType.QUERY_THEN_FETCH)).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted")); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
} }
}
@Test @Test
@Slow @Slow
@ -2575,25 +2518,12 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
logger.info("--> searching explicitly on field1 and highlighting on it"); logger.info("--> searching explicitly on field1 and highlighting on it");
SearchResponse searchResponse = client().prepareSearch() SearchRequestBuilder searchRequestBuilder = client().prepareSearch()
.setSize(COUNT) .setSize(COUNT)
.setQuery(termQuery("field1", "test")) .setQuery(termQuery("field1", "test"))
.addHighlightedField("field1") .addHighlightedField("field1");
.get(); SearchResponse searchResponse =
assertHitCount(searchResponse, (long)COUNT); searchRequestBuilder.get();
assertThat(searchResponse.getHits().hits().length, equalTo(COUNT));
for (SearchHit hit : searchResponse.getHits()) {
String prefix = prefixes.get(hit.id());
assertHighlight(hit, "field1", 0, 1, equalTo("Sentence " + prefix + " <em>test</em>."));
}
logger.info("--> searching explicitly on field1 and highlighting on it, with DFS");
searchResponse = client().prepareSearch()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(COUNT)
.setQuery(termQuery("field1", "test"))
.addHighlightedField("field1")
.get();
assertHitCount(searchResponse, (long)COUNT); assertHitCount(searchResponse, (long)COUNT);
assertThat(searchResponse.getHits().hits().length, equalTo(COUNT)); assertThat(searchResponse.getHits().hits().length, equalTo(COUNT));
for (SearchHit hit : searchResponse.getHits()) { for (SearchHit hit : searchResponse.getHits()) {
@ -2730,7 +2660,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
private <P extends QueryBuilder & BoostableQueryBuilder<?>> void private <P extends QueryBuilder & BoostableQueryBuilder<?>> void
phraseBoostTestCaseForClauses(String highlighterType, float boost, QueryBuilder terms, P phrase) { phraseBoostTestCaseForClauses(String highlighterType, float boost, QueryBuilder terms, P phrase) {
Matcher<String> highlightedMatcher = Matchers.<String>either(containsString("<em>highlight words together</em>")).or( Matcher<String> highlightedMatcher = Matchers.either(containsString("<em>highlight words together</em>")).or(
containsString("<em>highlight</em> <em>words</em> <em>together</em>")); containsString("<em>highlight</em> <em>words</em> <em>together</em>"));
SearchRequestBuilder search = client().prepareSearch("test").setHighlighterRequireFieldMatch(true) SearchRequestBuilder search = client().prepareSearch("test").setHighlighterRequireFieldMatch(true)
.setHighlighterOrder("score").setHighlighterType(highlighterType) .setHighlighterOrder("score").setHighlighterType(highlighterType)