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

refactor: Move facet extraction #3

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 0 additions & 18 deletions src/Examine.Core/Search/FacetDoubleField.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/Examine.Core/Search/FacetFloatField.cs

This file was deleted.

21 changes: 0 additions & 21 deletions src/Examine.Core/Search/FacetFullTextField.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/Examine.Core/Search/FacetLongField.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Examine.Core/Search/IFacetField.cs

This file was deleted.

8 changes: 7 additions & 1 deletion src/Examine.Lucene/Indexing/DateTimeType.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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;

namespace Examine.Lucene.Indexing
{

public class DateTimeType : IndexFieldRangeValueType<DateTime>
public class DateTimeType : IndexFieldRangeValueType<DateTime>, IIndexFacetValueType
{
public DateResolution Resolution { get; }

Expand Down Expand Up @@ -66,5 +70,7 @@ public override Query GetQuery(DateTime? lower, DateTime? upper, bool lowerInclu
lower != null ? DateToLong(lower.Value) : (long?)null,
upper != null ? DateToLong(upper.Value) : (long?)null, lowerInclusive, upperInclusive);
}

public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
7 changes: 6 additions & 1 deletion src/Examine.Lucene/Indexing/DoubleType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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;

namespace Examine.Lucene.Indexing
{
public class DoubleType : IndexFieldRangeValueType<double>
public class DoubleType : IndexFieldRangeValueType<double>, IIndexFacetValueType
{
private readonly bool _isFacetable;

Expand Down Expand Up @@ -46,5 +50,6 @@ public override Query GetQuery(double? lower, double? upper, bool lowerInclusive
lower ?? double.MinValue,
upper ?? double.MaxValue, lowerInclusive, upperInclusive);
}
public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
6 changes: 5 additions & 1 deletion src/Examine.Lucene/Indexing/FullTextType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
using System.IO;
using Examine.Lucene.Analyzers;
using Examine.Lucene.Providers;
using Examine.Lucene.Search;
using Examine.Search;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Miscellaneous;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Documents;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;
using Lucene.Net.Index;
using Lucene.Net.Search;
Expand All @@ -21,7 +24,7 @@ namespace Examine.Lucene.Indexing
/// do an exact match search if the term is less than 4 chars, else it will do a full text search on the phrase
/// with a higher boost, then
/// </remarks>
public class FullTextType : IndexFieldValueTypeBase
public class FullTextType : IndexFieldValueTypeBase, IIndexFacetValueType
{
private readonly bool _sortable;
private readonly Analyzer _analyzer;
Expand Down Expand Up @@ -150,5 +153,6 @@ public override Query GetQuery(string query)
return GenerateQuery(FieldName, query, _analyzer);
}

public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
20 changes: 20 additions & 0 deletions src/Examine.Lucene/Indexing/IIndexFacetValueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Examine.Lucene.Search;
using Examine.Search;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Indexing
{
public interface IIndexFacetValueType
{
/// <summary>
/// Extracts the facets from the field
/// </summary>
/// <param name="facetsCollector"></param>
/// <param name="sortedSetReaderState"></param>
/// <param name="facets"></param>
/// <param name="field"></param>
void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field);
}
}
8 changes: 7 additions & 1 deletion src/Examine.Lucene/Indexing/Int32Type.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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;

namespace Examine.Lucene.Indexing
{
public class Int32Type : IndexFieldRangeValueType<int>
public class Int32Type : IndexFieldRangeValueType<int>, IIndexFacetValueType
{
private readonly bool _isFacetable;

Expand Down Expand Up @@ -46,5 +50,7 @@ public override Query GetQuery(int? lower, int? upper, bool lowerInclusive = tru
lower,
upper, lowerInclusive, upperInclusive);
}

public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
7 changes: 6 additions & 1 deletion src/Examine.Lucene/Indexing/Int64Type.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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;

namespace Examine.Lucene.Indexing
{
public class Int64Type : IndexFieldRangeValueType<long>
public class Int64Type : IndexFieldRangeValueType<long>, IIndexFacetValueType
{
private readonly bool _isFacetable;

Expand Down Expand Up @@ -46,5 +50,6 @@ public override Query GetQuery(long? lower, long? upper, bool lowerInclusive = t
lower,
upper, lowerInclusive, upperInclusive);
}
public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
8 changes: 7 additions & 1 deletion src/Examine.Lucene/Indexing/SingleType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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;

namespace Examine.Lucene.Indexing
{
public class SingleType : IndexFieldRangeValueType<float>
public class SingleType : IndexFieldRangeValueType<float>, IIndexFacetValueType
{
private readonly bool _isFacetable;

Expand Down Expand Up @@ -46,5 +50,7 @@ public override Query GetQuery(float? lower, float? upper, bool lowerInclusive =
lower ?? float.MinValue,
upper ?? float.MaxValue, lowerInclusive, upperInclusive);
}

public virtual void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets, IFacetField field) => field.ExtractFacets(facetsCollector, sortedSetReaderState, facets);
}
}
39 changes: 39 additions & 0 deletions src/Examine.Lucene/Search/FacetDoubleField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Lucene.Net.Facet.Range;
using Lucene.Net.Facet;
using System.Collections.Generic;
using Examine.Search;
using System.Linq;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Search
{
public readonly struct FacetDoubleField : IFacetField
{
public Examine.Search.DoubleRange[] DoubleRanges { get; }

public string Field { get; }

public string FacetField { get; }

public FacetDoubleField(string field, Examine.Search.DoubleRange[] doubleRanges, string facetField)
{
Field = field;
DoubleRanges = doubleRanges;
FacetField = facetField;
}

public void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets)
{
var doubleFacetCounts = new DoubleRangeFacetCounts(Field, facetsCollector, DoubleRanges.AsLuceneRange().ToArray());

var doubleFacets = doubleFacetCounts.GetTopChildren(0, Field);

if (doubleFacets == null)
{
return;
}

facets.Add(Field, new Examine.Search.FacetResult(doubleFacets.LabelValues.Select(labelValue => new FacetValue(labelValue.Label, labelValue.Value) as IFacetValue)));
}
}
}
40 changes: 40 additions & 0 deletions src/Examine.Lucene/Search/FacetFloatField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using Examine.Search;
using Lucene.Net.Facet;
using Lucene.Net.Facet.Range;
using Lucene.Net.Facet.SortedSet;
using Lucene.Net.Queries.Function.ValueSources;

namespace Examine.Lucene.Search
{
public readonly struct FacetFloatField : IFacetField
{
public FloatRange[] FloatRanges { get; }

public string Field { get; }

public string FacetField { get; }

public FacetFloatField(string field, FloatRange[] floatRanges, string facetField)
{
Field = field;
FloatRanges = floatRanges;
FacetField = facetField;
}

public void ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, Dictionary<string, IFacetResult> facets)
{
var floatFacetCounts = new DoubleRangeFacetCounts(Field, new SingleFieldSource(Field), facetsCollector, FloatRanges.AsLuceneRange().ToArray());

var floatFacets = floatFacetCounts.GetTopChildren(0, Field);

if (floatFacets == null)
{
return;
}

facets.Add(Field, new Examine.Search.FacetResult(floatFacets.LabelValues.Select(labelValue => new FacetValue(labelValue.Label, labelValue.Value) as IFacetValue)));
}
}
}
Loading