lucene.net 2.9.2 实现索引生成,修改,查询,删除功
2010-10-19 14:38 yuejianjun 阅读(728) 评论(0) 编辑 收藏 举报索引生成功能
1 using System;2 using System.Collections.Generic;
3 using System.Web;
4 using System.Web.UI;
5 using System.Web.UI.WebControls;
6
7 using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
8 using IndexWriter = Lucene.Net.Index.IndexWriter;
9 using FSDirectory = Lucene.Net.Store.FSDirectory;
10 using Version = Lucene.Net.Util.Version;
11
12
13 namespace Demo
14 {
15 public partial class _Default : System.Web.UI.Page
16 {
17 //生成索引文件的路径
18 private static readonly System.IO.FileInfo INDEX_DIR = new System.IO.FileInfo("D:\\ProjectsSVN\\ChinaValueApplication\\ChinaValueSearch\\Demo\\index");
19
20 protected void Page_Load(object sender, EventArgs e)
21 {
22
23 }
24
25 protected void CreateIndex_Click(object sender, EventArgs e)
26 {
27 //被索引文件的路径
28 System.IO.FileInfo docDir = new System.IO.FileInfo("E:\\work\\back\\Document\\");
29
30 Boolean tmpBool2;
31
32 if (System.IO.File.Exists(docDir.FullName))
33 {
34 tmpBool2 = true;
35 }
36 else
37 {
38 tmpBool2 = System.IO.Directory.Exists(docDir.FullName);
39 }
40
41 if (!tmpBool2)
42 {
43 ltrInfo.Text = "Document directory '" + docDir.FullName + "' does not exist or is not readable, please check the path";
44 return;
45 }
46
47 System.DateTime start = System.DateTime.Now;
48
49 try
50 {
51 Boolean EnableCreate = true;
52
53 if (System.IO.File.Exists("D:\\ProjectsSVN\\ChinaValueApplication\\ChinaValueSearch\\Demo\\index\\segments"))
54 {
55 EnableCreate = false;
56 }
57
58 IndexWriter writer = new IndexWriter(FSDirectory.Open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), EnableCreate, IndexWriter.MaxFieldLength.LIMITED);
59 ltrInfo.Text += "Indexing to directory '" + INDEX_DIR + "'... <br>";
60 IndexDocs(writer, docDir);
61 ltrInfo.Text += "Optimizing... <br>";
62 writer.Optimize();
63 writer.Close();
64
65 System.DateTime end = System.DateTime.Now;
66 ltrInfo.Text += "生成成功" + (end.Millisecond - start.Millisecond).ToString() + " total milliseconds <br>";
67 CreateIndex.Enabled = false;
68 }
69 catch (System.IO.IOException ex)
70 {
71 ltrInfo.Text += " caught a " + ex.GetType() + "\n with message: " + ex.Message;
72 }
73 }
74
75 private void IndexDocs(IndexWriter writer, System.IO.FileInfo file)
76 {
77 if (System.IO.Directory.Exists(file.FullName))
78 {
79 System.String[] files = System.IO.Directory.GetFileSystemEntries(file.FullName);
80
81 if (files != null)
82 {
83 for (int i = 0; i < files.Length; i++)
84 {
85 IndexDocs(writer, new System.IO.FileInfo(files[i]));
86 }
87 }
88 }
89 else
90 {
91 try
92 {
93 writer.AddDocument(FileDocument.Document(file));
94 }
95 catch (System.IO.FileNotFoundException fnfe)
96 {
97 ltrInfo.Text += "FileNotFoundException: " + fnfe.Message;
98 }
99 }
100 }
101 }
102 }
103
索引修改功能
1 using System;
2 using System.Collections.Generic;
3 using System.Web;
4 using System.Web.UI;
5 using System.Web.UI.WebControls;
6
7 using IndexWriter = Lucene.Net.Index.IndexWriter;
8 using FSDirectory = Lucene.Net.Store.FSDirectory;
9 using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
10 using Version = Lucene.Net.Util.Version;
11 using Term = Lucene.Net.Index.Term;
12
13 namespace Demo
14 {
15 public partial class UpdateIndex : System.Web.UI.Page
16 {
17 public String index = "D:\\ProjectsSVN\\ChinaValueApplication\\ChinaValueSearch\\Demo\\index";
18
19 protected void Page_Load(object sender, EventArgs e)
20 {
21
22 }
23
24 protected void btnUpdate_Click(object sender, EventArgs e)
25 {
26 Boolean EnableCreate = true;
27
28 if (System.IO.File.Exists(index + "\\segments.gen"))
29 {
30 EnableCreate= false;
31 }
32
33 IndexWriter writer = new IndexWriter(FSDirectory.Open(new System.IO.FileInfo(index)), new StandardAnalyzer(Version.LUCENE_CURRENT), EnableCreate);
34
35 // System.IO.FileInfo file = new System.IO.FileInfo(txtID.Text.Trim().Replace("\\", "\\\\"));
36
37 System.IO.FileInfo file = new System.IO.FileInfo(@"E:\work\back\Document\ksp.txt");
38 writer.UpdateDocument(new Term("path", txtID.Text.Trim()), FileDocument.Document(file));
39
40 writer.Close();
41 }
42 }
43 }
44
索引查询
1 using System;2
3 using FSDirectory = Lucene.Net.Store.FSDirectory;
4 using IndexReader = Lucene.Net.Index.IndexReader;
5 using Searcher = Lucene.Net.Search.Searcher;
6 using IndexSearcher = Lucene.Net.Search.IndexSearcher;
7 using Analyzer = Lucene.Net.Analysis.Analyzer;
8 using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
9 using Version = Lucene.Net.Util.Version;
10 using QueryParser = Lucene.Net.QueryParsers.QueryParser;
11 using Query = Lucene.Net.Search.Query;
12 using TopScoreDocCollector = Lucene.Net.Search.TopScoreDocCollector;
13 using ScoreDoc = Lucene.Net.Search.ScoreDoc;
14 using Document = Lucene.Net.Documents.Document;
15
16 namespace Demo
17 {
18 public partial class SearchIndex : System.Web.UI.Page
19 {
20 private static String index = "D:\\ProjectsSVN\\ChinaValueApplication\\ChinaValueSearch\\Demo\\index";
21
22 protected void Page_Load(object sender, EventArgs e)
23 {
24
25 }
26 protected void btnSearch_Click(object sender, EventArgs e)
27 {
28 String field = "contents";
29
30 IndexReader reader = IndexReader.Open(FSDirectory.Open(new System.IO.FileInfo(index)), true);
31
32 Searcher searcher = new IndexSearcher(reader);
33 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
34
35 QueryParser parser = new QueryParser(field, analyzer);
36
37 Query query = parser.Parse(txtKeyWord.Text.Trim());
38
39 TopScoreDocCollector collector = TopScoreDocCollector.create(searcher.MaxDoc(), false);
40 searcher.Search(query, collector);
41 ScoreDoc[] hits = collector.TopDocs().scoreDocs;
42
43 ltrResult.Text = "共 " + collector.GetTotalHits().ToString()+" 条记录<br>";
44
45 for (Int32 i = 0; i < collector.GetTotalHits(); i++)
46 {
47 ltrResult.Text += "doc=" + hits[i].doc + " score=" + hits[i].score + "<br>";
48 Document doc = searcher.Doc(hits[i].doc);
49 ltrResult.Text += "Path:" + doc.Get("path") + "<br>";
50 }
51
52 reader.Close();
53 }
54 }
55 }
索引删除
1 using System;2
3 using FSDirectory = Lucene.Net.Store.FSDirectory;
4 using IndexReader = Lucene.Net.Index.IndexReader;
5 using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
6 using Version = Lucene.Net.Util.Version;
7 using Directory = Lucene.Net.Store.Directory;
8 using Term = Lucene.Net.Index.Term;
9 using IndexWriter = Lucene.Net.Index.IndexWriter;
10
11 namespace Demo
12 {
13 public partial class DelIndex : System.Web.UI.Page
14 {
15 private static String index = "D:\\ProjectsSVN\\ChinaValueApplication\\ChinaValueSearch\\Demo\\index";
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 }
20
21 protected void btnDel_Click(object sender, EventArgs e)
22 {
23 try
24 {
25 Directory directory = FSDirectory.Open(new System.IO.FileInfo(index));
26 IndexReader reader = IndexReader.Open(directory, false);
27
28 //// 删除所有记录
29 //for (int i = 0; i < reader.MaxDoc(); i++)
30 //{
31 // reader.DeleteDocument(i);
32 //}
33
34 if (rdlDel.SelectedValue == "1") //根据ID删除记录
35 {
36 Int32 i = Convert.ToInt32(txtID.Text);
37 reader.DeleteDocument(i);
38 ltrResult.Text = "删除成功";
39 }
40 else if (rdlDel.SelectedValue == "2") // 根据条件删除记录
41 {
42 Term term = new Term("path", txtID.Text.Trim());
43 Int32 num = reader.DeleteDocuments(term);
44 ltrResult.Text = "删除" + num.ToString() + "条记录成功";
45 }
46
47 reader.Close();
48 directory.Close();
49
50 }
51 catch (System.Exception ex)
52 {
53 ltrResult.Text = " caught a " + e.GetType() + "\n with message: " + ex.Message;
54 }
55 }
56
57 protected void btnOptimize_Click(object sender, EventArgs e)
58 {
59 IndexWriter writer = new IndexWriter(FSDirectory.Open(new System.IO.FileInfo(index)), new StandardAnalyzer(Version.LUCENE_CURRENT), false);
60 writer.Commit();
61 writer.Optimize();
62 writer.Close();
63 }
64 }
65 }
http://www.newwhy.com/2010/0906/12585.html