首页 > 学院 > 开发设计 > 正文

【转载】使用Lucene.NET实现数据检索功能

2019-11-17 01:42:17
字体:
来源:转载
供稿:网友

【转载】使用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.VEVb.com/liusuqi/p/3671161.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表