Skip to content

Commit

Permalink
Introduce IFacetExtractionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nzdev committed Mar 20, 2023
1 parent 1b08a17 commit 226245e
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/DateTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override Query GetQuery(DateTime? lower, DateTime? upper, bool lowerInclu
upper != null ? DateToLong(upper.Value) : (long?)null, lowerInclusive, upperInclusive);
}

public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/DoubleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override Query GetQuery(double? lower, double? upper, bool lowerInclusive
lower ?? double.MinValue,
upper ?? double.MaxValue, lowerInclusive, upperInclusive);
}
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/FullTextType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public override Query GetQuery(string query)
return GenerateQuery(FieldName, query, _analyzer);
}

public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
4 changes: 1 addition & 3 deletions src/Examine.Lucene/Indexing/IIndexFacetValueType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using Examine.Lucene.Search;
using Examine.Search;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Indexing
{
Expand All @@ -15,6 +13,6 @@ public interface IIndexFacetValueType
/// <param name="sortedSetReaderState"></param>
/// <param name="field"></param>
/// <returns>A dictionary of facets for this field</returns>
IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field);
IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field);
}
}
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/Int32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override Query GetQuery(int? lower, int? upper, bool lowerInclusive = tru
upper, lowerInclusive, upperInclusive);
}

public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/Int64Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override Query GetQuery(long? lower, long? upper, bool lowerInclusive = t
lower,
upper, lowerInclusive, upperInclusive);
}
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/SingleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override Query GetQuery(float? lower, float? upper, bool lowerInclusive =
upper ?? float.MaxValue, lowerInclusive, upperInclusive);
}

public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState, IFacetField field)
=> field.ExtractFacets(facetsCollector, sortedSetReaderState);
public virtual IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext, IFacetField field)
=> field.ExtractFacets(facetExtractionContext);
}
}
6 changes: 2 additions & 4 deletions src/Examine.Lucene/Search/FacetDoubleField.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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
{
Expand All @@ -22,9 +20,9 @@ public FacetDoubleField(string field, Examine.Search.DoubleRange[] doubleRanges,
FacetField = facetField;
}

public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState)
public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext)
{
var doubleFacetCounts = new DoubleRangeFacetCounts(Field, facetsCollector, DoubleRanges.AsLuceneRange().ToArray());
var doubleFacetCounts = new DoubleRangeFacetCounts(Field, facetExtractionContext.FacetsCollector, DoubleRanges.AsLuceneRange().ToArray());

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

Expand Down
7 changes: 3 additions & 4 deletions src/Examine.Lucene/Search/FacetFloatField.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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
Expand All @@ -23,9 +21,9 @@ public FacetFloatField(string field, FloatRange[] floatRanges, string facetField
FacetField = facetField;
}

public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState)
public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext)
{
var floatFacetCounts = new DoubleRangeFacetCounts(Field, new SingleFieldSource(Field), facetsCollector, FloatRanges.AsLuceneRange().ToArray());
var floatFacetCounts = new DoubleRangeFacetCounts(Field, new SingleFieldSource(Field), facetExtractionContext.FacetsCollector, FloatRanges.AsLuceneRange().ToArray());

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

Expand All @@ -36,5 +34,6 @@ public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsColle

yield return new KeyValuePair<string, IFacetResult>(Field, new Examine.Search.FacetResult(floatFacets.LabelValues.Select(labelValue => new FacetValue(labelValue.Label, labelValue.Value) as IFacetValue)));
}

}
}
5 changes: 2 additions & 3 deletions src/Examine.Lucene/Search/FacetFullTextField.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Examine.Search;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Search
Expand All @@ -24,9 +23,9 @@ public FacetFullTextField(string field, string[] values, string facetField, int
MaxCount = maxCount;
}

public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState)
public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext)
{
var sortedFacetsCounts = new SortedSetDocValuesFacetCounts(sortedSetReaderState, facetsCollector);
var sortedFacetsCounts = new SortedSetDocValuesFacetCounts(facetExtractionContext.GetSortedSetReaderState(FacetField), facetExtractionContext.FacetsCollector);

if (Values != null && Values.Length > 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Search/FacetLongField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public FacetLongField(string field, Examine.Search.Int64Range[] longRanges, stri
FacetField = facetField;
}

public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState)
public IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext)
{
var longFacetCounts = new Int64RangeFacetCounts(Field, facetsCollector, LongRanges.AsLuceneRange().ToArray());
var longFacetCounts = new Int64RangeFacetCounts(Field, facetExtractionContext.FacetsCollector, LongRanges.AsLuceneRange().ToArray());

var longFacets = longFacetCounts.GetTopChildren(0, Field);

Expand Down
12 changes: 12 additions & 0 deletions src/Examine.Lucene/Search/IFacetExtractionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Search
{
public interface IFacetExtractionContext
{
FacetsCollector FacetsCollector { get; }

SortedSetDocValuesReaderState GetSortedSetReaderState(string facetFieldName);
}
}
4 changes: 1 addition & 3 deletions src/Examine.Lucene/Search/IFacetField.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Collections.Generic;
using Examine.Search;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;

namespace Examine.Lucene.Search
{
Expand All @@ -23,6 +21,6 @@ public interface IFacetField
/// <param name="facetsCollector"></param>
/// <param name="sortedSetReaderState"></param>
/// <returns>Returns the facets for this field</returns>
IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(FacetsCollector facetsCollector, SortedSetDocValuesReaderState sortedSetReaderState);
IEnumerable<KeyValuePair<string, IFacetResult>> ExtractFacets(IFacetExtractionContext facetExtractionContext);
}
}
31 changes: 31 additions & 0 deletions src/Examine.Lucene/Search/LuceneFacetExtractionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Lucene.Net.Facet;
using Lucene.Net.Facet.SortedSet;
using Lucene.Net.Index;

namespace Examine.Lucene.Search
{
public class LuceneFacetExtractionContext : IFacetExtractionContext
{
private readonly IndexReader _indexReader;

private SortedSetDocValuesReaderState _sortedSetReaderState = null;

public LuceneFacetExtractionContext(FacetsCollector facetsCollector, IndexReader indexReader)
{
FacetsCollector = facetsCollector;
_indexReader = indexReader;
}

public FacetsCollector FacetsCollector { get; }

public virtual SortedSetDocValuesReaderState GetSortedSetReaderState(string facetFieldName)
{
if (_sortedSetReaderState == null || !_sortedSetReaderState.Field.Equals(facetFieldName))
{
_sortedSetReaderState = new DefaultSortedSetDocValuesReaderState(_indexReader, facetFieldName);
}
return _sortedSetReaderState;
}
}
}
7 changes: 2 additions & 5 deletions src/Examine.Lucene/Search/LuceneSearchExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,9 @@ private IReadOnlyDictionary<string, IFacetResult> ExtractFacets(FacetsCollector
var valueType = _searchContext.GetFieldValueType(field.Field);
if(valueType is IIndexFacetValueType facetValueType)
{
if (field is FacetFullTextField && (sortedSetReaderState == null || !sortedSetReaderState.Field.Equals(field.FacetField)))
{
sortedSetReaderState = new DefaultSortedSetDocValuesReaderState(searcher.IndexSearcher.IndexReader, field.FacetField);
}
var facetExtractionContext = new LuceneFacetExtractionContext(facetsCollector, searcher.IndexSearcher.IndexReader);

var fieldFacets = facetValueType.ExtractFacets(facetsCollector, sortedSetReaderState, field);
var fieldFacets = facetValueType.ExtractFacets(facetExtractionContext, field);
foreach(var fieldFacet in fieldFacets)
{
// overwrite if necessary (no exceptions thrown in case of collision)
Expand Down
2 changes: 1 addition & 1 deletion src/Examine.Lucene/Search/LuceneSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal IFacetOperations FacetInternal(string field, Action<IFacetQueryField> f
{
values = Array.Empty<string>();
}

var fieldValueType = _searchContext.GetFieldValueType(field);
var facet = new FacetFullTextField(field, values, GetFacetField(field));

if(facetConfiguration != null)
Expand Down

0 comments on commit 226245e

Please sign in to comment.