介绍下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如何自定义函数
Sep 16 面试题
PHP面试题及答案二
May 23 面试题
SQL中where和having的区别
Jun 17 面试题
简述你对Statement,PreparedStatement,CallableStatement的理解
Mar 25 面试题
C语言中一个结构不能包含指向自己的指针吗
May 25 面试题
寻找迷宫的一条出路,o通路;X:障碍
Jul 10 面试题
SQL Server笔试题
Jan 10 面试题
分布式数据库需要考虑哪些问题
Dec 08 面试题
采用怎样的方法保证数据的完整性
Dec 02 面试题
你对IPv6了解程度
Feb 09 面试题
什么是三层交换,说说和路由的区别在那里
Sep 01 面试题
什么是封装
Mar 26 面试题
如何防止同一个帐户被多人同时登录
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
PHP4实际应用经验篇(7)
2006/10/09 PHP
php模拟post行为代码总结(POST方式不是绝对安全)
2012/02/22 PHP
简单实用的.net DataTable导出Execl
2013/10/28 PHP
PHP goto语句简介和使用实例
2014/03/11 PHP
PHP图像处理之使用imagecolorallocate()函数设置颜色例子
2014/11/19 PHP
PHP使用DOM和simplexml读取xml文档的方法示例
2017/02/08 PHP
php使用crypt()函数进行加密
2017/06/08 PHP
Laravel框架模板加载,分配变量及简单路由功能示例
2018/06/11 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
2019/08/06 PHP
jquery实现checkbox 全选/全不选的通用写法
2014/02/22 Javascript
javascript中的nextSibling使用陷(da)阱(keng)
2014/05/05 Javascript
让JavaScript中setTimeout支持链式操作的方法
2015/06/19 Javascript
js简单工厂模式用法实例
2015/06/30 Javascript
Node.js的基本知识简单汇总
2016/09/19 Javascript
jQuery使用EasyUi实现三级联动下拉框效果
2017/03/08 Javascript
jQuery表单设置值的方法
2017/06/30 jQuery
原生js中ajax访问的实例详解
2017/09/19 Javascript
js实现简单分页导航栏效果
2019/06/28 Javascript
vscode中的vue项目报错Property ‘xxx‘ does not exist on type ‘CombinedVueInstance<{ readyOnly...Vetur(2339)
2020/09/11 Javascript
python编写暴力破解FTP密码小工具
2014/11/19 Python
Python基本语法经典教程
2016/03/11 Python
Python编程之微信推送模板消息功能示例
2017/08/21 Python
python暴力解压rar加密文件过程详解
2019/07/05 Python
对python中不同模块(函数、类、变量)的调用详解
2019/07/16 Python
django框架F&Q 聚合与分组操作示例
2019/12/12 Python
python怎么调用自己的函数
2020/07/01 Python
详解CSS3 rem(设置字体大小) 教程
2017/11/21 HTML / CSS
HTML5 body设置全屏背景图片的示例代码
2020/12/08 HTML / CSS
违反学校规定检讨书
2014/01/18 职场文书
遗嘱公证书标准样本
2014/04/08 职场文书
主题班会演讲稿
2014/05/22 职场文书
食品质量与安全专业毕业生求职信
2014/08/11 职场文书
三八妇女节标语
2014/10/09 职场文书
干货:企业内部人才推荐奖励方案!
2019/07/09 职场文书
使用Python的开发框架Brownie部署以太坊智能合约
2021/05/28 Python
基于python定位棋子位置及识别棋子颜色
2021/07/26 Python