lucene.net排序不工作访问冲突

时间:2014-09-28 10:48:22

标签: c# lucene.net

我正在尝试在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)));

1 个答案:

答案 0 :(得分:0)

尝试在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;