介绍下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...

面试题 相关文章推荐
几个人围成一圈的问题
Sep 26 面试题
Java面试题:请问一下代码输出是什么
May 27 面试题
"火柴棍式"程序员面试题
Mar 16 面试题
.NET里面什么时候需要调用垃圾回收
Jun 01 面试题
System.Array.CopyTo()和System.Array.Clone()有什么区别
Jun 20 面试题
新大陆软件面试题
Nov 24 面试题
什么是动态端口(Dynamic Ports)?动态端口的范围是多少?
Dec 12 面试题
C#如何判断当前用户是否输入某个域
Dec 07 面试题
C# Debug和Testing相关面试题
Oct 25 面试题
Linux机考试题
Jul 17 面试题
怎样在 Applet 中建立自己的菜单(MenuBar/Menu)?
Jun 20 面试题
J2EE包括哪些技术
Nov 25 面试题
如何防止同一个帐户被多人同时登录
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-redis中的sort排序函数总结
2015/07/08 PHP
php需登录的文件上传管理系统
2020/03/21 PHP
PHP 实现手机端APP支付宝支付功能
2018/06/07 PHP
thinkphp5.1框架容器与依赖注入实例分析
2019/07/23 PHP
javascript[js]获取url参数的代码
2007/10/17 Javascript
IE8 浏览器Cookie的处理
2009/01/31 Javascript
javascript json2 使用方法
2010/03/16 Javascript
js 判断checkbox是否选中的操作方法
2012/11/09 Javascript
jquery利用ajax调用后台方法实例
2013/08/23 Javascript
Javascript的&&和||的另类用法
2014/07/23 Javascript
Javascript基础知识(一)核心基础语法与事件模型
2014/09/29 Javascript
jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法
2015/12/12 Javascript
JavaScript通过HTML的class来获取HTML元素的方法总结
2016/05/24 Javascript
Javascript类型系统之undefined和null浅析
2016/07/13 Javascript
基于JavaScript实现随机颜色输入框
2016/12/10 Javascript
JavaScript实现倒计时跳转页面功能【实用】
2016/12/13 Javascript
关于AngularJs数据的本地存储详解
2017/01/20 Javascript
使用get方式提交表单在地址栏里面不显示提交信息
2017/02/21 Javascript
angularjs实现首页轮播图效果
2017/04/14 Javascript
基于Vue+element-ui 的Table二次封装的实现
2018/07/20 Javascript
解决微信小程序中的滚动穿透问题
2019/09/16 Javascript
跟老齐学Python之再深点,更懂list
2014/09/20 Python
Python函数中定义参数的四种方式
2014/11/30 Python
python实现机器学习之多元线性回归
2018/09/06 Python
Python找出列表中出现次数最多的元素三种方式
2020/02/24 Python
html5 viewport使用方法示例详解
2013/12/02 HTML / CSS
C/C++有关内存的思考题
2015/12/04 面试题
广告学专业推荐信范文
2013/11/23 职场文书
班干部竞选演讲稿
2014/04/24 职场文书
教师四风对照检查材料思想汇报
2014/09/17 职场文书
优秀班主任申报材料
2014/12/16 职场文书
会计岗位职责范本
2015/04/02 职场文书
楚门的世界观后感
2015/06/03 职场文书
婚宴致辞
2015/07/28 职场文书
浅谈Python数学建模之固定费用问题
2021/06/23 Python
win11无法添加打印机怎么办? 提示windows无法打开添加打印机的解决办法
2022/04/05 数码科技