Apache Solr open-source search software
LUCENE-4905: make maxPassages per-field in postingshighlighter
Robert Muire78fd1cec91b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 327dbbe19ef..a0d04149df7 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -164,6 +164,9 @@ New Featureson an IndexCommit starting from a near-real-time reader (previouslythis would throw IllegalArgumentException). (Mike McCandless)+* LUCENE-4905: Made the maxPassages parameter per-field in PostingsHighlighter.+ (Robert Muir)+Optimizations* LUCENE-4839: SorterTemplate.merge can now be overridden in order to replacediff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.javaindex f6d0cc954cb..337cddfe0e4 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@@ -48,6 +48,7 @@ import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.SorterTemplate;import org.apache.lucene.util.UnicodeUtil;/**@@ -194,7 +195,7 @@ public class PostingsHighlighter {* {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS}*/public String[] highlight(String field, Query query, IndexSearcher searcher, TopDocs topDocs, int maxPassages) throws IOException {- Map<String,String[]> res = highlightFields(new String[] { field }, query, searcher, topDocs, maxPassages);+ Map<String,String[]> res = highlightFields(new String[] { field }, query, searcher, topDocs, new int[] { maxPassages });return res.get(field);}@@ -224,7 +225,9 @@ public class PostingsHighlighter {* {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS}*/public Map<String,String[]> highlightFields(String fields[], Query query, IndexSearcher searcher, TopDocs topDocs) throws IOException {- return highlightFields(fields, query, searcher, topDocs, 1);+ int maxPassages[] = new int[fields.length];+ Arrays.fill(maxPassages, 1);+ return highlightFields(fields, query, searcher, topDocs, maxPassages);}/**@@ -255,7 +258,7 @@ public class PostingsHighlighter {* @throws IllegalArgumentException if <code>field</code> was indexed without* {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS}*/- public Map<String,String[]> highlightFields(String fields[], Query query, IndexSearcher searcher, TopDocs topDocs, int maxPassages) throws IOException {+ public Map<String,String[]> highlightFields(String fields[], Query query, IndexSearcher searcher, TopDocs topDocs, int maxPassages[]) throws IOException {final ScoreDoc scoreDocs[] = topDocs.scoreDocs;int docids[] = new int[scoreDocs.length];for (int i = 0; i < docids.length; i++) {@@ -269,12 +272,12 @@ public class PostingsHighlighter {* Highlights the top-N passages from multiple fields,* for the provided int[] docids.*- * @param fields field names to highlight.+ * @param fieldsIn field names to highlight.* Must have a stored string value and also be indexed with offsets.* @param query query to highlight.* @param searcher searcher that was previously used to execute the query.* @param docidsIn containing the document IDs to highlight.- * @param maxPassages The maximum number of top-N ranked passages per-field used to+ * @param maxPassagesIn The maximum number of top-N ranked passages per-field used to* form the highlighted snippets.* @return Map keyed on field name, containing the array of formatted snippets* corresponding to the documents in <code>topDocs</code>.@@ -285,7 +288,13 @@ public class PostingsHighlighter {* @throws IllegalArgumentException if <code>field</code> was indexed without* {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS}*/- public Map<String,String[]> highlightFields(String fields[], Query query, IndexSearcher searcher, int[] docidsIn, int maxPassages) throws IOException {+ public Map<String,String[]> highlightFields(String fieldsIn[], Query query, IndexSearcher searcher, int[] docidsIn, int maxPassagesIn[]) throws IOException {+ if (fieldsIn.length < 1) {+ throw new IllegalArgumentException("fieldsIn must not be empty");+ }+ if (fieldsIn.length != maxPassagesIn.length) {+ throw new IllegalArgumentException("invalid number of maxPassagesIn");+ }final IndexReader reader = searcher.getIndexReader();query = rewrite(query);SortedSet<Term> queryTerms = new TreeSet<Term>();@@ -294,20 +303,53 @@ public class PostingsHighlighter {IndexReaderContext readerContext = reader.getContext();List<AtomicReaderContext> leaves = readerContext.leaves();- // Make our own copy because we sort in-place:+ // Make our own copies because we sort in-place:int[] docids = new int[docidsIn.length];System.arraycopy(docidsIn, 0, docids, 0, docidsIn.length);+ final String fields[] = new String[fieldsIn.length];+ System.arraycopy(fieldsIn, 0, fields, 0, fieldsIn.length);+ final int maxPassages[] = new int[maxPassagesIn.length];+ System.arraycopy(maxPassagesIn, 0, maxPassages, 0, maxPassagesIn.length);// sort for sequential ioArrays.sort(docids);- Arrays.sort(fields);+ new SorterTemplate() {+ String pivot;++ @Override+ protected void swap(int i, int j) {+ String tmp = fields[i];+ fields[i] = fields[j];+ fields[j] = tmp;+ int tmp2 = maxPassages[i];+ maxPassages[i] = maxPassages[j];+ maxPassages[j] = tmp2;+ }++ @Override+ protected int compare(int i, int j) {+ return fields[i].compareTo(fields[j]);+ }++ @Override+ protected void setPivot(int i) {+ pivot = fields[i];+ }++ @Override+ protected int comparePivot(int j) {+ return pivot.compareTo(fields[j]);+ }++ }.mergeSort(0, fields.length-1);// pull stored data:String[][] contents = loadFieldValues(searcher, fields, docids, maxLength);- Map<String,String[]> highlights = new HashMap<String,String[]>();+ Map<String,String[]> highlights = new HashMap<String,String[]>();;for (int i = 0; i < fields.length; i++) {String field = fields[i];+ int numPassages = maxPassages[i];Term floor = new Term(field, "");Term ceiling = new Term(field, UnicodeUtil.BIG_TERM);SortedSet<Term> fieldTerms = queryTerms.subSet(floor, ceiling);@@ -319,7 +361,7 @@ public class PostingsHighlighter {for(Term term : fieldTerms) {terms[termUpto++] = term.bytes();}- Map<Integer,String> fieldHighlights = highlightField(field, contents[i], getBreakIterator(field), terms, docids, leaves, maxPassages);+ Map<Integer,String> fieldHighlights = highlightField(field, contents[i], getBreakIterator(field), terms, docids, leaves, numPassages);String[] result = new String[docids.length];for (int j = 0; j < docidsIn.length; j++) {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.javaindex f0a7c4153df..103693b66f8 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@@ -503,7 +503,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {int[] docIDs = new int[2];docIDs[0] = hits[0].doc;docIDs[1] = hits[1].doc;- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 1).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 1 }).get("body");assertEquals(2, snippets.length);assertEquals("Just a test <b>highlighting</b> from postings. ", snippets[0]);assertEquals("<b>Highlighting</b> the first term. ", snippets[1]);@@ -582,7 +582,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {PostingsHighlighter highlighter = new PostingsHighlighter();Query query = new TermQuery(new Term("body", "highlighting"));int[] docIDs = new int[] {0};- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 2).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");assertEquals(1, snippets.length);assertEquals("test this is. another sentence this test has. ", snippets[0]);@@ -618,7 +618,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {};Query query = new TermQuery(new Term("body", "highlighting"));int[] docIDs = new int[] {0};- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 2).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");assertEquals(1, snippets.length);assertNull(snippets[0]);@@ -654,7 +654,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {};Query query = new TermQuery(new Term("body", "highlighting"));int[] docIDs = new int[] {0};- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 2).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");assertEquals(1, snippets.length);assertEquals("test this is. another sentence this test has. far away is that planet.", snippets[0]);@@ -685,7 +685,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {PostingsHighlighter highlighter = new PostingsHighlighter();Query query = new TermQuery(new Term("bogus", "highlighting"));int[] docIDs = new int[] {0};- String snippets[] = highlighter.highlightFields(new String[] {"bogus"}, query, searcher, docIDs, 2).get("bogus");+ String snippets[] = highlighter.highlightFields(new String[] {"bogus"}, query, searcher, docIDs, new int[] { 2 }).get("bogus");assertEquals(1, snippets.length);assertNull(snippets[0]);@@ -721,7 +721,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {Query query = new TermQuery(new Term("body", "highlighting"));int[] docIDs = new int[1];docIDs[0] = docID;- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 2).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");assertEquals(1, snippets.length);assertEquals(" ", snippets[0]);@@ -757,7 +757,7 @@ public class TestPostingsHighlighter extends LuceneTestCase {Query query = new TermQuery(new Term("body", "highlighting"));int[] docIDs = new int[1];docIDs[0] = docID;- String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, 2).get("body");+ String snippets[] = highlighter.highlightFields(new String[] {"body"}, query, searcher, docIDs, new int[] { 2 }).get("body");assertEquals(1, snippets.length);assertNull(snippets[0]);@@ -814,4 +814,39 @@ public class TestPostingsHighlighter extends LuceneTestCase {ir.close();dir.close();}++ public void testMultipleSnippetSizes() 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);+ Field title = new Field("title", "", offsetsType);+ Document doc = new Document();+ doc.add(body);+ doc.add(title);++ body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");+ title.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");+ iw.addDocument(doc);++ IndexReader ir = iw.getReader();+ iw.close();++ IndexSearcher searcher = newSearcher(ir);+ PostingsHighlighter highlighter = new PostingsHighlighter();+ BooleanQuery query = new BooleanQuery();+ query.add(new TermQuery(new Term("body", "test")), BooleanClause.Occur.SHOULD);+ query.add(new TermQuery(new Term("title", "test")), BooleanClause.Occur.SHOULD);+ Map<String,String[]> snippets = highlighter.highlightFields(new String[] { "title", "body" }, query, searcher, new int[] { 0 }, new int[] { 1, 2 });+ String titleHighlight = snippets.get("title")[0];+ String bodyHighlight = snippets.get("body")[0];+ assertEquals("This is a <b>test</b>. ", titleHighlight);+ assertEquals("This is a <b>test</b>. Just a <b>test</b> highlighting from postings. ", bodyHighlight);+ ir.close();+ dir.close();+ }}diff --git a/solr/core/src/java/org/apache/solr/highlight/PostingsSolrHighlighter.java b/solr/core/src/java/org/apache/solr/highlight/PostingsSolrHighlighter.javaindex f8ed466e4d6..2249c85393b 100644--- a/solr/core/src/java/org/apache/solr/highlight/PostingsSolrHighlighter.java+++ b/solr/core/src/java/org/apache/solr/highlight/PostingsSolrHighlighter.java@@ -19,6 +19,7 @@ package org.apache.solr.highlight;import java.io.IOException;import java.text.BreakIterator;+import java.util.Arrays;import java.util.Collections;import java.util.Locale;import java.util.Map;@@ -162,9 +163,12 @@ public class PostingsSolrHighlighter extends SolrHighlighter implements PluginIn// query-time parametersString[] fieldNames = getHighlightFields(query, req, defaultFields);+ // TODO: make this per-fieldint numSnippets = params.getInt(HighlightParams.SNIPPETS, 1);+ int maxPassages[] = new int[fieldNames.length];+ Arrays.fill(maxPassages, numSnippets);- Map<String,String[]> snippets = highlighter.highlightFields(fieldNames, query, searcher, docIDs, numSnippets);+ Map<String,String[]> snippets = highlighter.highlightFields(fieldNames, query, searcher, docIDs, maxPassages);return encodeSnippets(keys, fieldNames, snippets);} else {return null;