你可以用Lucene.Net构建这样的解决方案。 将您的文档保存在数据库中(如已经)并使用您想要的Lucene.Net文档编制索引。
Lucene将在文件系统中拥有自己的索引。
您需要在DB和Lucene索引中为您的文档提供同步,因此当DB中的文档发生更改时,您需要使用Lucene重新索引它。 同步(DB和Lucene索引之间的匹配)可以基于来自DB的某个唯一键值(例如:ID)。
所以,当你想向Lucene索引添加一些文档时,你需要索引文档内容(你不需要在Lucene中保存内容)并使用DB中的唯一键值将其保存到Lucene中ID)。
然后您可以搜索Lucene索引并获取匹配文档ID的列表。 并通过这些ID从您的数据库中检索它们并向用户显示。
以下是我的项目的示例方法,它将文档添加到Lucene索引。 方法参数中的InformationAsset是来自数据库的文档,我想索引。 这种方法与创建“Lucene的文档”几“域”:
- “场”:从数据库中的文档的内容(InformationAsset从方法参数)
- “fieldId”:这是从数据库中InformationAsset的ID ,以匹配数据库和Lucene索引
- 'fieldPubDate':发布日期,我可以基于所有字段创建高级查询到Lucene引擎。
'fieldDataSource':它是某种类别。
public void AddToIndex(Entities.InformationAsset infAsset, IList<Keyword> additionalKeywords)
{
Analyzer analyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(LuceneDir, analyzer, false);
Document doc = new Document();
// string z dodatkowymi slowami po ktorych ma byc tez zindeksowana tresc
string addKeysStr = "";
if(additionalKeywords != null)
{
foreach (Keyword keyword in additionalKeywords)
{
addKeysStr += " " + keyword.Value;
}
}
addKeysStr += " " + m_RootKeyword;
string contentStr;
contentStr = infAsset.Title + " " + infAsset.Content + addKeysStr;
// indeksacja pola z trescia
Field field = new Field(LuceneFieldName.Content, contentStr, Field.Store.NO, Field.Index.TOKENIZED,
Field.TermVector.YES);
// pole z Id
Field fieldId = new Field(LuceneFieldName.Id, infAsset.Id.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED);
// pole publish date
Field fieldPubDate = new Field(LuceneFieldName.PublishDate,
DateTools.DateToString(infAsset.PublishingDate, DateTools.Resolution.MINUTE),
Field.Store.YES, Field.Index.NO_NORMS, Field.TermVector.YES);
// pole DataSource
// pole z Id
Field fieldDataSource = new Field(LuceneFieldName.DataSourceId, infAsset.DataSource.Id.ToString(), Field.Store.YES,
Field.Index.UN_TOKENIZED);
doc.Add(field);
doc.Add(fieldId);
doc.Add(fieldPubDate);
doc.Add(fieldDataSource);
doc.SetBoost((float)CalculateDocBoostForInfAsset(infAsset));
indexWriter.AddDocument(doc);
indexWriter.Optimize();
indexWriter.Close();
}
我认为,同样的逻辑也将工作,但我们不存储在文档中的CRM数据库中,我们存储文本,看起来像一篇文章的描述字段,但我可以拉说出来的如果它是一个文档。谢谢! – Marsharks 2010-01-28 22:50:07