zoukankan      html  css  js  c++  java
  • Lucene2.1 的官方示例代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using Lucene.Net;
    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;
    using Lucene.Net.Util;





    namespace LuceneTest
    {
        
    public partial class Form1 : Form
        
    {
            
    public Form1()
            
    {
                InitializeComponent();
            }


            
    private void Form1_Load(object sender, EventArgs e)
            
    {
                Test();
            }



            
    void Test() 
            
    {
                Analyzer analyzer 
    = new StandardAnalyzer();

                
    // Store the index in memory:
                Directory directory = new RAMDirectory();
                
    // To store an index on disk, use this instead (note that the 
                
    // parameter true will overwrite the index in that directory
                
    // if one exists):
                
    //Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
                IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
               
                iwriter.SetMaxFieldLength(
    25000);
                Document doc 
    = new Document();
                String text 
    = "This is the text to be indexed.";
                doc.Add(
    new Field("fieldname", text, Field.Store.YES,
                    Field.Index.TOKENIZED));
                iwriter.AddDocument(doc);
                iwriter.Close();

                
    // Now search the index:
                IndexSearcher isearcher = new IndexSearcher(directory);
                
    // Parse a simple query that searches for "text":
                QueryParser parser = new QueryParser("fieldname", analyzer);
                Query query 
    = parser.Parse("text");
                Hits hits 
    = isearcher.Search(query);
                
    //assertEquals(1, hits.Length());
                
    // Iterate through the results:
                for (int i = 0; i < hits.Length(); i++)
                
    {
                    Document hitDoc 
    = hits.Doc(i);
                    
    //AssertEquals("This is the text to be indexed.", hitDoc.Get("fieldname"));
                    MessageBox.Show(hitDoc.Get("fieldname"));
                }

                isearcher.Close();
                directory.Close(); 
            }

        }

    }
    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    通过C#的HttpClient模拟form表单请求
    通过Mysql连接ASP.Net Core2.0(Code First模式)
    每天一点产品思考(1):微博是否可以尝试取消点赞数展示?
    Google Analytics 学习笔记四 —— GA的Channels划分规则
    Google Analytics 学习笔记三 —— GA常用术语
    Google Analytics 学习笔记二 —— GA部署
    Google Analytics 学习笔记一 —— GA简介
    java基础知识总结(一)
    java源码解析之String类(二)
    java源码解析之String类(一)
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/1019067.html
Copyright © 2011-2022 走看看