Lucene.Net

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

using System.IO;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Documents;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
namespace WindowsFormsApp1
{
    //http://www.zhuzhusoft.com/article.php?id=151
    //Install-Package Lucene.Net -Pre
    //Install-Package Lucene.Net.Analysis.Common -Version 4.8.0-beta00014
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;

                // Ensures index backward compatibility
                const LuceneVersion AppLuceneVersion = LuceneVersion.LUCENE_48;

                // Construct a machine-independent path for the index
                var basePath = Environment.GetFolderPath(
                    Environment.SpecialFolder.CommonApplicationData);
                var indexPath = Path.Combine(basePath, "index");

                var dir = FSDirectory.Open(indexPath);

                // Create an analyzer to process the text
                var analyzer = new StandardAnalyzer(AppLuceneVersion);

                // Create an index writer
                var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
                var writer = new IndexWriter(dir, indexConfig);

                var source = new
                {
                    Name = "Kermit the Frog",
                    FavoritePhrase = "The quick brown fox jumps over the lazy dog"
                };
                var doc = new Document
{
    // StringField indexes but doesn't tokenize
    new StringField("name",
        source.Name,
        Field.Store.YES),
    new TextField("favoritePhrase",
        source.FavoritePhrase,
        Field.Store.YES)
};

                writer.AddDocument(doc);
                writer.Flush(triggerMerge: false, applyAllDeletes: false);


                writer.Dispose();
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {


            // Ensures index backward compatibility
            const LuceneVersion AppLuceneVersion = LuceneVersion.LUCENE_48;

            // Construct a machine-independent path for the index
            var basePath = Environment.GetFolderPath(
                Environment.SpecialFolder.CommonApplicationData);
            var indexPath = Path.Combine(basePath, "index");

            var dir = FSDirectory.Open(indexPath);

            // Create an analyzer to process the text
            var analyzer = new StandardAnalyzer(AppLuceneVersion);

            // Create an index writer
            var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
            var writer = new IndexWriter(dir, indexConfig);


            // Search with a phrase
            var phrase = new MultiPhraseQuery
{
    new Term("favoritePhrase", "brown"),
    new Term("favoritePhrase", "fox")
};
            // Re-use the writer to get real-time updates
            var reader = writer.GetReader(applyAllDeletes: true);
            var searcher = new IndexSearcher(reader);
            var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;

            DataTable dataTable=new  DataTable();
            dataTable.Columns.Add("name");
            dataTable.Columns.Add("favoritePhrase");
            // Display the output in a table
            Console.WriteLine($"{"Score",10}" +
                $" {"Name",-15}" +
                $" {"Favorite Phrase",-40}");
            foreach (var hit in hits)
            {
                var foundDoc = searcher.Doc(hit.Doc);
            DataRow dr=    dataTable.NewRow();
                dr["name"] = foundDoc.Get("name");
                dr["favoritePhrase"] = foundDoc.Get("favoritePhrase");
               dataTable.Rows.Add(dr);
                Console.WriteLine($"{hit.Score:f8}" +
                    $" {foundDoc.Get("name"),-15}" +
                    $" {foundDoc.Get("favoritePhrase"),-40}");
            }
            this.dataGridView1.DataSource=dataTable;
            writer.Dispose();
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值