From 55edc565d8f192f8a349611f8ca827610b313148 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 7 Mar 2014 16:12:00 +0000 Subject: [PATCH] LUCENE-5500: SortingMergePolicy should error if the Sort refers to the score git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1575306 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/core/src/java/org/apache/lucene/search/Sort.java | 4 ++-- .../src/java/org/apache/lucene/index/sorter/Sorter.java | 3 +++ .../lucene/index/sorter/SortingAtomicReaderTest.java | 9 +++++++++ .../lucene/index/sorter/TestSortingMergePolicy.java | 9 +++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/Sort.java b/lucene/core/src/java/org/apache/lucene/search/Sort.java index 0650d37fe33..57630635d20 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Sort.java +++ b/lucene/core/src/java/org/apache/lucene/search/Sort.java @@ -202,8 +202,8 @@ public class Sort { return 0x45aaf665 + Arrays.hashCode(fields); } - /** Whether the relevance score is needed to sort documents. */ - boolean needsScores() { + /** Returns true if the relevance score is needed to sort documents. */ + public boolean needsScores() { for (SortField sortField : fields) { if (sortField.needsScores()) { return true; diff --git a/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java b/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java index d32785f8876..608b072237a 100644 --- a/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java +++ b/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java @@ -39,6 +39,9 @@ final class Sorter { /** Creates a new Sorter to sort the index with {@code sort} */ Sorter(Sort sort) { + if (sort.needsScores()) { + throw new IllegalArgumentException("Cannot sort an index with a Sort that refers to the relevance score"); + } this.sort = sort; } diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/SortingAtomicReaderTest.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/SortingAtomicReaderTest.java index 89d6403619b..bb75fbcb62d 100644 --- a/lucene/misc/src/test/org/apache/lucene/index/sorter/SortingAtomicReaderTest.java +++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/SortingAtomicReaderTest.java @@ -62,5 +62,14 @@ public class SortingAtomicReaderTest extends SorterTestBase { TestUtil.checkReader(reader); } + + public void testBadSort() throws Exception { + try { + SortingAtomicReader.wrap(reader, Sort.RELEVANCE); + fail("Didn't get expected exception"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot sort an index with a Sort that refers to the relevance score", e.getMessage()); + } + } } diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java index 47fb654d3ef..5095aeca299 100644 --- a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java +++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java @@ -172,5 +172,14 @@ public class TestSortingMergePolicy extends LuceneTestCase { assertReaderEquals("", sortedReader1, sortedReader2); } + + public void testBadSort() throws Exception { + try { + new SortingMergePolicy(newMergePolicy(), Sort.RELEVANCE); + fail("Didn't get expected exception"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot sort an index with a Sort that refers to the relevance score", e.getMessage()); + } + } }