Index: lucene/CHANGES.txt
===================================================================
--- lucene/CHANGES.txt (revision 1455730)
+++ lucene/CHANGES.txt (working copy)
@@ -61,6 +61,10 @@
subclasses with ctors taking AttributeFactory.
(Renaud Delbru, Uwe Schindler, Steve Rowe)
+* 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
Index: lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
===================================================================
--- lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java (revision 1455730)
+++ lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java (working copy)
@@ -406,4 +406,35 @@
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 test. Just highlighting from postings. This is also a much sillier test. Feel free to test test test test test test test.", snippets[0]);
+
+ ir.close();
+ dir.close();
+ }
}
Index: lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
===================================================================
--- lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java (revision 1455730)
+++ lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java (working copy)
@@ -118,7 +118,9 @@
/**
* 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 maxLength is negative or Integer.MAX_VALUE
@@ -129,7 +131,10 @@
// 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;
Index: lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java
===================================================================
--- lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java (revision 0)
+++ lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java (working copy)
@@ -0,0 +1,83 @@
+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.
+ *
+ * NOTE: this is NOT general purpose! It throws
+ * UnsupportedOperationException from all methods except
+ * those required by PostingsHighlighter. */
+
+class WholeBreakIterator extends BreakIterator {
+
+ private int len;
+
+ @Override
+ public int current() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int first() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int following(int pos) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public CharacterIterator getText() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int last() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int next() {
+ return len;
+ }
+
+ @Override
+ public int next(int n) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int preceding(int pos) {
+ return 0;
+ }
+
+ @Override
+ public int previous() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setText(CharacterIterator newText) {
+ len = newText.getEndIndex();
+ }
+}
Property changes on: lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property