Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4.0 Bugfix Index Float field as a Single #365

Open
wants to merge 3 commits into
base: release/4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Examine.Core/FieldDefinitionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
public const string Integer = "int";

/// <summary>
/// Will be indexed as a float
/// Will be indexed as a single
/// </summary>
public const string Float = "float";

/// <summary>
/// Will be indexed as a single
/// </summary>
[Obsolete("To remove in Examine V5. Use Float")]
public const string Single = "single";

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 24 in src/Examine.Core/FieldDefinitionTypes.cs

View workflow job for this annotation

GitHub Actions / build


/// <summary>
/// Will be indexed as a double
/// </summary>
Expand Down
114 changes: 114 additions & 0 deletions src/Examine.Lucene/Indexing/FloatType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using Examine.Lucene.Providers;
using Examine.Lucene.Search;
using Examine.Search;
using Lucene.Net.Documents;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;
using Lucene.Net.Search;
using Microsoft.Extensions.Logging;
using static Lucene.Net.Queries.Function.ValueSources.MultiFunction;

namespace Examine.Lucene.Indexing
{
/// <summary>
/// Represents a float/single <see cref="IndexFieldRangeValueType{T}"/>
/// </summary>
public class FloatType : IndexFieldRangeValueType<float>, IIndexFacetValueType
{
private readonly bool _isFacetable;
#pragma warning disable IDE0032 // Use auto property
private readonly bool _taxonomyIndex;
#pragma warning restore IDE0032 // Use auto property

/// <inheritdoc/>
public FloatType(string fieldName, bool isFacetable, bool taxonomyIndex, ILoggerFactory logger, bool store)
: base(fieldName, logger, store)
{
_isFacetable = isFacetable;
_taxonomyIndex = taxonomyIndex;
}

/// <inheritdoc/>
[Obsolete("To be removed in Examine V5")]
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
public FloatType(string fieldName, ILoggerFactory logger, bool store = true)
#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
: base(fieldName, logger, store)
{
_isFacetable = false;
}

/// <summary>
/// Can be sorted by the normal field name
/// </summary>
public override string SortableFieldName => FieldName;

/// <inheritdoc/>
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out float parsedVal))
{
return;
}

if (!TryConvert(objArr[1], out string[]? parsedPathVal))
{
return;
}

doc.Add(new SingleField(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));

doc.Add(new FacetField(FieldName, parsedPathVal));
doc.Add(new SingleDocValuesField(FieldName, parsedVal));
return;
}
base.AddValue(doc, value);
}

/// <inheritdoc/>
protected override void AddSingleValue(Document doc, object value)
{
if (!TryConvert(value, out float parsedVal))
{
return;
}

doc.Add(new SingleField(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));

if (_isFacetable && _taxonomyIndex)
{
doc.Add(new FacetField(FieldName, parsedVal.ToString()));
doc.Add(new SingleDocValuesField(FieldName, parsedVal));
}
else if (_isFacetable && !_taxonomyIndex)
{
doc.Add(new SortedSetDocValuesFacetField(FieldName, parsedVal.ToString()));
doc.Add(new SingleDocValuesField(FieldName, parsedVal));
}
}

/// <inheritdoc/>
public override Query? GetQuery(string query) => !TryConvert(query, out float parsedVal) ? null : GetQuery(parsedVal, parsedVal);

/// <inheritdoc/>
public override Query GetQuery(float? lower, float? upper, bool lowerInclusive = true, bool upperInclusive = true)
{
return NumericRangeQuery.NewSingleRange(FieldName,
lower ?? float.MinValue,
upper ?? float.MaxValue, lowerInclusive, upperInclusive);
}

/// <inheritdoc/>
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}

}
8 changes: 5 additions & 3 deletions src/Examine.Lucene/ValueTypeFactoryCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using Examine.Lucene.Analyzers;
using Examine.Lucene.Indexing;
using Lucene.Net.Analysis;
Expand Down Expand Up @@ -73,11 +74,12 @@
private static IReadOnlyDictionary<string, Func<string, IIndexFieldValueType>> GetDefaults(ILoggerFactory loggerFactory, Analyzer? defaultAnalyzer = null) =>
new Dictionary<string, Func<string, IIndexFieldValueType>>(StringComparer.InvariantCultureIgnoreCase) //case insensitive
{
{"number", name => new Int32Type(name, loggerFactory)},

Check warning on line 77 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int32Type.Int32Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 77 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int32Type.Int32Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{FieldDefinitionTypes.Integer, name => new Int32Type(name, loggerFactory)},

Check warning on line 78 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int32Type.Int32Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 78 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int32Type.Int32Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{FieldDefinitionTypes.Float, name => new SingleType(name, loggerFactory)},
{FieldDefinitionTypes.Float, name => new FloatType(name, loggerFactory)},

Check warning on line 79 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'FloatType.FloatType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 79 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'FloatType.FloatType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{FieldDefinitionTypes.Single, name => new SingleType(name, loggerFactory)},

Check warning on line 80 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'FieldDefinitionTypes.Single' is obsolete: 'To remove in Examine V5. Use Float'

Check warning on line 80 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'SingleType.SingleType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 80 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'FieldDefinitionTypes.Single' is obsolete: 'To remove in Examine V5. Use Float'

Check warning on line 80 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'SingleType.SingleType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{FieldDefinitionTypes.Double, name => new DoubleType(name, loggerFactory)},

Check warning on line 81 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'DoubleType.DoubleType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 81 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'DoubleType.DoubleType(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{FieldDefinitionTypes.Long, name => new Int64Type(name, loggerFactory)},

Check warning on line 82 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int64Type.Int64Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'

Check warning on line 82 in src/Examine.Lucene/ValueTypeFactoryCollection.cs

View workflow job for this annotation

GitHub Actions / build

'Int64Type.Int64Type(string, ILoggerFactory, bool)' is obsolete: 'To be removed in Examine V5'
{"date", name => new DateTimeType(name, loggerFactory, DateResolution.MILLISECOND)},
{FieldDefinitionTypes.DateTime, name => new DateTimeType(name, loggerFactory, DateResolution.MILLISECOND)},
{FieldDefinitionTypes.DateYear, name => new DateTimeType(name, loggerFactory, DateResolution.YEAR)},
Expand All @@ -91,7 +93,7 @@
{FieldDefinitionTypes.InvariantCultureIgnoreCase, name => new GenericAnalyzerFieldValueType(name, loggerFactory, new CultureInvariantWhitespaceAnalyzer())},
{FieldDefinitionTypes.EmailAddress, name => new GenericAnalyzerFieldValueType(name, loggerFactory, new EmailAddressAnalyzer())},
{FieldDefinitionTypes.FacetInteger, name => new Int32Type(name, true,false,loggerFactory, true)},
{FieldDefinitionTypes.FacetFloat, name => new SingleType(name, true, false, loggerFactory, true)},
{FieldDefinitionTypes.FacetFloat, name => new FloatType(name, true, false, loggerFactory, true)},
{FieldDefinitionTypes.FacetDouble, name => new DoubleType(name,true, false, loggerFactory, true)},
{FieldDefinitionTypes.FacetLong, name => new Int64Type(name, true, false, loggerFactory, true)},
{FieldDefinitionTypes.FacetDateTime, name => new DateTimeType(name, true, true, false, loggerFactory, DateResolution.MILLISECOND)},
Expand All @@ -103,7 +105,7 @@
{FieldDefinitionTypes.FacetFullText, name => new FullTextType(name, loggerFactory, true, false, false, defaultAnalyzer ?? new CultureInvariantStandardAnalyzer())},
{FieldDefinitionTypes.FacetFullTextSortable, name => new FullTextType(name, loggerFactory, true, false,true, defaultAnalyzer ?? new CultureInvariantStandardAnalyzer())},
{FieldDefinitionTypes.FacetTaxonomyInteger, name => new Int32Type(name,true,true, loggerFactory, true)},
{FieldDefinitionTypes.FacetTaxonomyFloat, name => new SingleType(name,isFacetable: true, taxonomyIndex: true, loggerFactory, true)},
{FieldDefinitionTypes.FacetTaxonomyFloat, name => new FloatType(name,isFacetable: true, taxonomyIndex: true, loggerFactory, true)},
{FieldDefinitionTypes.FacetTaxonomyDouble, name => new DoubleType(name, true, true, loggerFactory, true)},
{FieldDefinitionTypes.FacetTaxonomyLong, name => new Int64Type(name, isFacetable: true, taxonomyIndex: true, loggerFactory, true)},
{FieldDefinitionTypes.FacetTaxonomyDateTime, name => new DateTimeType(name,true, true, taxonomyIndex : true, loggerFactory, DateResolution.MILLISECOND)},
Expand Down
Loading