你可以用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