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
This commit is contained in:
Martijn van Groningen 2013-12-09 13:37:42 +01:00
parent d2ee22f01c
commit 1b481ef395
3 changed files with 25 additions and 5 deletions

View File

@ -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<String, Object> 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;
}

View File

@ -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())) {

View File

@ -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");