From 1b481ef395dc40819c50a508acc5ec190a641964 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 9 Dec 2013 13:37:42 +0100 Subject: [PATCH] Support postings highlighter in percolate api. The postings hl now uses a searcher that only encapsulate the view of segment the document being highlighted is in, this should be better than using the top level engine searcher. Closes #4385 --- .../elasticsearch/search/fetch/FetchSubPhase.java | 10 ++++++++++ .../search/highlight/PostingsHighlighter.java | 7 ++----- .../elasticsearch/percolator/PercolatorTests.java | 13 +++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/fetch/FetchSubPhase.java b/src/main/java/org/elasticsearch/search/fetch/FetchSubPhase.java index 7141f5aac06..d1ac77f2540 100644 --- a/src/main/java/org/elasticsearch/search/fetch/FetchSubPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/FetchSubPhase.java @@ -23,6 +23,7 @@ import com.google.common.collect.Maps; import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.search.IndexSearcher; import org.elasticsearch.ElasticSearchException; import org.elasticsearch.index.fieldvisitor.FieldsVisitor; import org.elasticsearch.search.SearchParseElement; @@ -44,6 +45,7 @@ public interface FetchSubPhase { private int docId; private FieldsVisitor fieldVisitor; private Map cache; + private IndexSearcher atomicIndexSearcher; public void reset(InternalSearchHit hit, AtomicReaderContext context, int docId, IndexReader topLevelReader, int topLevelDocId, FieldsVisitor fieldVisitor) { this.hit = hit; @@ -52,6 +54,7 @@ public interface FetchSubPhase { this.topLevelReader = topLevelReader; this.topLevelDocId = topLevelDocId; this.fieldVisitor = fieldVisitor; + this.atomicIndexSearcher = null; } public InternalSearchHit hit() { @@ -66,6 +69,13 @@ public interface FetchSubPhase { return readerContext; } + public IndexSearcher searcher() { + if (atomicIndexSearcher == null) { + atomicIndexSearcher = new IndexSearcher(readerContext); + } + return atomicIndexSearcher; + } + public int docId() { return docId; } diff --git a/src/main/java/org/elasticsearch/search/highlight/PostingsHighlighter.java b/src/main/java/org/elasticsearch/search/highlight/PostingsHighlighter.java index b45f8b560a1..bbf3a17b944 100644 --- a/src/main/java/org/elasticsearch/search/highlight/PostingsHighlighter.java +++ b/src/main/java/org/elasticsearch/search/highlight/PostingsHighlighter.java @@ -22,10 +22,7 @@ import com.google.common.collect.Maps; import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; -import org.apache.lucene.search.MultiTermQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.ScoringRewrite; -import org.apache.lucene.search.TopTermsRewrite; +import org.apache.lucene.search.*; import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.postingshighlight.CustomPassageFormatter; import org.apache.lucene.search.postingshighlight.CustomPostingsHighlighter; @@ -109,7 +106,7 @@ public class PostingsHighlighter implements Highlighter { //we highlight every value separately calling the highlight method multiple times, only if we need to have back a snippet per value (whole value) int values = mergeValues ? 1 : textsToHighlight.size(); for (int i = 0; i < values; i++) { - Snippet[] fieldSnippets = highlighter.highlightDoc(fieldMapper.names().indexName(), mapperHighlighterEntry.filteredQueryTerms, context.searcher(), hitContext.topLevelDocId(), numberOfFragments); + Snippet[] fieldSnippets = highlighter.highlightDoc(fieldMapper.names().indexName(), mapperHighlighterEntry.filteredQueryTerms, hitContext.searcher(), hitContext.docId(), numberOfFragments); if (fieldSnippets != null) { for (Snippet fieldSnippet : fieldSnippets) { if (Strings.hasText(fieldSnippet.getText())) { diff --git a/src/test/java/org/elasticsearch/percolator/PercolatorTests.java b/src/test/java/org/elasticsearch/percolator/PercolatorTests.java index 2cf55ff2248..dfbc66d1cb7 100644 --- a/src/test/java/org/elasticsearch/percolator/PercolatorTests.java +++ b/src/test/java/org/elasticsearch/percolator/PercolatorTests.java @@ -1314,6 +1314,19 @@ public class PercolatorTests extends ElasticsearchIntegrationTest { .endObject().endObject() ) .execute().actionGet(); + } else if (randomBoolean()) { + // positions hl + client.admin().indices().preparePutMapping("test").setType("type") + .setSource( + jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("field1").field("type", "string") + .field("index_options", "offsets") + .endObject() + .endObject() + .endObject().endObject() + ) + .execute().actionGet(); } logger.info("--> register a queries");