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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    was控制台误禁用后的恢复启用办法
    Linux升级内核教程(CentOS7)
    ifcfg-eth配置详解(CentOS6)
    CentOS7和CentOS6的区别
    ftp/sftp定时自动上传文件脚本(CentOS)
    AIX安装JDK1.7教程
    PE文件结构解析
    ffmpeg+libmp3lame库源码安装教程(CentOS)
    kafka安装使用教程
    Weblogic禁用SSLv3和RC4算法教程
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/1019067.html
Copyright © 2011-2022 走看看