Search code examples
c#.netlucene.net

Grouping on Lucene.net


`

 public static async Task<List<string>> SearchGroup(string filedName, Query bq, Filter fil, IndexSearcher searcher)
        {
            //分组的最大数量
            int groupNum = 100;
            return await Task.Run(() =>
            {
                GroupingSearch groupingSearch = new GroupingSearch(filedName);
                groupingSearch.SetCachingInMB(8192, cacheScores: true);
                //不做排序,浪费性能
                //groupingSearch.SetGroupSort(new Sort(new SortField("name", SortFieldType.STRING)));
                groupingSearch.SetGroupDocsLimit(groupNum);
                ITopGroups<BytesRef> topGroups = groupingSearch.SearchByField(searcher, fil, bq, groupOffset: 0, groupNum);
                List<string> groups = new List<string>();
                foreach (var groupDocs in topGroups.Groups.Take(groupNum))
                {
                    if (groupDocs.GroupValue != null)
                    {
                        groups.Add(groupDocs.GroupValue.Utf8ToString());
                    }
                }
                return groups;
            });
        }

`

Here is my current code for grouping, but there are performance issues. The time for each call is equal to the time for one query. If I group multiple fields at the same time, it is very time-consuming. Is there any way to improve the speed?

enter image description here

There will be multiple screening items, but it is too time-consuming

Hope to have fast grouping results, or grouping at the same time


Solution

    1. You have to add cache - group values usual not changing very frequently. So it is better to pre-cache data before execute search request. And then use such information every next query of same information. To develop this look like you have to customzie lucene engine (searcher).

    2. in my case i am using faceted filters instead of groups. Difference that groups shows sub content as tree view (above group), filters - all data shows as one set but can be easelly and quickly filters as on screenshot List item

    Filters is not answer for all questions/problems. There is no information about task which you are going to sove with grouping - so i can't answer more exactly.

    So cache as much as possible - is answer for 99% of such issues. Probably change strtategy to use facet filters - another way to improve issue/situation.