-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: Numerical range queries don't work when using lucene query syn…
…tax #133
- Loading branch information
Showing
15 changed files
with
118 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Examine/LuceneEngine/Indexing/IndexFieldRangeValueType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Lucene.Net.Search; | ||
|
||
namespace Examine.LuceneEngine.Indexing | ||
{ | ||
public abstract class IndexFieldRangeValueType<T> : IndexFieldValueTypeBase, IIndexRangeValueType<T>, IIndexRangeValueType | ||
where T : struct | ||
{ | ||
protected IndexFieldRangeValueType(string fieldName, bool store = true) : base(fieldName, store) | ||
{ | ||
} | ||
|
||
public abstract Query GetQuery(T? lower, T? upper, bool lowerInclusive = true, bool upperInclusive = true); | ||
|
||
public Query GetQuery(string lower, string upper, bool lowerInclusive = true, bool upperInclusive = true) | ||
{ | ||
var lowerParsed = TryConvert<T>(lower, out var lowerValue); | ||
var upperParsed = TryConvert<T>(upper, out var upperValue); | ||
|
||
return GetQuery(lowerParsed ? (T?)lowerValue : null, upperParsed ? (T?)upperValue : null, lowerInclusive, upperInclusive); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Examine/LuceneEngine/Search/ExamineMultiFieldQueryParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Examine.LuceneEngine.Indexing; | ||
using Lucene.Net.Analysis; | ||
using Lucene.Net.Search; | ||
using Lucene.Net.Util; | ||
|
||
namespace Examine.LuceneEngine.Search | ||
{ | ||
/// <summary> | ||
/// Custom query parser to deal with Examine/Lucene field value types | ||
/// </summary> | ||
public class ExamineMultiFieldQueryParser : CustomMultiFieldQueryParser | ||
{ | ||
private readonly ISearchContext _searchContext; | ||
|
||
public ExamineMultiFieldQueryParser(ISearchContext searchContext, Version matchVersion, string[] fields, Analyzer analyzer) : base(matchVersion, fields, analyzer) | ||
{ | ||
_searchContext = searchContext ?? throw new System.ArgumentNullException(nameof(searchContext)); | ||
} | ||
|
||
/// <summary> | ||
/// Override to provide support for numerical range query parsing | ||
/// </summary> | ||
/// <param name="field"></param> | ||
/// <param name="part1"></param> | ||
/// <param name="part2"></param> | ||
/// <param name="inclusive"></param> | ||
/// <returns></returns> | ||
/// <remarks> | ||
/// By Default the lucene query parser only deals with strings and the result is a TermRangeQuery, however for numerics it needs to be a | ||
/// NumericRangeQuery. We can override this method to provide that behavior. | ||
/// | ||
/// In previous releases people were complaining that this wouldn't work and this is why. The answer came from here https://stackoverflow.com/questions/5026185/how-do-i-make-the-queryparser-in-lucene-handle-numeric-ranges | ||
/// </remarks> | ||
protected override Query GetRangeQuery(string field, string part1, string part2, bool inclusive) | ||
{ | ||
// if the field is IIndexRangeValueType then return it's query, else return the default | ||
var fieldType = _searchContext.GetFieldValueType(field); | ||
if (fieldType != null && fieldType is IIndexRangeValueType rangeType) | ||
{ | ||
return rangeType.GetQuery(part1, part2, inclusive, inclusive); | ||
} | ||
|
||
return base.GetRangeQuery(field, part1, part2, inclusive); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters