Skip to content

Commit

Permalink
Support postings highlighter in percolate api.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
martijnvg committed Dec 9, 2013
1 parent d2ee22f commit 1b481ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/elasticsearch/search/fetch/FetchSubPhase.java
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -44,6 +45,7 @@ public static class HitContext {
private int docId;
private FieldsVisitor fieldVisitor;
private Map<String, Object> cache;
private IndexSearcher atomicIndexSearcher;

public void reset(InternalSearchHit hit, AtomicReaderContext context, int docId, IndexReader topLevelReader, int topLevelDocId, FieldsVisitor fieldVisitor) {
this.hit = hit;
Expand All @@ -52,6 +54,7 @@ public void reset(InternalSearchHit hit, AtomicReaderContext context, int docId,
this.topLevelReader = topLevelReader;
this.topLevelDocId = topLevelDocId;
this.fieldVisitor = fieldVisitor;
this.atomicIndexSearcher = null;
}

public InternalSearchHit hit() {
Expand All @@ -66,6 +69,13 @@ public AtomicReaderContext readerContext() {
return readerContext;
}

public IndexSearcher searcher() {
if (atomicIndexSearcher == null) {
atomicIndexSearcher = new IndexSearcher(readerContext);
}
return atomicIndexSearcher;
}

public int docId() {
return docId;
}
Expand Down
Expand Up @@ -22,10 +22,7 @@
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;
Expand Down Expand Up @@ -109,7 +106,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
//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())) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/elasticsearch/percolator/PercolatorTests.java
Expand Up @@ -1314,6 +1314,19 @@ public void testPercolatorWithHighlighting() throws Exception {
.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");
Expand Down

0 comments on commit 1b481ef

Please sign in to comment.