private List<FacetMatch> FindMatchesInQuery(Facet facet, Filter query, Filter filter, IndexReader indexReader) { var matches = facet.Values.Select(value => { var bitsQuery = new OpenBitSetDISI(query.GetDocIdSet(indexReader).Iterator(), indexReader.MaxDoc()); bitsQuery.And(value.Item2); if (filter != null) { //TODO: Remove this hard coded value (1000) var bitsFilter = new OpenBitSetDISI(filter.GetDocIdSet(indexReader).Iterator(), 1000); bitsQuery.And(bitsFilter); } var count = bitsQuery.Cardinality(); return new FacetMatch() { Count = count, Value = value.Item1, Id = facet.Id }; }).ToList(); return matches; }
private IEnumerable<FacetMatch> FindMatchesInQuery(Query baseQueryWithoutFacetDrilldown, IList<FacetFieldInfo> allFacetFieldInfos, FacetFieldInfo facetFieldInfoToCalculateFor) { var calculations = 0; var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(CreateFacetedQuery(baseQueryWithoutFacetDrilldown, allFacetFieldInfos, facetFieldInfoToCalculateFor.FieldName))); var bitsQueryWithoutFacetDrilldown = new OpenBitSetDISI(queryFilter.GetDocIdSet(IndexReader).Iterator(), IndexReader.MaxDoc); var baseQueryWithoutFacetDrilldownCopy = new OpenBitSetDISI(bitsQueryWithoutFacetDrilldown.Bits.Length) { Bits = new long[bitsQueryWithoutFacetDrilldown.Bits.Length] }; var calculatedFacetCounts = new ResultCollection(facetFieldInfoToCalculateFor); foreach (var facetValueBitSet in GetOrCreateFacetBitSet(facetFieldInfoToCalculateFor.FieldName).FacetValueBitSetList) { var isSelected = calculatedFacetCounts.IsSelected(facetValueBitSet.Value); if (!isSelected && facetValueBitSet.Count < calculatedFacetCounts.MinCountForNonSelected) //Impossible to get a better result { if (calculatedFacetCounts.HaveEnoughResults) break; } bitsQueryWithoutFacetDrilldown.Bits.CopyTo(baseQueryWithoutFacetDrilldownCopy.Bits, 0); baseQueryWithoutFacetDrilldownCopy.NumWords = bitsQueryWithoutFacetDrilldown.NumWords; var bitset = facetValueBitSet.Bitset ?? CalculateOpenBitSetDisi(facetFieldInfoToCalculateFor.FieldName, facetValueBitSet.Value); baseQueryWithoutFacetDrilldownCopy.And(bitset); var count = baseQueryWithoutFacetDrilldownCopy.Cardinality(); if (count == 0) continue; var match = new FacetMatch { Count = count, Value = facetValueBitSet.Value, FacetFieldName = facetFieldInfoToCalculateFor.FieldName }; calculations++; if (isSelected) calculatedFacetCounts.AddToSelected(match); else calculatedFacetCounts.AddToNonSelected(match); } return calculatedFacetCounts.GetList(); }
// TODO: in 3.0, instead of removing this deprecated // method, make it a no-op and mark it final /** Provide a SortedVIntList when it is definitely smaller * than an OpenBitSet. * @deprecated Either use CachingWrapperFilter, or * switch to a different DocIdSet implementation yourself. */ protected DocIdSet FinalResult(OpenBitSetDISI result, int maxDocs) { return (result.Cardinality() < (maxDocs / 9)) ? (DocIdSet)new SortedVIntList(result) : (DocIdSet)result; }
public SimpleFacetedSearch(IndexReader reader, string[] groupByFields) { this._Reader = reader; List<FieldValuesBitSets> fieldValuesBitSets = new List<FieldValuesBitSets>(); //STEP 1 //f1 = A, B //f2 = I, J //f3 = 1, 2, 3 int maxFacets = 1; List<List<string>> inputToCP = new List<List<string>>(); foreach (string field in groupByFields) { FieldValuesBitSets f = new FieldValuesBitSets(reader, field); maxFacets *= f.FieldValueBitSetPair.Count; if (maxFacets > MAX_FACETS) throw new Exception("Facet count exceeded " + MAX_FACETS); fieldValuesBitSets.Add(f); inputToCP.Add(f.FieldValueBitSetPair.Keys.ToList()); } //STEP 2 // comb1: A I 1 // comb2: A I 2 etc. var cp = inputToCP.CartesianProduct(); //SETP 3 //create a single BitSet for each combination //BitSet1: A AND I AND 1 //BitSet2: A AND I AND 2 etc. //and remove impossible comb's (for ex, B J 3) from list. Parallel.ForEach(cp, combinations => { OpenBitSetDISI bitSet = new OpenBitSetDISI(_Reader.MaxDoc()); bitSet.Set(0, bitSet.Size()); List<string> comb = combinations.ToList(); for (int j = 0; j < comb.Count; j++) { bitSet.And(fieldValuesBitSets[j].FieldValueBitSetPair[comb[j]]); } //STEP 3 if (bitSet.Cardinality() > 0) { lock(_Groups) _Groups.Add(new KeyValuePair<List<string>, OpenBitSetDISI>(comb, bitSet)); } }); //Now _Groups has 7 rows (as <List<string>, BitSet> pairs) }
public IEnumerable<IHit> Query(int pageIndex, int pageSize, out int totalCount, out IEnumerable<FacetGroup> facetedResults) { totalCount = 0; facetedResults = null; if (searchPaths == null || searchPaths.Count <= 0) searchPaths.AddRange(indexPaths.Values.Select(o => o.Path)); List<LuceneHit> results = new List<LuceneHit>(); List<IndexSearcher> subSearchs = new List<IndexSearcher>(); searchPaths.ForEach(o => subSearchs.Add(new IndexSearcher(FSDirectory.Open(o)))); if (facetFields != null && facetFields.Count > 0) { var facetGroups = new List<FacetGroup>(); var mainQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query)); MultiReader readers = new MultiReader(subSearchs.Select(o => o.IndexReader).ToArray()); foreach (var facetField in facetFields) { FacetGroup fg = new FacetGroup(); fg.FieldName = facetFieldNameProvider.GetMapName(TypeName, facetField); var items = new List<FacetItem>(); var allDistinctField = FieldCache_Fields.DEFAULT.GetStrings(readers, facetField).Distinct().ToArray(); int totalHits = 0; Parallel.ForEach(allDistinctField, fieldValue => { //foreach (var fieldValue in allDistinctField) //{ var facetQuery = new TermQuery(new Term(facetField, fieldValue)); var facetQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(facetQuery)); var bs = new OpenBitSetDISI(facetQueryFilter.GetDocIdSet(readers).Iterator(), readers.MaxDoc); bs.InPlaceAnd(mainQueryFilter.GetDocIdSet(readers).Iterator()); int count = (Int32)bs.Cardinality(); FacetItem item = new FacetItem(); item.GroupValue = fieldValue; item.Count = count; items.Add(item); totalHits += count; } ); fg.FacetItems = items.OrderByDescending(o => o.Count); fg.TotalHits = totalHits; facetGroups.Add(fg); } facetedResults = facetGroups.OrderBy(o => o.FieldName); } ParallelMultiSearcher searcher = new ParallelMultiSearcher(subSearchs.ToArray()); Sort sort = null; if (sortFields != null && sortFields.Count > 0) { sort = new Sort(sortFields.ToArray()); } int maxDoc = searcher.MaxDoc; int startIndex = 0; if (pageIndex >= 0 && pageSize > 0) { startIndex = pageIndex * pageSize; maxDoc = pageSize * (pageIndex + 1); } var docs = sort == null ? searcher.Search(query, null, maxDoc) : searcher.Search(query, null, maxDoc, sort); totalCount = docs.TotalHits; int endIndex = docs.TotalHits - startIndex; for (int i = startIndex; i < endIndex; i++) { LuceneHit h = new LuceneHit(TypeName, DocumentBuilder, searcher.Doc(docs.ScoreDocs[i].Doc)); results.Add(h); } return results; }
private static long CalculateFacetCount(DocIdSet baseBitSet, DocIdSet filterDocSet) { var baseDisi = new OpenBitSetDISI(baseBitSet.Iterator(), 25000); var filterIterator = filterDocSet.Iterator(); baseDisi.InPlaceAnd(filterIterator); var total = baseDisi.Cardinality(); return total; }