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 }
结果如下:
时间: 2024-09-30 13:29:22