Lucene入门学习二
接上篇:增删改查
增加:这里不做过多阐述。
删除:删除全部,根据条件删除
修该:先删除,后添加
查询(*):查询所有,精确查询,根据数值范围查询,组合查询,解析查询。
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 import org.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 }
本文来自博客园,作者:土木转行的人才,转载请注明原文链接