lucene创建索引以及索引文件合并
2017-05-05 18:43
337 查看
1 package test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.nio.file.Path; 6 import java.util.ArrayList; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.apache.lucene.analysis.standard.StandardAnalyzer; 11 import org.apache.lucene.document.Document; 12 import org.apache.lucene.document.Field.Store; 13 import org.apache.lucene.document.StringField; 14 import org.apache.lucene.index.DirectoryReader; 15 import org.apache.lucene.index.IndexReader; 16 import org.apache.lucene.index.IndexWriter; 17 import org.apache.lucene.index.IndexWriterConfig; 18 import org.apache.lucene.store.Directory; 19 import org.apache.lucene.store.FSDirectory; 20 21 public class IndexCreate { 22 public static void main(String[] args) { 23 // makeIndexs(); 24 mergeIndexFiles(); 25 searchIndexFileSize(); 26 27 } 28 29 /** 30 * 创建索引 31 */ 32 public static void makeIndexs() { 33 String sql = " select * from (select rownum r,t.* from TB_MENU t) m where m.r>=15 "; 34 OracleDBUtils db = new OracleDBUtils(); 35 List<String> list = new ArrayList<String>(); 36 list.add("ID"); 37 list.add("MENUTYPE"); 38 list.add("MENUNAME"); 39 list.add("URL"); 40 list.add("ICONPATH"); 41 List<Map<String, Object>> res = db.exec(sql, list); 42 /** 43 * 创建索引文件 44 */ 45 IndexWriter writer = null; 46 Directory directory = null; 47 try { 48 Path path = new File("D:/temp").toPath(); 49 directory = FSDirectory.open(path); 50 writer = new IndexWriter(directory, new IndexWriterConfig( 51 new StandardAnalyzer())); 52 for (Map<String, Object> map : res) { 53 Document doc = new Document(); 54 for (String key : list) { 55 String value = map.get(key) == null ? "" : map.get(key) 56 .toString(); 57 doc.add(new StringField(key, value, Store.YES)); 58 } 59 writer.addDocument(doc); 60 } 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } finally { 64 try { 65 writer.commit(); 66 writer.close(); 67 directory.close(); 68 System.out.println("索引创建成功!"); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 73 } 74 } 75 76 /** 77 * 合并索引文件 78 */ 79 public static void mergeIndexFiles() { 80 IndexWriter writer = null; 81 Directory directory = null; 82 Directory tempDir = null; 83 try { 84 Path path = new File("D:/luceneDir").toPath(); 85 directory = FSDirectory.open(path); 86 Path temp = new File("D:/temp").toPath(); 87 tempDir = FSDirectory.open(temp); 88 writer = new IndexWriter(directory, 89 new IndexWriterConfig(new StandardAnalyzer())); 90 writer.addIndexes(tempDir); 91 writer.commit(); 92 } catch (IOException e) { 93 e.printStackTrace(); 94 } finally{ 95 try { 96 writer.close(); 97 directory.close(); 98 tempDir.close(); 99 System.out.println("索引文件合并成功!"); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } 103 } 104 } 105 106 public static void searchIndexFileSize() { 107 Directory directory = null; 108 try { 109 Path path = new File("D:/luceneDir").toPath(); 110 directory = FSDirectory.open(path); 111 IndexReader reader=DirectoryReader.open(directory); 112 System.out.println(reader.maxDoc()); 113 } catch (IOException e) { 114 e.printStackTrace(); 115 } finally{ 116 try { 117 directory.close(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } 121 } 122 } 123 }
结果如下:
相关文章推荐
- 01-lucene简介和索引创建以及搜索
- lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
- Lucene.Net在创建好索引文件内搜索
- oracle 重建索引以及导出所有的索引脚本(可以解决还原数据库文件时先还原数据,在重新用脚本创建索引)
- 通过队列解决Lucene文件并发创建索引
- lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
- 通过队列解决Lucene文件并发创建索引
- ogre:在运行时,为entity添加骨骼文件,以及合并骨骼文件的方法
- Python分割文件以及合并文件
- Python分割文件以及合并文件
- Lucene源代码索引合并之field,term层
- Lucene.NET搜索多个索引文件
- Asp.net文件上传 重命名 以及自动创建上传目录
- lucene学习----创建索引
- lucene.net索引文件存储简析
- Lucene源代码之创建索引小结
- lucene创建索引
- lucene索引合并与增量索引
- 使用File类判断文件是否存在以及创建,拷贝和移动文件
- lucene的索引文件