ITPub博客

首页 > IT职业 > IT生活 > Lucene2.1 的官方示例代码

Lucene2.1 的官方示例代码

IT生活 作者:fengwei 时间:2021-09-09 09:15:09 0 删除 编辑

图片描述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             图片描述{
图片描述                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(); 
图片描述        }

图片描述    }

图片描述}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2799/viewspace-2809017/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    90
  • 访问量
    676