lucene.net排序不工作访问违规

本文关键字:访问 工作 net 排序 lucene | 更新日期: 2023-09-27 18:24:45

我正在尝试在lucene 中对结果进行排序

我一直得到这个错误,然而

Search.dll 中发生类型为"System.AccessViolationException"的未处理异常

附加信息:试图读取或写入受保护的内存。这通常表示其他内存已损坏。

我试过将Field.Index设置为已分析和未分析但没有乐趣。

Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Title", analyzer);
Query query = parser.Parse(searchTerm.Trim() + "*");
var searcher = new IndexSearcher(directory, true);
var sortBy = new Lucene.Net.Search.Sort(new Lucene.Net.Search.SortField("Title", Lucene.Net.Search.SortField.STRING, true));
var filter = new QueryWrapperFilter(query);
// TopDocs topDocs3 = searcher.Search(query, filter, 500,sortBy);
// TopDocs topDocs = searcher.Search(query,500);
TopDocs topDocs2 = searcher.Search(query,null, 500, new Sort(new SortField("Title", SortField.STRING)));
var re = searcher.Search(query, null, 10, new Sort(new SortField("id", SortField.INT, true)));

lucene.net排序不工作访问违规

我在LUCENE_30中尝试排序搜索结果时遇到了同样的错误。我必须说,我写这个例子很匆忙,没有经过测试。我所做的是:

string sortText = Enum.GetName(typeof(SortableFields), sortBy);
        SortField field = new SortField(sortText, SortField.STRING, sortDesc);
        var sortByField = new Lucene.Net.Search.Sort(field);
        TopFieldCollector collector = Lucene.Net.Search.TopFieldCollector.Create(sortByField, MaxSearchResultsReturned, false, false, false, false);
        using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
        {
            var queryParse = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, IndexFields.FullText, analyzer);
            queryParse.AllowLeadingWildcard = true;
            Query query = queryParse.Parse(searchText);
            using (var searcher = new IndexSearcher(directory, true))
            {
                searcher.Search(query, collector);
                totalRows = collector.TotalHits;
                TopDocs matches = collector.TopDocs(skip, take);
                // convert results to known objects
                var results = new List<SearchResult>();
                foreach (var item in matches.ScoreDocs)
                {
                    int id = item.Doc;
                    Document doc = searcher.Doc(id);
                    SearchResult result = new SearchResult();
                    result.ID = doc.GetField("ID").StringValue;
                    results.Add(result);
                }
            }
        }
              return results;