Lucene.net 基本示例 《第一篇》

发布日期:2018-02-09    浏览次数:546

  Lucene.net是java平台搜索插件Lucene的移植版。它的主要用于开发搜索引擎,站内搜索等。

  开篇之前,写个最简单的DEMO,让自己先体验下Lucene.net的魅力,顺便搭建环境。

        static void Main(string[] args)
        {
            //写入数据到索引
            Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            Directory directory = new RAMDirectory();
            IndexWriter.MaxFieldLength maxFieldLength = new IndexWriter.MaxFieldLength(10000);

            using (IndexWriter writer = new IndexWriter(directory, analyzer, maxFieldLength))
            {
                Document document1 = new Document();
                document1.Add(new Field("Sentence", "刘备", Field.Store.YES, Field.Index.ANALYZED));
                writer.AddDocument(document1);

                Document document2 = new Document();
                document2.Add(new Field("Sentence", "张飞", Field.Store.YES, Field.Index.ANALYZED));
                writer.AddDocument(document2);

                Document document3 = new Document();
                document3.Add(new Field("Sentence", "关羽", Field.Store.YES, Field.Index.ANALYZED));
                writer.AddDocument(document3);

                writer.Optimize();
            }

            //查找
            using (IndexSearcher searcher = new IndexSearcher(directory))
            {
                Term t = new Term("Sentence", "");
                Query query = new TermQuery(t);
                TopDocs docs = searcher.Search(query, null, 1000);
                Console.WriteLine(docs.TotalHits);
                Console.WriteLine(docs.ScoreDocs[0].Doc);
                Document doc = searcher.Doc(docs.ScoreDocs[0].Doc);
                Console.WriteLine(doc.Get("Sentence"));
            }
            Console.ReadKey();
        }

  以上示例输出如下:

  

 

本文网址:https://www.wyxxw.cn/blog-detail-2-6-249.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:http://www.cnblogs.com/kissdodog/p/3625988.html 。 转载人:momo