(一)简单搜索

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-07-30 04:32:32

(一)简单搜索的相关文章

专题一、简单搜索 - Virtual Judge

很久以前刷完了Virtual Judge上的简单搜索专题,现总结如下: POJ 1321 由于题目的数据范围比较小,可以直接dfs暴力.读入时记录每个空位的位置,保存在pX[]以及pY[]数组中.暴力的时候统计当前处理第几个空格以及当前处理到了第几行即可. #include <iostream> #include <memory.h> using namespace std; const int MAX = 128; long long ans; int N, K, nCnt; b

java 模拟简单搜索

Java 模拟简单搜索 实体类 package org.dennisit.entity; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:[email protected]">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private I

lucene4.3简单搜索示例代码

原文:lucene4.3简单搜索示例代码 源代码下载地址:http://www.zuidaima.com/share/1550463715560448.htm   示例代码,请牛哥们多

参与过三次搜索引擎发展转折的百度,为什么要回归“简单搜索”

相信在搜索引擎技术产品化的二十余年间,我们早已经习惯了搜索引擎的商业化.不过在最近的数博会上,李彦宏却展示了一款名为"简单搜索"的语音交互搜索App,并且声明简单搜索永无广告. 相信很多人也曾想象过这样一款产品--通过语音发问,得出最简单的答案.不过可能大多数人没有想到,推出这款产品的是百度.很长一段时间内,百度的搜索引擎商业化一直为人诟病.但这次百度率先推出永无广告的语音交互搜索,无疑是在搜索引擎的技术和产品形式上都进行了重要创新. 当我们在质疑大企业的创新能力时常常会忽视一点,在技

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

poj 3279 Fliptile (简单搜索)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in whic

和我一起打造个简单搜索之Logstash实时同步建立索引

用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 Java API 的方式建立索引,也可以通过 Logstash 的插件 logstash-input-jdbc 完成,今天来探讨下如何使用 logstash-input-jdbc 完成全量同步以及增量同步. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch

和我一起打造个简单搜索之ElasticSearch集群搭建

我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对于一个网站几乎是标配,只是搜索的强大与否的区别,有的网站只支持关键词模糊搜索,而淘宝,京东提供了精细的筛选条件,同时支持拼音搜索等更方便的搜索方式. 由于笔者在一家做网络文学的公司工作,所以实现就是以小说为商品的搜索,具体可以参考起点网小说的搜索. 如图所示,起点网的搜索提供了关键词搜索和排序条件以

和我一起打造个简单搜索之SpringDataElasticSearch入门

网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 SpringDataElasticSearch 的 api 使用,更加方便的进行查询. 系列文章 一.和我一起打造个简单搜索之ElasticSearch集群搭建 二.和我一起打造个简单搜索之ElasticSearch入门 三.和我一起打造个简单搜索之IK分词以及拼音分词 四.和我一起打造个简单搜索之Log

和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮

前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文章 一.和我一起打造个简单搜索之ElasticSearch集群搭建 二.和我一起打造个简单搜索之ElasticSearch入门 三.和我一起打造个简单搜索之IK分词以及拼音分词 四.和我一起打造个简单搜索之Logstash实时同步建立索引 五.和我一起打造个简单搜索之SpringDataElasti