Skip to content

Apache Lucene open-source search software

LUCENE-4816: passing null to PostingsHighlighter means the entire content is treated as a single Passage

Michael McCandlesse48d0d44a9dc
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index dbd6cf194f5..dc9e96e24f4 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -64,6 +64,10 @@ New Features
* LUCENE-4820: Add payloads to Analyzing/FuzzySuggester, to record an
arbitrary byte[] per suggestion (Mike McCandless)
+* LUCENE-4816: Passing null as the BreakIterator to PostingsHighlighter
+ now highlights the entire content as a single Passage. (Robert
+ Muir, Mike McCandless)
+
Optimizations
* LUCENE-4819: Added Sorted[Set]DocValues.termsEnum(), and optimized the
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
index f0ad6a2ba5c..f4e92e41ced 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
@@ -118,7 +118,9 @@ public final class PostingsHighlighter {
/**
* Creates a new highlighter with custom parameters.
* @param maxLength maximum content size to process.
- * @param breakIterator used for finding passage boundaries.
+ * @param breakIterator used for finding passage
+ * boundaries; pass null to highlight the entire
+ * content as a single Passage.
* @param scorer used for ranking passages.
* @param formatter used for formatting passages into highlighted snippets.
* @throws IllegalArgumentException if <code>maxLength</code> is negative or <code>Integer.MAX_VALUE</code>
@@ -129,7 +131,10 @@ public final class PostingsHighlighter {
// our sentinel in the offsets queue uses this value to terminate.
throw new IllegalArgumentException("maxLength must be < Integer.MAX_VALUE");
}
- if (breakIterator == null || scorer == null || formatter == null) {
+ if (breakIterator == null) {
+ breakIterator = new WholeBreakIterator();
+ }
+ if (scorer == null || formatter == null) {
throw new NullPointerException();
}
this.maxLength = maxLength;
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java
new file mode 100644
index 00000000000..54eecd426dd
--- /dev/null
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java
@@ -0,0 +1,113 @@
+package org.apache.lucene.search.postingshighlight;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.text.BreakIterator;
+import java.text.CharacterIterator;
+
+/** Just produces one single fragment for the entire
+ * string. */
+final class WholeBreakIterator extends BreakIterator {
+ private CharacterIterator text;
+ private int len;
+ private int current;
+
+ @Override
+ public int current() {
+ return current;
+ }
+
+ @Override
+ public int first() {
+ return (current = 0);
+ }
+
+ @Override
+ public int following(int pos) {
+ if (pos < 0 || pos > len) {
+ throw new IllegalArgumentException("offset out of bounds");
+ } else if (pos == len) {
+ return DONE;
+ } else {
+ return last();
+ }
+ }
+
+ @Override
+ public CharacterIterator getText() {
+ return text;
+ }
+
+ @Override
+ public int last() {
+ return (current = len);
+ }
+
+ @Override
+ public int next() {
+ if (current == len) {
+ return DONE;
+ } else {
+ return last();
+ }
+ }
+
+ @Override
+ public int next(int n) {
+ if (n < 0) {
+ for (int i = 0; i < -n; i++) {
+ previous();
+ }
+ } else {
+ for (int i = 0; i < n; i++) {
+ next();
+ }
+ }
+ return current();
+ }
+
+ @Override
+ public int preceding(int pos) {
+ if (pos < 0 || pos > len) {
+ throw new IllegalArgumentException("offset out of bounds");
+ } else if (pos == 0) {
+ return DONE;
+ } else {
+ return first();
+ }
+ }
+
+ @Override
+ public int previous() {
+ if (current == 0) {
+ return DONE;
+ } else {
+ return first();
+ }
+ }
+
+ @Override
+ public void setText(CharacterIterator newText) {
+ if (newText.getBeginIndex() != 0) {
+ throw new UnsupportedOperationException();
+ }
+ len = newText.getEndIndex();
+ text = newText;
+ current = 0;
+ }
+}
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 55ac68e83df..387119d3779 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
@@ -434,4 +434,35 @@ public class TestPostingsHighlighter extends LuceneTestCase {
ir.close();
dir.close();
}
+
+ public void testHighlightAllText() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
+ 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 is a test. Just highlighting from postings. This is also a much sillier test. Feel free to test test test test test test test.");
+ iw.addDocument(doc);
+
+ IndexReader ir = iw.getReader();
+ iw.close();
+
+ IndexSearcher searcher = newSearcher(ir);
+ PostingsHighlighter highlighter = new PostingsHighlighter(10000, null, new PassageScorer(), new PassageFormatter());
+ Query query = new TermQuery(new Term("body", "test"));
+ TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER);
+ assertEquals(1, topDocs.totalHits);
+ String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
+ assertEquals(1, snippets.length);
+ assertEquals("This is a <b>test</b>. Just highlighting from postings. This is also a much sillier <b>test</b>. Feel free to <b>test</b> <b>test</b> <b>test</b> <b>test</b> <b>test</b> <b>test</b> <b>test</b>.", snippets[0]);
+
+ ir.close();
+ dir.close();
+ }
}