2
我在我的asp .net mvc应用程序中使用Lucene.Net + SimpleLucene。从数据库创建索引时出现问题。该指数似乎开始创建好,但我得到的是5档为0的大小和1 KB:Lucene.Net创建空索引
_0.fdt 0KB _0.fdx 0KB segment.gen 1KB segments_1 1KB write.lock 0KB
我有,我想创建索引这样的模式:
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的库中有两个必要的接口: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;
}
最后这里的方法是在控制的方法,应该找的结果
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;
}
如果有人能够指出我会感激的问题。
第三方的Java工具的索引,但我没有),Close()方法中的任何一种同花顺(我的indexWriter或indexService对象。 – RomanKapitonov 2011-12-21 11:51:44
当然它有Close()和Dispose() – 2011-12-21 12:37:01
没错。没有先通知。谢谢你的帮助。 – RomanKapitonov 2011-12-21 15:52:09