|
19 | 19 |
|
20 | 20 | package org.elasticsearch.test.integration.search.highlight;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.ElasticSearchException; |
22 | 23 | import org.elasticsearch.action.search.SearchResponse;
|
| 24 | +import org.elasticsearch.action.search.SearchType; |
23 | 25 | import org.elasticsearch.client.Client;
|
24 | 26 | import org.elasticsearch.common.settings.ImmutableSettings;
|
25 | 27 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
@@ -190,6 +192,65 @@ protected Client getClient() {
|
190 | 192 | assertThat(searchResponse.hits().getAt(0).highlightFields().get("field2").fragments()[0], equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
|
191 | 193 | }
|
192 | 194 |
|
| 195 | + @Test public void testFastVectorHighlighterManyDocs() throws Exception { |
| 196 | + try { |
| 197 | + client.admin().indices().prepareDelete("test").execute().actionGet(); |
| 198 | + } catch (ElasticSearchException e) { |
| 199 | + assertThat(e.unwrapCause(), instanceOf(IndexMissingException.class)); |
| 200 | + } |
| 201 | + client.admin().indices().prepareCreate("test").addMapping("type1", type1TermVectorMapping()).execute().actionGet(); |
| 202 | + client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); |
| 203 | + |
| 204 | + int COUNT = 100; |
| 205 | + logger.info("--> indexing docs"); |
| 206 | + for (int i = 0; i < COUNT; i++) { |
| 207 | + client.prepareIndex("test", "type1", Integer.toString(i)).setSource("field1", "test " + i).execute().actionGet(); |
| 208 | + if (i % 5 == 0) { |
| 209 | + // flush so we get updated readers and segmented readers |
| 210 | + client.admin().indices().prepareFlush().execute().actionGet(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + client.admin().indices().prepareRefresh().execute().actionGet(); |
| 215 | + |
| 216 | + logger.info("--> searching explicitly on field1 and highlighting on it"); |
| 217 | + SearchResponse searchResponse = client.prepareSearch() |
| 218 | + .setSize(COUNT) |
| 219 | + .setQuery(termQuery("field1", "test")) |
| 220 | + .addHighlightedField("field1", 100, 0) |
| 221 | + .execute().actionGet(); |
| 222 | + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); |
| 223 | + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); |
| 224 | + for (SearchHit hit : searchResponse.hits()) { |
| 225 | + assertThat(hit.highlightFields().get("field1").fragments()[0], equalTo("<em>test</em> " + hit.id())); |
| 226 | + } |
| 227 | + |
| 228 | + logger.info("--> searching explicitly on field1 and highlighting on it, with DFS"); |
| 229 | + searchResponse = client.prepareSearch() |
| 230 | + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) |
| 231 | + .setSize(COUNT) |
| 232 | + .setQuery(termQuery("field1", "test")) |
| 233 | + .addHighlightedField("field1", 100, 0) |
| 234 | + .execute().actionGet(); |
| 235 | + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); |
| 236 | + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); |
| 237 | + for (SearchHit hit : searchResponse.hits()) { |
| 238 | + assertThat(hit.highlightFields().get("field1").fragments()[0], equalTo("<em>test</em> " + hit.id())); |
| 239 | + } |
| 240 | + |
| 241 | + logger.info("--> searching explicitly _all and highlighting on _all"); |
| 242 | + searchResponse = client.prepareSearch() |
| 243 | + .setSize(COUNT) |
| 244 | + .setQuery(termQuery("_all", "test")) |
| 245 | + .addHighlightedField("_all", 100, 0) |
| 246 | + .execute().actionGet(); |
| 247 | + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); |
| 248 | + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); |
| 249 | + for (SearchHit hit : searchResponse.hits()) { |
| 250 | + assertThat(hit.highlightFields().get("_all").fragments()[0], equalTo("<em>test</em> " + hit.id() + " ")); |
| 251 | + } |
| 252 | + } |
| 253 | + |
193 | 254 | public XContentBuilder type1TermVectorMapping() throws IOException {
|
194 | 255 | return XContentFactory.jsonBuilder().startObject().startObject("type1")
|
195 | 256 | .startObject("_all").field("store", "yes").field("termVector", "with_positions_offsets").endObject()
|
|
0 commit comments