//Two-pass grouping search with cacheing (kinda working) private void TwoPassGroupingSearch(Directory directory) { var indexReader = DirectoryReader.Open(directory); var indexSearcher = new IndexSearcher(indexReader); //GroupingSearch groupingSearch = new GroupingSearch("Repetition"); GroupingSearch groupingSearch = new GroupingSearch("Category"); groupingSearch.SetAllGroups(true); groupingSearch.SetGroupDocsLimit(10); groupingSearch.SetCachingInMB(40.0, true); MatchAllDocsQuery all_query = new MatchAllDocsQuery(); NumericRangeQuery <int> numeric_query = NumericRangeQuery.NewInt32Range("Repetition", 1, 2, true, false); TermQuery term_query = new TermQuery(new Term("Category", "Cat 1")); //Use different queries above to test var topGroups = groupingSearch.Search(indexSearcher, term_query, 0, 10); Console.WriteLine("Total group count: " + topGroups.TotalGroupCount); Console.WriteLine("Total group hit count: " + topGroups.TotalGroupedHitCount); foreach (var groupDocs in topGroups.Groups) { Console.WriteLine("Group: " + ((BytesRef)groupDocs.GroupValue).Utf8ToString()); foreach (var scoreDoc in groupDocs.ScoreDocs) { var doc = indexSearcher.Doc(scoreDoc.Doc); Console.WriteLine("Category: " + doc.GetField("Category").GetStringValue() + ", BookId: " + doc.GetField("BookId").GetStringValue() + ", Rep: " + doc.GetField("Repetition").GetInt32Value()); } } indexReader.Dispose(); }
private IEnumerable <KeyValuePair <string, int> > _GroupBy(int skip, int pageSize, string fieldName) { GroupingSearch groupingSearch = new GroupingSearch(fieldName); groupingSearch.SetGroupSort(Sort.RELEVANCE); groupingSearch.SetFillSortFields(false); groupingSearch.SetCachingInMB(40.0, true); groupingSearch.SetAllGroups(true); // Render groupsResult... try { var reader = DirectoryReader.Open(_directory); var searcher = new Lucene.Net.Search.IndexSearcher(reader); Sort groupSort = Sort.RELEVANCE; int groupOffset = 0; int groupLimit = 10000000; string rawQuery = _queryProvider.GetBooleanQuery().ToString(); if (!rawQuery.Contains("isdeleted")) { rawQuery += "+isdeleted:0"; } var queryParser = new QueryParser(LuceneVersion.LUCENE_48, "isdeleted", analyzer); queryParser.AllowLeadingWildcard = _queryProvider.GetContainsWildCard(); var query = queryParser.Parse(rawQuery); ITopGroups <object> result = groupingSearch.Search(searcher, query, groupOffset, groupLimit); if (result.Groups == null || result.Groups.Count() <= 0) { return(new List <KeyValuePair <string, int> >()); } var d = result.Groups.OrderByDescending(p => p.TotalHits).ToList(); if (d.FirstOrDefault().GroupValue == null) { d.RemoveAt(0); } _groupCount = d.Count; if (pageSize > d.Count) { pageSize = d.Count; } d = d.Skip(skip).Take(pageSize).ToList(); if (d.Count > 0) { var rs = d.Select(p => new KeyValuePair <string, int>(((BytesRef)p.GroupValue)?.Utf8ToString(), p.TotalHits)).ToList(); return(rs); } else { return(new List <KeyValuePair <string, int> >()); } } catch { throw; } finally { } }