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

面试题 相关文章推荐
运行时异常与一般异常有何异同?
Jan 05 面试题
Java基础知识面试要点
Jul 29 面试题
C语言怎样定义和声明全局变量和函数最好
Nov 26 面试题
一些高难度的SQL面试题
Nov 29 面试题
路由表示做什么用的?在linux环境中怎么来配置一条默认路由?
Jun 07 面试题
网络编程中设计并发服务器,使用多进程与多线程,请问有什么区别?
Mar 27 面试题
能否解释一下XSS cookie盗窃是什么意思
Jun 02 面试题
LINUX下线程,GDI类的解释
Apr 17 面试题
init进程的作用
Apr 12 面试题
简单叙述一下MYSQL的优化
May 09 面试题
写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数
Apr 13 面试题
一套英文Java笔试题面试题
Apr 21 面试题
如何防止同一个帐户被多人同时登录
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生成短网址示例
2014/05/05 PHP
PHP实现通过CURL上传文件功能示例
2018/05/30 PHP
Laravel如何实现适合Api的异常处理响应格式
2020/06/14 PHP
javascript的函数
2007/01/31 Javascript
抽出www.templatemonster.com的鼠标悬停加载大图模板的代码
2007/07/11 Javascript
jQuery 常见学习网站与参考书
2009/11/09 Javascript
onsubmit阻止form表单提交与onclick的相关操作
2010/09/03 Javascript
扩展Jquery插件处理mouseover时内部有子元素时发生样式闪烁
2011/12/08 Javascript
让jQuery与其他JavaScript库并存避免冲突的方法
2013/12/23 Javascript
JavaScript中的函数的两种定义方式和函数变量赋值
2014/05/12 Javascript
jQuery的缓存机制浅析
2014/06/07 Javascript
JavaScript中判断页面关闭、页面刷新的实现代码
2014/08/27 Javascript
javascript中scrollTop详解
2015/04/13 Javascript
AngularJS Module方法详解
2015/12/08 Javascript
Bootstrap每天必学之折叠(Collapse)插件
2016/04/25 Javascript
JavaScript简单实现弹出拖拽窗口(一)
2016/06/17 Javascript
轻松玩转BootstrapTable(后端使用SpringMVC+Hibernate)
2017/09/06 Javascript
Vue.js分页组件实现:diVuePagination的使用详解
2018/01/10 Javascript
在vue-cli项目中使用bootstrap的方法示例
2018/04/21 Javascript
微信小程序搭建自己的Https服务器
2019/05/02 Javascript
通过实例讲解JS如何防抖动
2019/06/15 Javascript
uni-app使用微信小程序云函数的步骤示例
2020/05/22 Javascript
[06:44]2014DOTA2国际邀请赛-钥匙体育馆开战 开幕式振奋人心
2014/07/19 DOTA
[02:28]PWL开团时刻DAY3——Ink Ice与DeMonsTer之间的勾心斗角
2020/11/03 DOTA
Python中subprocess模块用法实例详解
2015/05/20 Python
Python 实现删除某路径下文件及文件夹的实例讲解
2018/04/24 Python
Python的argparse库使用详解
2018/10/09 Python
Python3.6实现带有简单界面的有道翻译小程序
2019/04/16 Python
python录音并调用百度语音识别接口的示例
2020/12/01 Python
高二美术教学反思
2014/01/14 职场文书
专业技术职务聘任书
2014/03/29 职场文书
党员领导干部民主生活会批评与自我批评发言
2014/09/28 职场文书
小兵张嘎观后感
2015/06/03 职场文书
回复函格式及范文
2015/07/14 职场文书
Python 数据可视化之Matplotlib详解
2021/11/02 Python
利用Python实现Picgo图床工具
2021/11/23 Python