You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2024/02/09 16:15:34 UTC
(lucenenet) branch master updated: BUG: Lucene.Net.Search.FieldComparer.TermValComparer: Fixed sorting ambiguity between empty fields and missing fields (#912)
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
The following commit(s) were added to refs/heads/master by this push:
new 3f87e842a BUG: Lucene.Net.Search.FieldComparer.TermValComparer: Fixed sorting ambiguity between empty fields and missing fields (#912)
3f87e842a is described below
commit 3f87e842aeda3e32b2773cd00100317b6ad352da
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Fri Feb 9 23:15:29 2024 +0700
BUG: Lucene.Net.Search.FieldComparer.TermValComparer: Fixed sorting ambiguity between empty fields and missing fields (#912)
* Lucene.Net.Search.TestSort: Added a test to ensure empty values are not mistaken for missing values.
* BUG: Lucene.Net.Search.FieldComparer.TermValComparer: Using a singleton instance breaks missing vs empty field comparison because the static fields MISSING_BYTES and NON_MISSING_BYTES are tested for reference equality.
* BREAKING: Lucene.Net.Search.FieldComparer: Added internal constructor so users upgrading from older versions of Lucene.NET don't attempt to use this class and instead use the Lucene equivalent class, FieldComparer<T>.
---
src/Lucene.Net.Tests/Search/TestSort.cs | 39 +++++++++++++++++++++++++++++++-
src/Lucene.Net/Search/FieldComparator.cs | 13 ++++++++---
2 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/src/Lucene.Net.Tests/Search/TestSort.cs b/src/Lucene.Net.Tests/Search/TestSort.cs
index 847012bc5..7abd5dd7b 100644
--- a/src/Lucene.Net.Tests/Search/TestSort.cs
+++ b/src/Lucene.Net.Tests/Search/TestSort.cs
@@ -241,6 +241,43 @@ namespace Lucene.Net.Search
dir.Dispose();
}
+ /// <summary>
+ /// Tests sorting on type string_val with a missing value and an empty value.</summary>
+ [Test]
+ [LuceneNetSpecific]
+ public virtual void TestStringValMissingVsNotMissing()
+ {
+ Directory dir = NewDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(Random, dir);
+ Document doc = new Document();
+ doc.Add(NewStringField("value", "", Field.Store.YES)); // Not missing (empty)
+ writer.AddDocument(doc);
+ doc = new Document();
+ writer.AddDocument(doc);
+ doc = new Document();
+ doc.Add(NewStringField("value", "foo", Field.Store.YES));
+ writer.AddDocument(doc);
+ doc = new Document();
+ doc.Add(NewStringField("value", "bar", Field.Store.YES));
+ writer.AddDocument(doc);
+ IndexReader ir = writer.GetReader();
+ writer.Dispose();
+
+ IndexSearcher searcher = NewSearcher(ir);
+ Sort sort = new Sort(new SortField("value", SortFieldType.STRING_VAL));
+
+ TopDocs td = searcher.Search(new MatchAllDocsQuery(), 10, sort);
+ Assert.AreEqual(4, td.TotalHits);
+ // null comes first
+ Assert.IsNull(searcher.Doc(td.ScoreDocs[0].Doc).Get("value"));
+ Assert.AreEqual("", searcher.Doc(td.ScoreDocs[1].Doc).Get("value"));
+ Assert.AreEqual("bar", searcher.Doc(td.ScoreDocs[2].Doc).Get("value"));
+ Assert.AreEqual("foo", searcher.Doc(td.ScoreDocs[3].Doc).Get("value"));
+
+ ir.Dispose();
+ dir.Dispose();
+ }
+
/// <summary>
/// Tests sorting on type string with a missing
/// value sorted first
@@ -2019,4 +2056,4 @@ namespace Lucene.Net.Search
dir.Dispose();
}
}
-}
\ No newline at end of file
+}
diff --git a/src/Lucene.Net/Search/FieldComparator.cs b/src/Lucene.Net/Search/FieldComparator.cs
index 67e4edfcc..11a707829 100644
--- a/src/Lucene.Net/Search/FieldComparator.cs
+++ b/src/Lucene.Net/Search/FieldComparator.cs
@@ -251,6 +251,9 @@ namespace Lucene.Net.Search
// type parameter to access these nested types. Also moving non-generic methods here for casting without generics.
public abstract class FieldComparer
{
+ // LUCENENET: This class is not intended for user subclassing. Use FieldComparer<T> instead.
+ internal FieldComparer() { }
+
/// <summary>
/// Returns -1 if first is less than second. Default
/// implementation to assume the type implements <see cref="IComparable{T}"/> and
@@ -1452,10 +1455,14 @@ namespace Lucene.Net.Search
// TODO: should we remove this? who really uses it?
public sealed class TermValComparer : FieldComparer<BytesRef>
{
+ // LUCENENET NOTE: We can't use Arrays.Empty<byte>() here because
+ // MISSING_BYTES and NON_MISSING_BYTES are compared for reference
+ // equality and Arrays.Empty<byte>() returns a singleton instance.
+
// sentinels, just used internally in this comparer
- private static readonly byte[] MISSING_BYTES = Arrays.Empty<byte>();
+ private static readonly byte[] MISSING_BYTES = new byte[0];
- private static readonly byte[] NON_MISSING_BYTES = Arrays.Empty<byte>();
+ private static readonly byte[] NON_MISSING_BYTES = new byte[0];
private readonly BytesRef[] values; // LUCENENET: marked readonly
private BinaryDocValues docTerms;
@@ -1577,4 +1584,4 @@ namespace Lucene.Net.Search
}
}
}
-}
\ No newline at end of file
+}