1界面
1.1抓图:
原则: 简化,直观,说明问题,有用
1.2说明:
1 对指定文件夹创建索引
2 输入关键词,显示查询结果
2 实现
2.1 创建
button1_Click方法:
//对制定文件夹建立索引 private void button1_Click(object sender, EventArgs e1) { System.IO.FileInfo docDir = new System.IO.FileInfo(textBox1.Text); System.DateTime start = System.DateTime.Now; try { //索引生成器IndexWriter, 标准分析器StandardAnalyzer IndexWriter writer = new IndexWriter(FSDirectory.Open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); IndexDocs(writer, docDir); writer.Optimize(); writer.Close(); System.DateTime end = System.DateTime.Now; int TotalMilliSeconds = end.Millisecond - start.Millisecond; MessageBox.Show("索引完成!total " + TotalMilliSeconds + " milliseconds"); } catch (System.IO.IOException e) { MessageBox.Show(" caught a " + e.GetType() + "\n with message: " + e.Message); } }
IndexDocs方法:
//遍历目录,对每个文档建立索引 internal static void IndexDocs(IndexWriter writer, System.IO.FileInfo file) { // do not try to index files that cannot be read // if (file.canRead()) // {{Aroush}} what is canRead() in C#? { if (System.IO.Directory.Exists(file.FullName)) //目录递归 { System.String[] files = System.IO.Directory.GetFileSystemEntries(file.FullName); // an IO error could occur if (files != null) { for (int i = 0; i < files.Length; i++) { IndexDocs(writer, new System.IO.FileInfo(files[i])); } } } else //对文件建立索引 { System.Console.Out.WriteLine("adding " + file); try { writer.AddDocument(FileDocument.Document(file)); //#####key } // at least on windows, some temporary files raise this exception with an "access denied" message // checking if the file can be read doesn‘t help catch (System.IO.FileNotFoundException fnfe) { ; } } } }
2.2 查询
button2_Click方法
//查询 private void button2_Click(object sender, EventArgs e) { dataGridView1.Rows.Clear(); System.String KeyWord = textBox2.Text; System.String index = "index"; System.String field = "contents"; System.String normsField = null; //1 初始化reader, searcher IndexReader reader = IndexReader.Open(FSDirectory.Open(new System.IO.FileInfo(index)), true); // only searching, so read-only=true if (normsField != null) //规范标准 reader = new OneNormsReader(reader, normsField); Searcher searcher = new IndexSearcher(reader); //搜索器 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); QueryParser parser = new QueryParser(field, analyzer); //关键字解析器:指定类型field,分词方法analyzer //2 解析关键字 if (KeyWord == null || KeyWord.Length == -1) return; KeyWord = KeyWord.Trim(); if (KeyWord.Length == 0) return; Query query = parser.Parse(KeyWord); System.Console.Out.WriteLine("Searching for: " + query.ToString(field)); //3 获取查询结果hits Hits hits = searcher.Search(query); //Hits for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); string fullName = doc.Get("path"); //####key string name = fullName.Substring(fullName.LastIndexOf("\\") + 1, fullName.Length - 1 - fullName.LastIndexOf("\\")); DataGridViewRow dr = new DataGridViewRow(); int index1 = dataGridView1.Rows.Add(dr); dataGridView1.Rows[index1].Cells[0].Value = name; dataGridView1.Rows[index1].Cells[1].Value = fullName; } searcher.Close(); reader.Close(); }
3总结
3.1方法:
1 逐层推进: 逻辑 -- 层次1 -- 层次2
2 抓住核心: //####key
3 源代码注释分段,预想猜测
3.2知识点:
1 生成索引: indexWriter.AddDocument(FileDocument.Document(file));
2 解析关键字: Query query = parser.Parse(KeyWord);
3 获取查询结果: Hits hits = searcher.Search(query);
3.3心态:
自然流畅,不追求完美。
循环练习,逐渐接近,形成模板。
Vs移除项目不会导致删除
4 下一步
文档摘要
中文支持
时间: 2024-09-30 07:09:32