Skip to content

Commit 2abc120

Browse files
Fix query parser failure with Unicode minus sign, #846 (#1205)
* Fix query parser failure with Unicode minus sign, #846 * Lucene.Net.QueryParsers.Flexible.Standard.TestNumericQueryParser::TestInclusiveNumericRange_UnicodeMinus(): Use CultureContext to ensure the temporary culture assigned to the current thread is reverted at the end of the test. --------- Co-authored-by: Shad Storhaug <shad@shadstorhaug.com>
1 parent ef4678c commit 2abc120

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

.build/dependencies.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<IKVMPackageVersion>8.7.5</IKVMPackageVersion>
3939
<IKVMMavenSdkPackageVersion>1.6.7</IKVMMavenSdkPackageVersion>
4040
<!-- J2N will break binary compatibility in 3.0.0 to fix the APIs of collection types -->
41-
<J2NPackageVersion>[2.2.0-alpha-0021, 3.0.0)</J2NPackageVersion>
41+
<J2NPackageVersion>[2.2.0-alpha-0026, 3.0.0)</J2NPackageVersion>
4242
<LiquidTestReportsMarkdownPackageVersion>1.0.9</LiquidTestReportsMarkdownPackageVersion>
4343
<LuceneNetCodeAnalysisDevPackageVersion>1.0.0-alpha.18</LuceneNetCodeAnalysisDevPackageVersion>
4444
<MicrosoftAspNetCoreHttpAbstractionsPackageVersion>2.3.0</MicrosoftAspNetCoreHttpAbstractionsPackageVersion>

src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestNumericQueryParser.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using J2N.Globalization;
12
using J2N.Numerics;
23
using Lucene.Net.Analysis;
4+
using Lucene.Net.Attributes;
35
using Lucene.Net.Documents;
46
using Lucene.Net.Index;
57
using Lucene.Net.Index.Extensions;
@@ -251,10 +253,10 @@ public override void SetUp()
251253
)), LOCALE))
252254
;
253255

254-
randomNumberMap[NumericType.INT64.ToString()] = (J2N.Numerics.Int64)randomLong;
255-
randomNumberMap[NumericType.INT32.ToString()] = (J2N.Numerics.Int32)randomInt;
256-
randomNumberMap[NumericType.SINGLE.ToString()] = (J2N.Numerics.Single)randomFloat;
257-
randomNumberMap[NumericType.DOUBLE.ToString()] = (J2N.Numerics.Double)randomDouble;
256+
randomNumberMap[nameof(NumericType.INT64)] = (J2N.Numerics.Int64)randomLong;
257+
randomNumberMap[nameof(NumericType.INT32)] = (J2N.Numerics.Int32)randomInt;
258+
randomNumberMap[nameof(NumericType.SINGLE)] = (J2N.Numerics.Single)randomFloat;
259+
randomNumberMap[nameof(NumericType.DOUBLE)] = (J2N.Numerics.Double)randomDouble;
258260
randomNumberMap[DATE_FIELD_NAME] = (J2N.Numerics.Int64)randomDate;
259261

260262
RANDOM_NUMBER_MAP = JCG.Extensions.DictionaryExtensions.AsReadOnly(randomNumberMap);
@@ -510,6 +512,41 @@ public void TestSimpleNumericQuery()
510512
AssertSimpleQuery(NumberType.NEGATIVE, 1);
511513
}
512514

515+
/// <summary>
516+
/// Tests the fix for Lucene.NET GitHub issue #846.
517+
/// Numeric values were failing for cultures that use a Unicode minus sign
518+
/// (U+2212) rather than a hyphen-minus (U+002D) in exponential notation.
519+
/// This was due to a bug in J2N (#128) that has since been fixed.
520+
/// </summary>
521+
/// <remarks>
522+
/// https://github.com/NightOwl888/J2N/issues/128
523+
/// </remarks>
524+
[Test]
525+
[LuceneNetSpecific]
526+
public void TestInclusiveNumericRange_UnicodeMinus()
527+
{
528+
// Use a culture that uses a Unicode minus sign rather than a hyphen-minus
529+
// in exponential notation.
530+
LOCALE = new CultureInfo("sv-FI");
531+
using (var cultureContext = new CultureContext(LOCALE))
532+
{
533+
NUMBER_FORMAT = new MockNumberFormat(LOCALE);
534+
535+
var randomNumberMap = new JCG.Dictionary<string, Number>(RANDOM_NUMBER_MAP!)
536+
{
537+
[nameof(NumericType.DOUBLE)] = J2N.Numerics.Double.GetInstance(1.0E-20),
538+
[nameof(NumericType.SINGLE)] = J2N.Numerics.Single.GetInstance(1.0E-20f)
539+
};
540+
541+
RANDOM_NUMBER_MAP = JCG.Extensions.DictionaryExtensions.AsReadOnly(randomNumberMap);
542+
543+
qp!.NumericConfigMap[nameof(NumericType.DOUBLE)] = new NumericConfig(PRECISION_STEP, NUMBER_FORMAT, NumericType.DOUBLE);
544+
qp.NumericConfigMap[nameof(NumericType.SINGLE)] = new NumericConfig(PRECISION_STEP, NUMBER_FORMAT, NumericType.SINGLE);
545+
546+
AssertRangeQuery(NumberType.ZERO, NumberType.POSITIVE, true, true, 1);
547+
}
548+
}
549+
513550
public void AssertRangeQuery(NumberType? lowerType, NumberType? upperType,
514551
bool lowerInclusive, bool upperInclusive, int expectedDocCount)
515552
{

0 commit comments

Comments
 (0)