Skip to content

Apache Lucene open-source search software

LUCENE-6558: fix highlighters to work with CustomScoreQuery

Michael McCandless625923e1f349
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f0b7a4e8a43..6b8c7bc0efe 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -116,6 +116,9 @@ Bug fixes
instance since this can prevent future improvements like a
disk-backed live docs (Adrien Grand, Mike McCandless)
+* LUCENE-6558: Highlighters now work with CustomScoreQuery (Cao Manh
+ Dat via Mike McCandless)
+
Changes in Runtime Behavior
* LUCENE-6501: The subreader structure in ParallelCompositeReader
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
index 25027322efe..8cb1f58eea8 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
@@ -32,6 +32,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.queries.CommonTermsQuery;
+import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
@@ -222,6 +223,8 @@ public class WeightedSpanTermExtractor {
}
} else if (query instanceof MatchAllDocsQuery) {
//nothing
+ } else if (query instanceof CustomScoreQuery){
+ extract(((CustomScoreQuery) query).getSubQuery(), terms);
} else {
Query origQuery = query;
if (query instanceof MultiTermQuery) {
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldQuery.java b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldQuery.java
index dbf72a13456..0b8abc30747 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldQuery.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldQuery.java
@@ -29,6 +29,7 @@ import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.CommonTermsQuery;
+import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
@@ -127,7 +128,12 @@ public class FieldQuery {
if (q != null) {
flatten( applyParentBoost( q, sourceQuery ), reader, flatQueries);
}
- } else if (reader != null){
+ } else if (sourceQuery instanceof CustomScoreQuery) {
+ final Query q = ((CustomScoreQuery) sourceQuery).getSubQuery();
+ if (q != null) {
+ flatten( applyParentBoost( q, sourceQuery ), reader, flatQueries);
+ }
+ } else if (reader != null) {
Query query = sourceQuery;
if (sourceQuery instanceof MultiTermQuery) {
MultiTermQuery copy = (MultiTermQuery) sourceQuery.clone();
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
index 686617e3bb2..17fd930e739 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
@@ -55,9 +55,12 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
+import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.StoredDocument;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.CommonTermsQuery;
+import org.apache.lucene.queries.CustomScoreProvider;
+import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
@@ -139,6 +142,28 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte
searcher.doc(docId).get(fieldName), analyzer, -1);
}
+ public void testCustomScoreQueryHighlight() throws Exception{
+ TermQuery termQuery = new TermQuery(new Term(FIELD_NAME, "very"));
+ CustomScoreQuery query = new CustomScoreQuery(termQuery);
+
+ searcher = newSearcher(reader);
+ TopDocs hits = searcher.search(query, 10);
+ assertEquals(2, hits.totalHits);
+ QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
+ Highlighter highlighter = new Highlighter(scorer);
+
+ final int docId0 = hits.scoreDocs[0].doc;
+ StoredDocument doc = searcher.doc(docId0);
+ String storedField = doc.get(FIELD_NAME);
+
+ TokenStream stream = getAnyTokenStream(FIELD_NAME, docId0);
+ Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
+ highlighter.setTextFragmenter(fragmenter);
+ String fragment = highlighter.getBestFragment(stream, storedField);
+ assertEquals("Hello this is a piece of text that is <B>very</B> long and contains too much preamble and the meat is really here which says kennedy has been shot", fragment);
+
+ }
+
public void testQueryScorerHits() throws Exception {
PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD_NAME, "very"));
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
index c9947c7b590..6b3e43589af 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
@@ -17,14 +17,6 @@ package org.apache.lucene.search.postingshighlight;
* limitations under the License.
*/
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
-import java.text.BreakIterator;
-import java.util.Arrays;
-import java.util.Map;
-
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
@@ -41,6 +33,7 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.StoredDocument;
import org.apache.lucene.index.Term;
+import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
@@ -54,6 +47,14 @@ import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.text.BreakIterator;
+import java.util.Arrays;
+import java.util.Map;
+
public class TestPostingsHighlighter extends LuceneTestCase {
public void testBasics() throws Exception {
@@ -1156,4 +1157,39 @@ public class TestPostingsHighlighter extends LuceneTestCase {
ir.close();
dir.close();
}
+
+ public void testCustomScoreQueryHighlight() throws Exception{
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
+ iwc.setMergePolicy(newLogMergePolicy());
+ RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);
+
+ FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
+ offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
+ Field body = new Field("body", "", offsetsType);
+ Document doc = new Document();
+ doc.add(body);
+
+ body.setStringValue("This piece of text refers to Kennedy at the beginning then has a longer piece of text that is very long in the middle and finally ends with another reference to Kennedy");
+ iw.addDocument(doc);
+
+ IndexReader ir = iw.getReader();
+ iw.close();
+
+ TermQuery termQuery = new TermQuery(new Term("body", "very"));
+ PostingsHighlighter highlighter = new PostingsHighlighter();
+ CustomScoreQuery query = new CustomScoreQuery(termQuery);
+
+ IndexSearcher searcher = newSearcher(ir);
+ TopDocs hits = searcher.search(query, 10);
+ assertEquals(1, hits.totalHits);
+
+ String snippets[] = highlighter.highlight("body", query, searcher, hits);
+ assertEquals(1, snippets.length);
+ assertEquals("This piece of text refers to Kennedy at the beginning then has a longer piece of text that is <b>very</b> long in the middle and finally ends with another reference to Kennedy",
+ snippets[0]);
+
+ ir.close();
+ dir.close();
+ }
}
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java
index e3f697f9d2b..07cef6529e4 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java
@@ -38,6 +38,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.CommonTermsQuery;
+import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
@@ -86,6 +87,36 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
writer.close();
dir.close();
}
+
+ public void testCustomScoreQueryHighlight() throws IOException {
+ Directory dir = newDirectory();
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+ Document doc = new Document();
+ FieldType type = new FieldType(TextField.TYPE_STORED);
+ type.setStoreTermVectorOffsets(true);
+ type.setStoreTermVectorPositions(true);
+ type.setStoreTermVectors(true);
+ type.freeze();
+ Field field = new Field("field", "This is a test where foo is highlighed and should be highlighted", type);
+
+ doc.add(field);
+ writer.addDocument(doc);
+ FastVectorHighlighter highlighter = new FastVectorHighlighter();
+
+ IndexReader reader = DirectoryReader.open(writer, true);
+ int docId = 0;
+ FieldQuery fieldQuery = highlighter.getFieldQuery( new CustomScoreQuery(new TermQuery(new Term("field", "foo"))), reader );
+ String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
+ // highlighted results are centered
+ assertEquals("This is a test where <b>foo</b> is highlighed and should be highlighted", bestFragments[0]);
+ bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 52, 1);
+ assertEquals("This is a test where <b>foo</b> is highlighed and should be", bestFragments[0]);
+ bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 30, 1);
+ assertEquals("a test where <b>foo</b> is highlighed", bestFragments[0]);
+ reader.close();
+ writer.close();
+ dir.close();
+ }
public void testPhraseHighlightLongTextTest() throws IOException {
Directory dir = newDirectory();