Lucene6.0学习笔记——建立索引
2016-07-15 00:00
796 查看
1.定义相关变量
filePath:需要创建索引的源文件地址
indexPath:索引保存地址
analyzer:定义分词器,这里采用lucene自带的中文分词器
2.建立索引
3.文件转Document方法
StringField:不会进行分词操作;
TextField:会进行分词操作。
private final static String filePath="E:\\workspace\\luceneDemo\\files"; private final static Path indexPath=Paths.get("E:\\workspace\\luceneDemo\\indexStore"); public static Analyzer analyzer = new SmartChineseAnalyzer();
filePath:需要创建索引的源文件地址
indexPath:索引保存地址
analyzer:定义分词器,这里采用lucene自带的中文分词器
2.建立索引
public static void createIndex(){ List<Document> doc = File2DocumentUtil.files2Document(filePath); try { /*索引文件采用物理存储*/ FSDirectory directory = FSDirectory.open(indexPath); /*索引文件内存存储*/ //RAMDirectory directory1 = new RAMDirectory(); //配置indexWriter,写入索引 IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter indexWriter=new IndexWriter(directory, config); //创建之前删除所有索引 indexWriter.deleteAll(); //添加需要建立索引的Document indexWriter.addDocuments(doc); //提交写入 indexWriter.commit(); //关闭indexWriter indexWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3.文件转Document方法
public static List<Document> files2Document(String filePath) { File dir=new File(filePath); List<Document> list=new ArrayList<>(); for(File file:dir.listFiles()){ Document doc=new Document(); doc.add(new TextField("name", file.getName(), Store.YES)); doc.add(new StringField("path", file.getPath(), Store.YES)); /*设置排序字段*/ doc.add(new NumericDocValuesField("size",file.length())); doc.add(new StringField("size", String.valueOf(file.length()), Store.YES)); doc.add(new TextField("content", getFileContent(file), Store.YES)); list.add(doc); } return list; }
StringField:不会进行分词操作;
TextField:会进行分词操作。
相关文章推荐
- 学学Java Servlet:servlet的简介
- Java线程中run和start方法的区别
- 移动端WEB调试
- Way.js Vue.js Avalon.js Pat.js Jquery.my
- 禁止RecycleView的滑动
- 关于ODBC api 执行sqlserver存储过程的问题
- oracle存储过程
- 适用于spring项目的httpinvoker接口
- junit常用注解详细说明
- java程序的耦合度是什么
- 二分查找以及二分查找的时间复杂度
- 一些常用的eclipse技巧~没见过的,知道的省掉自己查~
- JOIN关联表中ON,WHERE后面跟条件的区别
- 判断手机是否安装高德/百度地图
- 自动生成
- 浏览器表单提交不支持put与delete方法
- 在eclipse中把项目部署到tomcat中时,发现项目文件不完整
- 任意Point弹出自定义View
- 从零开始(5)
- eclipse 快捷键 ctrl+o