Lucene

  • 搜索引擎

  • 搜索引擎的分类
  1. 垂直搜索:专门对某一类信息进行搜索
  2. 综合搜索:对众多信息进行综合性搜索
  3. 站内搜索:对网站内的信息进行搜索
  4. 软件内部搜索

倒排索引技术(反向索引):以字或者词为关键字进行索引,表中关键字所对应的记录表项,记录了出现这个字或者词的所有文档,每一个表项记录该文档的ID和关键字在该文档中出现的位置情况。

  • 主要jar包的导入

  • 代码实现
 1 @Test
 2     public void indexCreate() throws Exception {
 3         // 创建文档对象
 4         Document document = new Document();
 5         /*
 6          * StringField需要三个参数: 1.字段名 2.字段值 3.是否保存到文档
 7          */
 8         document.add(new StringField("id", "1", Store.YES));
 9         // 创建索引提供分词
10         document.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));
11         // 创建目录对象
12         Directory directory = FSDirectory.open(new File("D:/temp/indexDir"));
13         // 创建分词器对象
14         Analyzer analyzer = new StandardAnalyzer();
15         // 创建索引写入器配置对象
16         IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);
17         // 创建分词索引器
18         IndexWriter indexWriter = new IndexWriter(directory, conf);
19         // 向索引库中写入文档对象
20         indexWriter.addDocument(document);
21         // 提交
22         indexWriter.commit();
23         // 关闭
24         indexWriter.close();
25     }
  • 思路
  • 创建文档对象,创建索引,提供分词
1      Document document = new Document();
2         /*
3          * StringField需要三个参数: 1.字段名 2.字段值 3.是否保存到文档
4          */
5         document.add(new StringField("id", "1", Store.YES));
6         // 创建索引提供分词
7         document.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));
  • 创建分词索引器

 1 IndexWriter indexWriter = new IndexWriter(directory, conf); 

  • 创建目录对象directory

 1 Directory directory = FSDirectory.open(new File("D:/temp/indexDir")); 

  • 创建索引写入器配置对象

 1 IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer); 

  • 创建分词器对象

 1 Analyzer analyzer = new StandardAnalyzer(); 

  • 向索引库中写入文档对象

 1 indexWriter.addDocument(document); 

1      // 提交
2         indexWriter.commit();
3         // 关闭
4         indexWriter.close();

通过索引查看:

 

 

 使用IK分词器改造程序:

 

posted @ 2017-08-16 23:09  NocareNoworry  阅读(130)  评论(0)    收藏  举报