(一)简单搜索

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);
            }  
            
        }
View Code

 

 

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) { ; } } } }
View Code

     

 

 

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();
        }
View Code

 

 

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 下一步

文档摘要

中文支持

转载于:https://www.cnblogs.com/minghui0804/p/4162412.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值