gpt4 book ai didi

c# - Lucene.Net 创建空索引

转载 作者:太空宇宙 更新时间:2023-11-03 14:08:31 25 4
gpt4 key购买 nike

我在我的 asp .net mvc 应用程序中使用 Lucene.Net + SimpleLucene。从数据库创建索引时出现问题。索引似乎开始创建得很好,但我得到的只是 5 个大小为 0 和 1 KB 的文件:

_0.fdt             0KB  _0.fdx             0KB  segment.gen        1KB  segments_1         1KB  write.lock         0KB

I have such a model from which i want to create index:

public class Feed : BaseEntity
{

public virtual string Address { get; set; }

public virtual string Title { get; set; }

public virtual string Description { get; set; }

public virtual bool IsExported { get; set; }

public virtual DateTime LastUpdateTime { get; set; }

public virtual int UserId { get; set; }

public virtual User User { get; set; }

public virtual ICollection<FeedPost> Posts { get; set; }

public virtual ICollection<Tag> Tags { get; set; }

public virtual Filter Filter { get; set; }
}

我已经从 lucene 库中实现了两个必要的接口(interface):IIndexDefinition 和 IResultDefinition:

public class FeedIndexDefinition : IIndexDefinition<Feed>
{
public Document Convert(Feed entity)
{
var document = new Document();
document.Add(new Field("id", entity.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("description", entity.Description, Field.Store.YES, Field.Index.ANALYZED));
document.Add(new Field("title", entity.Title, Field.Store.YES, Field.Index.ANALYZED));
return document;
}

public Term GetIndex(Feed entity)
{
return new Term("id", entity.Id.ToString());
}
}


public class SearchResultDefinition : IResultDefinition<Feed>
{
public Feed Convert(Document document)
{
Guid id = Guid.Parse(document.GetValue<String>("id"));
var context = new UnitOfWork();
Feed feed = context.FeedRepository.GetById(id);
return feed;
}
}

索引是这样执行的:

    private static readonly UnitOfWork Context = new UnitOfWork();
public static void IndexAllFeeds()
{
var indexWriter = new DirectoryIndexWriter(new DirectoryInfo(IndexPath), true);
var feeds = Context.FeedRepository.Get().AsEnumerable();
var indexService = new IndexService(indexWriter);
IEnumerable<Feed> array = feeds.ToList();
indexService.IndexWriter.IndexOptions.OptimizeIndex = true;
indexService.IndexEntities(array, new FeedIndexDefinition());
}

这是创建查询以执行搜索的方法

public FeedQuery Search(string keywords)
{
if(!String.IsNullOrEmpty(keywords))
{
String[] fields = {"title", "description"};
var parser = new MultiFieldQueryParser(
Lucene.Net.Util.Version.LUCENE_29,
fields,
new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
Query q = parser.Parse(keywords);
this.AddQuery(q);
}
return this;
}

最后是 Controller 中应该找到结果的方法

private static IEnumerable<Feed> SearchUserFeeds(string keywords)
{
keywords = "title5 description5 title4 description4 title3 description3";
IEnumerable<Feed> searchResults;
var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(LuceneHelper.IndexPath));
using (var searchService = new SearchService(indexSearcher))
{
var query = new FeedQuery().Search(keywords);
searchResults = searchService.SearchIndex(query.Query, new SearchResultDefinition()).Results;
}
return searchResults;
}

如果有人能够指出我的问题,我将不胜感激。

最佳答案

当您的 IndexWriter 未关闭或刷新时没关系。

如果这是关闭编写器后索引的凝视 - 尝试使用名为 lukeall 的第 3 方 java 工具检查索引

关于c# - Lucene.Net 创建空索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8583756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com