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

面试题 相关文章推荐
如何写出高性能的JSP和Servlet
Jan 22 面试题
如何实现一个自定义类的序列化
May 22 面试题
Linux如何命名文件--使用文件名时应注意
May 29 面试题
什么是虚拟内存?虚拟内存有什么优势?
Feb 19 面试题
在Ajax应用中信息是如何在浏览器和服务器之间传递的
May 31 面试题
若通过ObjectOutputStream向一个文件中多次以追加方式写入object,为什么用ObjectInputStream读取这些object时会产生StreamCorruptedException?
Oct 17 面试题
MVC的各个部分都有那些技术来实现?如何实现?
Apr 21 面试题
Java中各种基本数据类型的默认值都是什么
Dec 22 面试题
Structs界面控制层技术
Oct 11 面试题
Servlet方面面试题
Sep 28 面试题
Servlet如何得到客户端机器的信息
Oct 17 面试题
如何防止同一个帐户被多人同时登录
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搭建大文件切割分块上传功能示例
2017/01/04 PHP
PHP基于phpqrcode类生成二维码的方法详解
2018/03/14 PHP
thinkPHP5框架分页样式类完整示例
2018/09/01 PHP
Alliance vs AM BO3 第二场2.13
2021/03/10 DOTA
JS,Jquery获取select,dropdownlist,checkbox下拉列表框的值(示例代码)
2014/01/11 Javascript
js类型转换与引用类型详解(Boolean_Number_String)
2014/03/07 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
node.js调用C++开发的模块实例
2015/07/03 Javascript
纯javascript移动优先的幻灯片效果
2015/11/02 Javascript
Angularjs---项目搭建图文教程
2016/07/08 Javascript
jquery实现焦点轮播效果
2017/02/23 Javascript
javascript+jQuery实现360开机时间显示效果
2017/11/03 jQuery
jQuery实现验证表单密码一致性及正则表达式验证邮箱、手机号的方法
2017/12/05 jQuery
垃圾回收器的相关知识点总结
2018/05/13 Javascript
如何使node也支持从url加载一个module详解
2018/06/05 Javascript
解决vue2 在mounted函数无法获取prop中的变量问题
2018/11/15 Javascript
vue实现的组件兄弟间通信功能示例
2018/12/04 Javascript
Nodejs封装类似express框架的路由实例详解
2020/01/05 NodeJs
[03:16]DOTA2完美大师赛小组赛精彩集锦
2017/11/22 DOTA
分享一下Python数据分析常用的8款工具
2018/04/29 Python
基于python实现百度翻译功能
2019/05/09 Python
Flask中endpoint的理解(小结)
2019/12/11 Python
django框架中ajax的使用及避开CSRF 验证的方式详解
2019/12/11 Python
如何在django中实现分页功能
2020/04/22 Python
浅谈Python3多线程之间的执行顺序问题
2020/05/02 Python
浅谈keras.callbacks设置模型保存策略
2020/06/18 Python
next在python中返回迭代器的实例方法
2020/12/15 Python
css3教程之倾斜页面
2014/01/27 HTML / CSS
高中军训第一天感言
2014/03/06 职场文书
2015年民主评议党员工作总结
2015/05/19 职场文书
收入证明怎么写
2015/06/12 职场文书
《祁黄羊》教学反思
2016/02/20 职场文书
MySQL创建索引需要了解的
2021/04/08 MySQL
详解MySQL 联合查询优化机制
2021/05/10 MySQL
.Net Core导入千万级数据至Mysql的步骤
2021/05/24 MySQL
python Tkinter模块使用方法详解
2022/04/07 Python