From 31231531e117e257d7a2dcdccf89b5f179cde8a6 Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 5 Jan 2011 15:45:46 +0200 Subject: [PATCH] Highlighting: The result of highlighting for a hit can contain data from another document, closes #600. --- .../search/highlight/HighlightPhase.java | 2 +- .../highlight/HighlighterSearchTests.java | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java index f86eadfb332..165f97312e7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/highlight/HighlightPhase.java @@ -155,7 +155,7 @@ public class HighlightPhase implements SearchHitPhase { try { // a HACK to make highlighter do highlighting, even though its using the single frag list builder int numberOfFragments = field.numberOfFragments() == 0 ? 1 : field.numberOfFragments(); - fragments = highlighter.getBestFragments(fieldQuery, context.searcher().getIndexReader(), docId, mapper.names().indexName(), field.fragmentCharSize(), numberOfFragments); + fragments = highlighter.getBestFragments(fieldQuery, reader, docId, mapper.names().indexName(), field.fragmentCharSize(), numberOfFragments); } catch (IOException e) { throw new FetchPhaseExecutionException(context, "Failed to highlight field [" + field.field() + "]", e); } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java index c4d919e3cf6..59163fb4b47 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java @@ -19,7 +19,9 @@ package org.elasticsearch.test.integration.search.highlight; +import org.elasticsearch.ElasticSearchException; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -190,6 +192,65 @@ public class HighlighterSearchTests extends AbstractNodesTests { assertThat(searchResponse.hits().getAt(0).highlightFields().get("field2").fragments()[0], equalTo("The quick brown fox jumps over the lazy dog")); } + @Test public void testFastVectorHighlighterManyDocs() throws Exception { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (ElasticSearchException e) { + assertThat(e.unwrapCause(), instanceOf(IndexMissingException.class)); + } + client.admin().indices().prepareCreate("test").addMapping("type1", type1TermVectorMapping()).execute().actionGet(); + client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + + int COUNT = 100; + logger.info("--> indexing docs"); + for (int i = 0; i < COUNT; i++) { + client.prepareIndex("test", "type1", Integer.toString(i)).setSource("field1", "test " + i).execute().actionGet(); + if (i % 5 == 0) { + // flush so we get updated readers and segmented readers + client.admin().indices().prepareFlush().execute().actionGet(); + } + } + + client.admin().indices().prepareRefresh().execute().actionGet(); + + logger.info("--> searching explicitly on field1 and highlighting on it"); + SearchResponse searchResponse = client.prepareSearch() + .setSize(COUNT) + .setQuery(termQuery("field1", "test")) + .addHighlightedField("field1", 100, 0) + .execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); + for (SearchHit hit : searchResponse.hits()) { + assertThat(hit.highlightFields().get("field1").fragments()[0], equalTo("test " + 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) + .execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); + for (SearchHit hit : searchResponse.hits()) { + assertThat(hit.highlightFields().get("field1").fragments()[0], equalTo("test " + hit.id())); + } + + logger.info("--> searching explicitly _all and highlighting on _all"); + searchResponse = client.prepareSearch() + .setSize(COUNT) + .setQuery(termQuery("_all", "test")) + .addHighlightedField("_all", 100, 0) + .execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo((long) COUNT)); + assertThat(searchResponse.hits().hits().length, equalTo(COUNT)); + for (SearchHit hit : searchResponse.hits()) { + assertThat(hit.highlightFields().get("_all").fragments()[0], equalTo("test " + hit.id() + " ")); + } + } + public XContentBuilder type1TermVectorMapping() throws IOException { return XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("_all").field("store", "yes").field("termVector", "with_positions_offsets").endObject()