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

面试题 相关文章推荐
Eclipse面试题
Mar 22 面试题
如何提高JDBC的性能
Apr 30 面试题
精伦电子Java笔试题
Jan 16 面试题
外企C语言笔试题
Nov 10 面试题
如何提高SQL Server的安全性
Jul 25 面试题
西安众合通用.net笔试题
Mar 18 面试题
什么是Smart Navigation?
Jul 03 面试题
非常详细的C#面试题集
Jul 13 面试题
别名指示符是什么
Oct 08 面试题
Python如何定义一个函数
Sep 01 面试题
JVM是一个编译程序还是解释程序
Sep 11 面试题
如何安装ruby on rails
Feb 09 面试题
如何防止同一个帐户被多人同时登录
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
YB217、YB235、YB400浅听
2021/03/02 无线电
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)
2012/07/31 PHP
PHP清除数组中所有字符串两端空格的方法
2014/10/20 PHP
PHP中is_dir()函数使用指南
2015/05/08 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
2015/11/05 PHP
php成功操作redis cluster集群的实例教程
2019/01/13 PHP
深入浅析安装PhpStorm并激活的步骤详解
2020/09/17 PHP
js下获取div中的数据的原理分析
2010/04/07 Javascript
当自定义数据属性为json格式字符串时jQuery的data api问题探讨
2013/02/18 Javascript
jquery在Chrome下获取图片的长宽问题解决
2013/03/20 Javascript
jquery的相对父元素和相对文档定位示例代码
2013/08/02 Javascript
JS实现的表格行鼠标点击高亮效果代码
2015/11/27 Javascript
BootStrap 轮播插件(carousel)支持左右手势滑动的方法(三种)
2016/07/07 Javascript
JavaScript正则获取地址栏中参数的方法
2017/03/02 Javascript
JavaScript实现打地鼠小游戏
2020/04/23 Javascript
微信小程序 新建登录页并实现tabBar隐藏
2017/06/13 Javascript
JavaScript中使用import 和require打包后实现原理分析
2018/03/07 Javascript
微信小程序中时间戳和日期的相互转换问题
2018/07/09 Javascript
浅谈javascript事件环微任务和宏任务队列原理
2020/09/12 Javascript
JS数组索引检测中的数据类型问题详解
2021/01/11 Javascript
[16:56]教你分分钟做大人:司夜刺客
2014/10/30 DOTA
Python网页解析利器BeautifulSoup安装使用介绍
2015/03/17 Python
Python输出各行命令详解
2018/02/01 Python
pycharm中显示CSS提示的知识点总结
2019/07/29 Python
Python爬虫之Selenium多窗口切换的实现
2020/12/04 Python
html5 canvas 画图教程案例分析
2012/11/23 HTML / CSS
Lululemon加拿大官网:加拿大知名体育服装零售商
2019/04/12 全球购物
盖尔斯工厂店:GUESS Factory
2020/01/21 全球购物
承办会议欢迎词
2014/01/17 职场文书
技校毕业生个人学习的自我评价
2014/02/21 职场文书
机械工程及自动化专业求职信
2014/09/03 职场文书
县委常委班子对照检查材料思想汇报
2014/09/28 职场文书
四风问题对照检查材料思想汇报
2014/10/07 职场文书
工作表扬信
2015/01/17 职场文书
如何搭建 MySQL 高可用高性能集群
2021/06/21 MySQL
Python简易开发之制作计算器
2022/04/28 Python