Skip to content

Commit

Permalink
Highlighting: keep track of the original query only in HighlighterCon…
Browse files Browse the repository at this point in the history
…text

We used to keep track of the rewritten query in the highlighter context to support custom rewriting done by our own postings highlighter fork. Now that we rely on lucene implementation, no rewrite happens, we can simply keep track of the original query and simplify code around it.

Closes #11317
  • Loading branch information
javanna committed May 26, 2015
1 parent 802b7b8 commit 543f572
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 39 deletions.
Expand Up @@ -79,13 +79,13 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
if (field.fieldOptions().requireFieldMatch()) {
if (cache.fieldMatchFieldQuery == null) {
// we use top level reader to rewrite the query against all readers, with use caching it across hits (and across readers...)
cache.fieldMatchFieldQuery = new CustomFieldQuery(highlighterContext.query.originalQuery(), hitContext.topLevelReader(), true, field.fieldOptions().requireFieldMatch());
cache.fieldMatchFieldQuery = new CustomFieldQuery(highlighterContext.query, hitContext.topLevelReader(), true, field.fieldOptions().requireFieldMatch());
}
fieldQuery = cache.fieldMatchFieldQuery;
} else {
if (cache.noFieldMatchFieldQuery == null) {
// we use top level reader to rewrite the query against all readers, with use caching it across hits (and across readers...)
cache.noFieldMatchFieldQuery = new CustomFieldQuery(highlighterContext.query.originalQuery(), hitContext.topLevelReader(), true, field.fieldOptions().requireFieldMatch());
cache.noFieldMatchFieldQuery = new CustomFieldQuery(highlighterContext.query, hitContext.topLevelReader(), true, field.fieldOptions().requireFieldMatch());
}
fieldQuery = cache.noFieldMatchFieldQuery;
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex;
Expand Down Expand Up @@ -113,12 +114,7 @@ public void hitExecute(SearchContext context, HitContext hitContext) {
throw new IllegalArgumentException("unknown highlighter type [" + highlighterType + "] for the field [" + fieldName + "]");
}

HighlighterContext.HighlightQuery highlightQuery;
if (field.fieldOptions().highlightQuery() == null) {
highlightQuery = new HighlighterContext.HighlightQuery(context.parsedQuery().query(), context.query(), context.queryRewritten());
} else {
highlightQuery = new HighlighterContext.HighlightQuery(field.fieldOptions().highlightQuery(), field.fieldOptions().highlightQuery(), false);
}
Query highlightQuery = field.fieldOptions().highlightQuery() == null ? context.parsedQuery().query() : field.fieldOptions().highlightQuery();
HighlighterContext highlighterContext = new HighlighterContext(fieldName, field, fieldMapper, context, hitContext, highlightQuery);
HighlightField highlightField = highlighter.highlight(highlighterContext);
if (highlightField != null) {
Expand Down
Expand Up @@ -18,7 +18,6 @@
*/
package org.elasticsearch.search.highlight;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.search.fetch.FetchSubPhase;
Expand All @@ -34,39 +33,15 @@ public class HighlighterContext {
public final FieldMapper mapper;
public final SearchContext context;
public final FetchSubPhase.HitContext hitContext;
public final HighlightQuery query;
public final Query query;

public HighlighterContext(String fieldName, SearchContextHighlight.Field field, FieldMapper mapper, SearchContext context,
FetchSubPhase.HitContext hitContext, HighlightQuery query) {
FetchSubPhase.HitContext hitContext, Query query) {
this.fieldName = fieldName;
this.field = field;
this.mapper = mapper;
this.context = context;
this.hitContext = hitContext;
this.query = query;
}

public static class HighlightQuery {
private final Query originalQuery;
private final Query query;
private final boolean queryRewritten;

protected HighlightQuery(Query originalQuery, Query query, boolean queryRewritten) {
this.originalQuery = originalQuery;
this.query = query;
this.queryRewritten = queryRewritten;
}

public boolean queryRewritten() {
return queryRewritten;
}

public Query originalQuery() {
return originalQuery;
}

public Query query() {
return query;
}
}
}
Expand Up @@ -23,7 +23,6 @@
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.common.text.StringText;
Expand Down Expand Up @@ -69,8 +68,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {

org.apache.lucene.search.highlight.Highlighter entry = cache.get(mapper);
if (entry == null) {
Query query = highlighterContext.query.originalQuery();
QueryScorer queryScorer = new CustomQueryScorer(query, field.fieldOptions().requireFieldMatch() ? mapper.names().indexName() : null);
QueryScorer queryScorer = new CustomQueryScorer(highlighterContext.query, field.fieldOptions().requireFieldMatch() ? mapper.names().indexName() : null);
queryScorer.setExpandMultiTermQuery(true);
Fragmenter fragmenter;
if (field.fieldOptions().numberOfFragments() == 0) {
Expand Down
Expand Up @@ -91,7 +91,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
}

IndexSearcher searcher = new IndexSearcher(hitContext.reader());
Snippet[] fieldSnippets = highlighter.highlightField(fieldMapper.names().indexName(), highlighterContext.query.originalQuery(), searcher, hitContext.docId(), numberOfFragments);
Snippet[] fieldSnippets = highlighter.highlightField(fieldMapper.names().indexName(), highlighterContext.query, searcher, hitContext.docId(), numberOfFragments);
for (Snippet fieldSnippet : fieldSnippets) {
if (Strings.hasText(fieldSnippet.getText())) {
snippets.add(fieldSnippet);
Expand Down

0 comments on commit 543f572

Please sign in to comment.