zoukankan      html  css  js  c++  java
  • These codes are How to use Lucence.net

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Lucene.Net.Analysis;
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Documents;
    using Lucene.Net.Index;
    using Lucene.Net.QueryParsers;
    using Lucene.Net.Search;
    using Lucene.Net.Store;
    
    namespace Basic_Concepts
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Setup indexer
    
                Directory directory = FSDirectory.GetDirectory("LuceneIndex");
                Analyzer analyzer = new StandardAnalyzer();
                IndexWriter writer = new IndexWriter(directory, analyzer);
    
                IndexReader red = IndexReader.Open(directory);
                int totDocs = red.MaxDoc();
                red.Close();
    
                //Add documents to the index
                string text = String.Empty;
                Console.WriteLine("Enter the text you want to add to the index:");
                Console.Write(">");
                int txts = totDocs;
                int j = 0;
                while ((text = Console.ReadLine()) != String.Empty)
                {
                    AddTextToIndex(txts++, text, writer);
                    j++;
                    Console.Write(">");
                }
    
                
                writer.Optimize();
                //Close the writer
                writer.Flush();
                writer.Close();
    
                Console.WriteLine(j + " lines added, "+txts+" documents total");
    
                //Setup searcher
                IndexSearcher searcher = new IndexSearcher(directory);
                QueryParser parser = new QueryParser("postBody", analyzer);
    
    
                Console.WriteLine("Enter the search string:");
                Console.Write(">");
    
                while ((text = Console.ReadLine()) != String.Empty)
                {
                    Search(text, searcher, parser);
                    Console.Write(">");
                }
    
                //Clean up everything
                searcher.Close();
                directory.Close();
            }
    
            private static void Search(string text, IndexSearcher searcher, QueryParser parser)
            {
                //Supply conditions
                Query query = parser.Parse(text);
    
                //Do the search
                Hits hits = searcher.Search(query);
    
                //Display results
                Console.WriteLine("Searching for '" + text + "'");
                int results = hits.Length();
                Console.WriteLine("Found {0} results", results);
                for (int i = 0; i < results; i++)
                {
                    Document doc = hits.Doc(i);
                    float score = hits.Score(i);
                    Console.WriteLine("--Result num {0}, score {1}", i + 1, score);
                    Console.WriteLine("--ID: {0}", doc.Get("id"));
                    Console.WriteLine("--Text found: {0}" + Environment.NewLine, doc.Get("postBody"));
                }
            }
    
            private static void AddTextToIndex(int txts, string text, IndexWriter writer)
            {
                Document doc = new Document();
                doc.Add(new Field("id", txts.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("postBody", text, Field.Store.YES, Field.Index.TOKENIZED));
                writer.AddDocument(doc);
            }
        }
    }
  • 相关阅读:
    指定的 LINQ 表达式包含对与不同上下文关联的查询的引用
    无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类
    获取路径 GetModuleFileName() GetModuleFileName() GetCurrentDirectory
    重启动界面 ShellExecute() ShellExecute()
    MFC 强大功能函数
    extern 头文件 定义&声明
    全局变量/常量
    有时不需要头文件包含也能编译过
    字符串(3):字符串与函数
    函数不能传递动态内存
  • 原文地址:https://www.cnblogs.com/jameslif/p/2971401.html
Copyright © 2011-2022 走看看