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:
parent
d2ee22f01c
commit
1b481ef395
|
@ -23,6 +23,7 @@ import com.google.common.collect.Maps;
|
||||||
import org.apache.lucene.index.AtomicReader;
|
import org.apache.lucene.index.AtomicReader;
|
||||||
import org.apache.lucene.index.AtomicReaderContext;
|
import org.apache.lucene.index.AtomicReaderContext;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.elasticsearch.ElasticSearchException;
|
import org.elasticsearch.ElasticSearchException;
|
||||||
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
|
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
|
||||||
import org.elasticsearch.search.SearchParseElement;
|
import org.elasticsearch.search.SearchParseElement;
|
||||||
|
@ -44,6 +45,7 @@ public interface FetchSubPhase {
|
||||||
private int docId;
|
private int docId;
|
||||||
private FieldsVisitor fieldVisitor;
|
private FieldsVisitor fieldVisitor;
|
||||||
private Map<String, Object> cache;
|
private Map<String, Object> cache;
|
||||||
|
private IndexSearcher atomicIndexSearcher;
|
||||||
|
|
||||||
public void reset(InternalSearchHit hit, AtomicReaderContext context, int docId, IndexReader topLevelReader, int topLevelDocId, FieldsVisitor fieldVisitor) {
|
public void reset(InternalSearchHit hit, AtomicReaderContext context, int docId, IndexReader topLevelReader, int topLevelDocId, FieldsVisitor fieldVisitor) {
|
||||||
this.hit = hit;
|
this.hit = hit;
|
||||||
|
@ -52,6 +54,7 @@ public interface FetchSubPhase {
|
||||||
this.topLevelReader = topLevelReader;
|
this.topLevelReader = topLevelReader;
|
||||||
this.topLevelDocId = topLevelDocId;
|
this.topLevelDocId = topLevelDocId;
|
||||||
this.fieldVisitor = fieldVisitor;
|
this.fieldVisitor = fieldVisitor;
|
||||||
|
this.atomicIndexSearcher = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InternalSearchHit hit() {
|
public InternalSearchHit hit() {
|
||||||
|
@ -66,6 +69,13 @@ public interface FetchSubPhase {
|
||||||
return readerContext;
|
return readerContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IndexSearcher searcher() {
|
||||||
|
if (atomicIndexSearcher == null) {
|
||||||
|
atomicIndexSearcher = new IndexSearcher(readerContext);
|
||||||
|
}
|
||||||
|
return atomicIndexSearcher;
|
||||||
|
}
|
||||||
|
|
||||||
public int docId() {
|
public int docId() {
|
||||||
return docId;
|
return docId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,7 @@ import com.google.common.collect.Maps;
|
||||||
import org.apache.lucene.index.FieldInfo;
|
import org.apache.lucene.index.FieldInfo;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.MultiTermQuery;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.search.Query;
|
|
||||||
import org.apache.lucene.search.ScoringRewrite;
|
|
||||||
import org.apache.lucene.search.TopTermsRewrite;
|
|
||||||
import org.apache.lucene.search.highlight.Encoder;
|
import org.apache.lucene.search.highlight.Encoder;
|
||||||
import org.apache.lucene.search.postingshighlight.CustomPassageFormatter;
|
import org.apache.lucene.search.postingshighlight.CustomPassageFormatter;
|
||||||
import org.apache.lucene.search.postingshighlight.CustomPostingsHighlighter;
|
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)
|
//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();
|
int values = mergeValues ? 1 : textsToHighlight.size();
|
||||||
for (int i = 0; i < values; i++) {
|
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) {
|
if (fieldSnippets != null) {
|
||||||
for (Snippet fieldSnippet : fieldSnippets) {
|
for (Snippet fieldSnippet : fieldSnippets) {
|
||||||
if (Strings.hasText(fieldSnippet.getText())) {
|
if (Strings.hasText(fieldSnippet.getText())) {
|
||||||
|
|
|
@ -1314,6 +1314,19 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
|
||||||
.endObject().endObject()
|
.endObject().endObject()
|
||||||
)
|
)
|
||||||
.execute().actionGet();
|
.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");
|
logger.info("--> register a queries");
|
||||||
|
|
Loading…
Reference in New Issue