介绍下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 面试题
Java的五个基础面试题
Feb 26 面试题
我有一个char * 型指针正巧指向一些int 型变量, 我想跳过它们。 为什么如下的代码((int *)p)++; 不行?
May 09 面试题
一个C/C++编程面试题
Nov 10 面试题
写一个函数返回1+2+3+…+n的值(假定结果不会超过长整型变量的范围)
Sep 05 面试题
struct和class的区别
Nov 20 面试题
几个Linux面试题笔试题
Aug 01 面试题
软件测试常见笔试题
Feb 04 面试题
Python面试题:Python是如何进行内存管理的
Aug 04 面试题
mysql有关权限的表都有哪几个
Apr 22 面试题
日期和时间问题
Jan 04 面试题
ruby如何进行集成操作?Ruby能进行多重继承吗?
Oct 16 面试题
如何防止同一个帐户被多人同时登录
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学习之PHP表达式
2006/10/09 PHP
PHP $_SERVER详解
2009/01/16 PHP
php 网上商城促销设计实例代码
2012/02/17 PHP
thinkphp中memcache的用法实例
2014/11/29 PHP
php将文件夹打包成zip文件的简单实现方法
2016/10/04 PHP
thinkphp实现附件上传功能
2017/05/26 PHP
PHP中phar包的使用教程
2017/06/14 PHP
php反射学习之依赖注入示例
2019/06/14 PHP
个人总结的一些关于String、Function、Array的属性和用法
2007/01/10 Javascript
JQuery UI皮肤定制
2009/07/27 Javascript
JS代码优化技巧之通俗版(减少js体积)
2011/12/23 Javascript
jquery获取当前点击对象的value方法
2014/02/28 Javascript
jQuery中siblings()方法用法实例
2015/01/08 Javascript
JQuery基础语法小结
2015/02/27 Javascript
详谈js中数组(array)和对象(object)的区别
2017/02/27 Javascript
angularjs中回车键触发某一事件的方法
2017/04/24 Javascript
详解使用Typescript开发node.js项目(简单的环境配置)
2017/10/09 Javascript
JS/jQuery实现简单的开关灯效果【案例】
2019/02/19 jQuery
在vue+element ui框架里实现lodash的debounce防抖
2019/11/13 Javascript
Js代码中的span拼接问题解决
2019/11/22 Javascript
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2014/08/22 Python
python中__call__方法示例分析
2014/10/11 Python
在Python中的Django框架中进行字符串翻译
2015/07/27 Python
33个Python爬虫项目实战(推荐)
2019/07/08 Python
如何使用Python破解ZIP或RAR压缩文件密码
2020/01/09 Python
python读取tif图片时保留其16bit的编码格式实例
2020/01/13 Python
html5中使用hotcss.js实现手机端自适配的方法
2020/04/23 HTML / CSS
瑞典时尚服装购物网站:Miinto.se
2017/10/30 全球购物
岗位廉洁从政承诺书
2014/03/27 职场文书
公司合作意向书范文
2014/07/30 职场文书
民主生活会批评与自我批评总结
2014/10/17 职场文书
幼儿园大班教师个人工作总结
2015/02/05 职场文书
搞笑婚前保证书
2015/02/28 职场文书
2015最新民情日记范文
2015/06/26 职场文书
python 如何将两个实数矩阵合并为一个复数矩阵
2021/05/19 Python
正确使用MySQL INSERT INTO语句
2021/05/26 MySQL