介绍下Lucene建立索引的过程


Posted in 面试题 onMarch 02, 2016
代码如下:

1. package utils;
2.
3. import java.io.File;
4. import java.io.FileReader;
5. import java.io.IOException;
6. import java.io.Reader;
7.
8. import org.apache.lucene.analysis.standard.StandardAnalyzer;
9. import org.apache.lucene.document.Document;
10. import org.apache.lucene.document.Field;
11. import org.apache.lucene.index.IndexWriter;
12.
13. public class Indexer {
14.
15. public int index(String indexDir, String dataDir) throws IOException
16. {
17. File indexDirFile = new File(indexDir);
18. File dataDirFile = new File(dataDir);
19. int numIndexed = index(indexDirFile, dataDirFile);
20. return 0;
21. }
22.
23. private int index(File indexDirFile, File dataDirFile) throws IOException {
24. if(!dataDirFile.exists() || !dataDirFile.isDirectory())
25. {
26. throw new IOException(dataDirFile + ” does not exist or is not a directory”);
27. }
28. IndexWriter writer = new IndexWriter(indexDirFile, new StandardAnalyzer(), true);
29. writer.setUseCompoundFile(false);
30. indexDirectory(writer, dataDirFile);
31.
32. int numIndexed = writer.docCount();
33. writer.optimize();
34. writer.close();
35. return numIndexed;
36. }
37.
38. private void indexDirectory(IndexWriter writer, File dataDirFile) throws IOException {
39. File[] files = dataDirFile.listFiles();
40. for(int i = 0; i
41. {
42. File f = files[i];
43. if(f.isDirectory())
44. {
45. indexDirectory(writer, f);
46. }else if(f.getName().endsWith(”.java”) || f.getName().endsWith(”.txt”))//需要索引的文件类型
47. {
48. indexFile(writer, f);
49. }
50.
51. }
52.
53. }
54.
55. private void indexFile(IndexWriter writer, File f) throws IOException {
56. if(f.isHidden() || !f.exists() || !f.canRead())
57. {
58. return;
59. }
60. System.out.println(”Indexing” + f.getCanonicalPath());
61. Document doc = new Document();
62. Reader txtReader = new FileReader(f);
63. doc.add(new Field(”path”,f.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED));
64. doc.add(new Field(”contents”,txtReader));
65. doc.add(new Field(”name”,f.getName(),Field.Store.YES,Field.Index.UN_TOKENIZED));
66. writer.addDocument(doc);
67. }
68.
69. }
70.
71.
调用的代码如下:
1. String filesRepoDir = “C:/workspace-2.0″;//需要被索引的目录
2. String indexDir = “C:/apache-tomcat-6.0.18/webapps/index”;//存放索引的目录
3. Indexer indexer= new Indexer();
4. indexer.index(indexDir, filesRepoDir);

Tags in this post...

面试题 相关文章推荐
华为的Java面试题
Mar 07 面试题
C语言如何决定使用那种整数类型
Nov 26 面试题
下面关于"联合"的题目的输出是什么
Aug 06 面试题
一套SQL笔试题
Aug 14 面试题
南京某公司笔试题
Jan 27 面试题
说出你对remoting 和webservice的理解和应用
Jun 08 面试题
Linux面试经常问的文件系统操作命令
Nov 05 面试题
介绍一下XMLHttpRequest对象
Feb 12 面试题
Prototype是怎么扩展DOM的
Oct 01 面试题
介绍JAVA 中的Collection FrameWork(及如何写自己的数据结构)
Oct 31 面试题
我们没有写servlet的构造方法,那么容器是怎么创建servlet的实例呢
Apr 24 面试题
什么是servlet链?
Jul 13 面试题
如何防止同一个帐户被多人同时登录
Aug 01 #面试题
swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?
Mar 30 #面试题
Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
Jan 27 #面试题
Set里的元素是不能重复的,那么用什么方法来区分重复与否呢?
Aug 18 #面试题
GC是什么?为什么要有GC?
Dec 08 #面试题
Overload和Override的区别
Sep 02 #面试题
什么时候用assert
May 08 #面试题
You might like
php is_file 判断给定文件名是否为一个正常的文件
2010/05/10 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
2016/03/21 PHP
PHP在弹框中获取foreach中遍历的id值并传递给地址栏
2017/06/13 PHP
PHP PDOStatement::execute讲解
2019/01/31 PHP
javascript面向对象的方式实现的弹出层效果代码
2010/01/28 Javascript
jquery中子元素和后代元素的区别示例介绍
2014/04/02 Javascript
javascript Array 数组常用方法
2015/04/05 Javascript
JS实战篇之收缩菜单表单布局
2016/12/10 Javascript
浅述节点的创建及常见功能的实现
2016/12/15 Javascript
jQuery实现可移动选项的左右下拉列表示例
2016/12/26 Javascript
Google 爬虫如何抓取 JavaScript 的内容
2017/04/07 Javascript
基于Vue实现页面切换左右滑动效果
2020/06/29 Javascript
Javascript中toFixed计算错误(依赖银行家舍入法的缺陷)解决方法
2017/08/22 Javascript
vue自动化表单实例分析
2018/05/06 Javascript
解决element-ui中下拉菜单子选项click事件不触发的问题
2018/08/22 Javascript
解决百度Echarts图表坐标轴越界的方法
2018/10/17 Javascript
详解如何使用nvm管理Node.js多版本
2019/05/06 Javascript
使用Vue 实现滑动验证码功能
2019/06/27 Javascript
Javascript Dom元素获取和添加详解
2019/09/24 Javascript
ES6使用新特性Proxy实现的数据绑定功能实例
2020/05/11 Javascript
jQuery实现简单弹幕制作
2020/12/10 jQuery
讲解Python的Scrapy爬虫框架使用代理进行采集的方法
2016/02/18 Python
深入理解python函数递归和生成器
2016/06/06 Python
python数据预处理之数据标准化的几种处理方式
2019/07/17 Python
seek引发的python文件读写的问题及解决
2019/07/26 Python
Python中PyQt5/PySide2的按钮控件使用实例
2019/08/17 Python
python实现的分析并统计nginx日志数据功能示例
2019/12/21 Python
全球领先的各类汽车配件零售商:Advance Auto Parts
2016/08/26 全球购物
Boden澳大利亚官网:英国在线服装公司
2018/08/05 全球购物
应用心理学个人求职信范文
2013/12/11 职场文书
外语系大学生自荐信范文
2014/03/01 职场文书
公司寄语大全
2014/04/10 职场文书
三好学生先进事迹材料
2014/08/28 职场文书
小学母亲节活动总结
2015/02/10 职场文书
2015年检验科工作总结
2015/04/27 职场文书
高二数学教学反思
2016/02/18 职场文书