Lucene入門學習二
阿新 • • 發佈:2018-12-27
接上篇:增刪改查
增加:這裡不做過多闡述。
刪除:刪除全部,根據條件刪除
修該:先刪除,後新增
查詢(*):查詢所有,精確查詢,根據數值範圍查詢,組合查詢,解析查詢。
1 package com.itheima.luncence; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer;8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.document.Field.Store; 10 import org.apache.lucene.document.TextField; 11 import org.apache.lucene.index.DirectoryReader; 12 import org.apache.lucene.index.IndexReader; 13 import org.apache.lucene.index.IndexWriter; 14 importorg.apache.lucene.index.IndexWriterConfig; 15 import org.apache.lucene.index.Term; 16 import org.apache.lucene.queryparser.classic.QueryParser; 17 import org.apache.lucene.search.BooleanClause.Occur; 18 import org.apache.lucene.search.BooleanQuery; 19 import org.apache.lucene.search.IndexSearcher;20 import org.apache.lucene.search.MatchAllDocsQuery; 21 import org.apache.lucene.search.NumericRangeQuery; 22 import org.apache.lucene.search.Query; 23 import org.apache.lucene.search.ScoreDoc; 24 import org.apache.lucene.search.TermQuery; 25 import org.apache.lucene.search.TopDocs; 26 import org.apache.lucene.store.Directory; 27 import org.apache.lucene.store.FSDirectory; 28 import org.apache.lucene.util.Version; 29 import org.junit.Test; 30 import org.wltea.analyzer.lucene.IKAnalyzer; 31 32 /** 33 * 34 * @author jack cheng 35 * 36 */ 37 public class LuceneManager { 38 39 //公共部分儲存到索引庫 40 public IndexWriter getIndexWriter() throws Exception{ 41 // 1、建立Directory 42 // JDK 1.7以後 open只能接收Path 43 Directory directory = FSDirectory.open(new File("D:\\BaiduYunDownload\\index")); 44 // 2、建立IndexWriter 45 Analyzer analyzer = new StandardAnalyzer(); 46 IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); 47 IndexWriter indexWriter = new IndexWriter(directory, config);//************ 48 //indexWriter.deleteAll();// 清除以前的index 49 return indexWriter; 50 } 51 52 //全刪除 53 @Test 54 public void deleteAll() throws Exception{ 55 IndexWriter writer = getIndexWriter(); 56 writer.deleteAll(); 57 writer.close(); 58 System.out.println("刪除成功"); 59 } 60 //根據條件刪除 61 @Test 62 public void deleteBy() throws Exception{ 63 IndexWriter writer = getIndexWriter(); 64 Query query = new TermQuery(new Term("filename", "apache")); 65 writer.deleteDocuments(query);; 66 writer.close(); 67 System.out.println("刪除成功2"); 68 } 69 70 //修該 71 @Test 72 public void update() throws Exception{ 73 IndexWriter writer = getIndexWriter(); 74 Document doc = new Document(); 75 doc.add(new TextField("fileN", "測試檔名",Store.YES)); 76 doc.add(new TextField("fileC", "測試檔案內容",Store.YES)); 77 writer.updateDocument(new Term("filename", "lucene"), doc, new IKAnalyzer());//刪除第一個,加入第二個 78 79 writer.close(); 80 System.out.println("修該成功"); 81 } 82 83
84 //*************************************************************************** 85 //公共部分讀取索引庫方法 86 public IndexSearcher getIndexSearcher() throws Exception{ 87 //建立索引庫 88 Directory directory = FSDirectory.open(new File("D:\\BaiduYunDownload\\index")); 89 //建立indexreader 90 IndexReader indexReader = DirectoryReader.open(directory); 91 //建立Searcher 92 IndexSearcher indexSearcher = new IndexSearcher(indexReader); 93 return indexSearcher; 94 95 } 96 97 //結果顯示(公共部分) 98 public void printResult(IndexSearcher indexSearcher,Query query) throws Exception{ 99 TopDocs topDocs = indexSearcher.search(query, 20); 100 int totalHits = topDocs.totalHits; 101 System.out.println("記錄數" + totalHits); 102 // 6、根據TopDocs獲取ScoreDoc物件 103 ScoreDoc[] scoreDocs = topDocs.scoreDocs; 104 for (ScoreDoc scoreDoc : scoreDocs) { 105 106 // 7、根據searcher和ScoreDoc物件獲取具體的Document物件 107 Document document = indexSearcher.doc(scoreDoc.doc); 108 109 // 8、根據Document物件獲取需要的值 110 System.out.println("檔名:" + document.get("filename") 111 + " " + "檔案路徑:" + document.get("filepath") 112 + "檔案大小:"+ document.get("filesize") 113 + "檔案內容:" + document.get("content")); 114 System.out.println("-----------------------------------------"); 115 } 116 117 } 118 119 //查詢所有 120 @Test 121 public void testAll() throws Exception{ 122 IndexSearcher indexSearcher =getIndexSearcher(); 123 Query query = new MatchAllDocsQuery(); 124 printResult(indexSearcher, query); 125 //關閉流 126 indexSearcher.getIndexReader().close(); 127 } 128 129 //更具資料範圍查詢 130 @Test 131 public void testnumber() throws Exception{ 132 IndexSearcher indexSearcher =getIndexSearcher(); 133 Query query = NumericRangeQuery.newLongRange("filesize", 400l, 1000000l, true, true); 134 printResult(indexSearcher, query); 135 //關閉流 136 indexSearcher.getIndexReader().close(); 137 } 138 139 //組合查詢 140 @Test 141 public void Booleanquery() throws Exception{ 142 IndexSearcher indexSearcher =getIndexSearcher(); 143 BooleanQuery booleanquery = new BooleanQuery(); 144 TermQuery query2 = new TermQuery(new Term("filename","changes")); 145 TermQuery query1 = new TermQuery(new Term("content","changes")); 146 booleanquery.add(query2,Occur.MUST); 147 booleanquery.add(query1,Occur.MUST); 148 printResult(indexSearcher, booleanquery); 149 //關閉流 150 indexSearcher.getIndexReader().close(); 151 } 152 153 //解析查詢(多看文件) 154 @Test 155 public void testqueryparser() throws Exception{ 156 IndexSearcher indexSearcher =getIndexSearcher(); 157 158 QueryParser queryParser = new QueryParser("filename",new IKAnalyzer()); 159 //Query query =queryParser.parse("*:*");//查詢條件(查詢所有) 160 Query query =queryParser.parse("changes"); 161 printResult(indexSearcher, query); 162 //關閉流 163 indexSearcher.getIndexReader().close(); 164 } 165 }