From f7e106bd71879d654d76ff79d8c08d72517f6e44 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Wed, 20 Mar 2013 16:56:25 +0000 Subject: [PATCH] LUCENE-4853: fix sort order bug with returned snippets git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1458944 13f79535-47bb-0310-9956-ffa450edef68 --- .../PostingsHighlighter.java | 10 ++-- .../TestPostingsHighlighter.java | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) 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 b06d943ea5d..2874712b733 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 @@ -273,7 +273,7 @@ public class PostingsHighlighter { * @throws IllegalArgumentException if field was indexed without * {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS} */ - public Map highlightFields(String fields[], Query query, IndexSearcher searcher, int[] docids, int maxPassages) throws IOException { + public Map highlightFields(String fields[], Query query, IndexSearcher searcher, int[] docidsIn, int maxPassages) throws IOException { final IndexReader reader = searcher.getIndexReader(); query = rewrite(query); SortedSet queryTerms = new TreeSet(); @@ -284,6 +284,10 @@ public class PostingsHighlighter { BreakIterator bi = (BreakIterator)breakIterator.clone(); + // Make our own copy because we sort in-place: + int[] docids = new int[docidsIn.length]; + System.arraycopy(docidsIn, 0, docids, 0, docidsIn.length); + // sort for sequential io Arrays.sort(docids); Arrays.sort(fields); @@ -302,8 +306,8 @@ public class PostingsHighlighter { Map fieldHighlights = highlightField(field, contents[i], bi, terms, docids, leaves, maxPassages); String[] result = new String[docids.length]; - for (int j = 0; j < docids.length; j++) { - result[j] = fieldHighlights.get(docids[j]); + for (int j = 0; j < docidsIn.length; j++) { + result[j] = fieldHighlights.get(docidsIn[j]); } highlights.put(field, result); } 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 a638ac72bfc..40e4745698f 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 @@ -35,6 +35,7 @@ import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.IndexReader; 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.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; @@ -748,4 +749,54 @@ public class TestPostingsHighlighter extends LuceneTestCase { ir.close(); dir.close(); } + + public void testMultipleDocs() throws Exception { + Directory dir = newDirectory(); + IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, 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); + + int numDocs = atLeast(100); + for(int i=0;i