• Lucenet.Net学习笔记


    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Lucene.Net.Analysis;
    using Lucene.Net.Documents;
    using Lucene.Net.Index;
    using Lucene.Net.QueryParsers;
    using Lucene.Net.Search;
    using Lucene.Net.Store;
    using Lucene.Net.Analysis.Cn;

    namespace Lucene01
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                CreateLucenetIndex();
                ReadLucenetIndex();

            }



            
    /// <summary>
            
    /// 创建搜索
            
    /// </summary>
            protected static void CreateLucenetIndex()
            {
               
    //FSDirectory 建立基于文档索引   ,RAMDirectory 建立基于内存索引
              
    //   Directory dir = new FSDirectory();  //new RAMDirectory(); 
                Analyzer analyzer = new ChineseAnalyzer();
                IndexWriter writer 
    = new IndexWriter("rhythmKIndex", analyzer, true); //new IndexWriter(dir,analyzer,true);
                
    //IndexReader.IndexExists("rhythmKIndex"); 可以判断是否存在索引文件 
                 
    //故上面也可以用 new IndexWriter("rhythmkIndex",analyzer,true)
                Document doc = new Document();

                doc.Add(
    new Field("title""这只是一个标题", Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(
    new Field("content""我是一个中国人!I am  a chinese boy~", Field.Store.YES, Field.Index.TOKENIZED));
              
    //  doc.Add(new Field("dd",DateField.DateToString(); ;
                writer.AddDocument(doc);
                
                doc 
    = new Document();
                doc.Add(
    new Field("title""学习Lucene.Net", Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(
    new Field("content""今天是我学习Lucene.Net第一天!", Field.Store.YES, Field.Index.TOKENIZED));
                 doc.SetBoost(
    2.5f);//通过设置权重 优先级,让其排名靠前
               
                writer.AddDocument(doc);
                
                writer.Optimize();
                
                writer.Close();

            }
            
    /// <summary>
            
    /// 读取索引
            
    /// </summary>
            protected static void ReadLucenetIndex()
            {
                
    //Directory dir = FSDirectory.GetDirectory("path");//
                
    //dir = RAMDirectory(FSDirectory.GetDirectory("path"));//将指定文件下的索引读入内存
                IndexSearcher searcher = new IndexSearcher("rhythmKIndex");
                MultiFieldQueryParser mfq 
    = new MultiFieldQueryParser(new string[] { "title""content" }, new ChineseAnalyzer());
                Query query 
    = mfq.Parse("");

                Console.WriteLine(query.ToString());
    //打印索引语句
                Hits hit = searcher.Search(query);
                Console.WriteLine(
    "总共获取到相关数据:{0}条,索引库总数:", hit.Length(), searcher.Reader.NumDocs());

                
    for (int i = 0; i < hit.Length(); i++)
                {
                    
    int docId = hit.Id(i);
                    
    string title = hit.Doc(i).Get("title");
                    
    string content = hit.Doc(i).Get("content");
                    Console.WriteLine(
    "当前ID:{0}搜索到的标题:{1}内容:{2}", docId, title, content);

                }

                Console.ReadKey();

                 
            }
        }
    }

    学习lucenet.Net demo 以及学习资料下载 

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    【SpringBoot】 理解SpringBoot的启动原理
    【SpringBoot】SpringBoot的基础,全面理解bean的生命周期
    【转】 Linux 命令解释(Linux基础二)
    【转】 Linux 的目录详解 (Linux基础一)
    【SpringBoot】 一种解决接口返回慢的方式
    【Jmeter基础】 Linux上运行Jmeter
    【SpingBoot】 测试如何使用SpringBoot搭建一个简单后台1
    单例模式@Singleton在测试中的运用
    性能测试基础 ---TCP通信过程的状态码与过程,以及出现错误码的分析(TIME_WAIT,CLOSE_WAIT)
    Spring5源码分析(007)——IoC篇之加载BeanDefinition总览
  • 原文地址:https://www.cnblogs.com/rhythmK/p/1704104.html
Copyright © 2020-2023  润新知