使用Lucene.NET实现数据检索功能

1、索引的管理

//指定索引库文件存放文件位置
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(this.IndexDataDir), new NativeFSLockFactory());
//判断索引文件目录是否存在
bool isExist = IndexReader.IndexExists(directory);
if (isExist)
{
    if (IndexWriter.IsLocked(directory))
    {
        IndexWriter.Unlock(directory);
    }
}
//盘古分词器
PanGuAnalyzer analyzer = new PanGuAnalyzer();
//索引写入类
IndexWriter writer = new IndexWriter(directory, analyzer, !isExist, IndexWriter.MaxFieldLength.UNLIMITED);
//循环队列执行操作
while (IndexDataQueue.Count > 0)
{
    Document document = new Document();
//这是我为索引数据自定义的模型类,主要内容是文件的路径、名称、内容和索引管理的操作类型(新增、更新、删除)
    BaseDataMode mode = IndexDataQueue.Dequeue();
    switch (mode.Type)
    {
        case OperationType.Insert:
            {
                foreach (KeyValuePair<string, string> kv in mode.Content)
                {
                    //这里kv.Key是设置索引内字段的名称,kv.Value是这个字段内存储的内容。
                    document.Add(new Field(kv.Key, kv.Value, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
                }
                writer.AddDocument(document);
            }; break;
        case OperationType.Update:
        {
            //设置删除条件
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "id" }, analyzer);
            Query query = parser.Parse(mode.Content["id"]);
            writer.DeleteDocuments(query);
            foreach (KeyValuePair<string, string> kv in mode.Content)
            {
                document.Add(new Field(kv.Key, kv.Value, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
            }
            writer.AddDocument(document);
        }; break;
        case OperationType.Delete:
        {
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "id" }, analyzer);
            Query query = parser.Parse(mode.Content["id"]);
            writer.DeleteDocuments(query);
        }; break;
        default: { }; break;
    }
}
//提交操作
writer.Commit();
//优化
writer.Optimize();
//关闭连接
writer.Close();
directory.Close();

2、检索

//指定索引库文件存放文件位置
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(this.IndexDir), new NativeFSLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//设置关键词在条件中为OR关系
BooleanQuery queryOr = new BooleanQuery();
foreach (string word in SplitContent.SplitByPanGu(keyword))
{
    foreach (KeyValuePair<string, string> kv in Mode.Content)
    {
        TermQuery query = new TermQuery(new Term(kv.Key, word));
        //这里设置条件为Or关系
        queryOr.Add(query, BooleanClause.Occur.SHOULD);
    }
}
//获取搜索结果       
//1000为搜索文件的下标限制,设置这个可以控制检索的范围,也可以用于分页显示
TopDocs tds = searcher.Search(queryOr, null, 1000);
ScoreDoc[] docs = tds.scoreDocs;
for (int i = 0; i < docs.Length; i++)
{
    int docId = docs[i].doc;
    Document doc = searcher.Doc(docId);
string content = doc.Get("索引内字段的名称");
}

转自:http://www.cnblogs.com/liusuqi/p/3671161.html

【转载】使用Lucene.NET实现数据检索功能,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。