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

面试题 相关文章推荐
普通PHP程序员笔试题
Jan 01 面试题
abstract class和interface有什么区别?
Jan 03 面试题
面向对象编程是如何提高软件开发水平的
May 06 面试题
一个C/C++编程面试题
Nov 10 面试题
函数指针的定义是什么
Aug 14 面试题
SQL面试题
Apr 30 面试题
星空联盟C# .net笔试题
Dec 05 面试题
extern是什么意思
Mar 10 面试题
C#里面可以避免一个类被其他类继承么?如何?
Sep 26 面试题
介绍一下gcc特性
Oct 31 面试题
Python如何定义一个函数
Sep 01 面试题
final, finally, finalize的区别
Mar 01 面试题
如何防止同一个帐户被多人同时登录
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
DOTA2 无惧惊涛骇浪 昆卡大型水友攻略
2020/04/20 DOTA
visual studio code 调试php方法(图文详解)
2017/09/15 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
静态的动态续篇之来点XML
2006/12/23 Javascript
JavaScript 变量基础知识
2009/11/07 Javascript
jQuery之选项卡的简单实现
2014/02/28 Javascript
Node.js中AES加密和其它语言不一致问题解决办法
2014/03/10 Javascript
HTML页面弹出居中可拖拽的自定义窗口层
2014/05/07 Javascript
js css 实现遮罩层覆盖其他页面元素附图
2014/09/22 Javascript
JavaScript字符串对象substr方法入门实例(用于截取字符串)
2014/10/16 Javascript
javascript实现按回车键切换焦点
2015/02/09 Javascript
JQ技术实现注册页面带有校验密码强度
2015/07/27 Javascript
省市二级联动小案例讲解
2016/07/24 Javascript
JS触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
2016/10/30 Javascript
Node.js中路径处理模块path详解
2016/11/14 Javascript
vue+SSM实现验证码功能
2018/12/07 Javascript
详解vue项目中使用token的身份验证的简单实践
2019/03/08 Javascript
微信小程序学习笔记之获取位置信息操作图文详解
2019/03/29 Javascript
微信小程序动态显示项目倒计时
2019/06/20 Javascript
详解python3中tkinter知识点
2018/06/21 Python
python用fsolve、leastsq对非线性方程组求解
2018/12/15 Python
详解在python操作数据库中游标的使用方法
2019/11/12 Python
python 穷举指定长度的密码例子
2020/04/02 Python
python中plt.imshow与cv2.imshow显示颜色问题
2020/07/16 Python
STUBHUB日本:购买和出售全球活动门票
2018/07/01 全球购物
银行职员思想汇报
2013/12/31 职场文书
回门宴新郎答谢词
2014/01/12 职场文书
教师绩效考核方案
2014/01/21 职场文书
社区安全生产月活动总结
2014/07/05 职场文书
群众路线专项整治工作情况报告
2014/10/28 职场文书
仓库管理员岗位职责
2015/02/03 职场文书
一波干货,会议主持词开场白范文
2019/05/06 职场文书
Python数据分析入门之教你怎么搭建环境
2021/05/13 Python
WINDOWS 64位 下安装配置mysql8.0.25最详细的教程
2022/03/22 MySQL
python标准库ElementTree处理xml
2022/05/20 Python
CSS使用Flex和Grid布局实现3D骰子
2022/08/05 HTML / CSS