package step1; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.document; import org.apache.lucene.document.TextField; import org.apache.lucene.document.Field.Store; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class HelloWorld { //删除索引 public void delete()throws IOException{ Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer()); //创建一个indexwriter对象 IndexWriter indexWriter = new IndexWriter(directory, config); //创建一个查询条件 Query query = new TermQuery(new Term("content", "mybatis")); //根据查询条件删除 indexWriter.deletedocuments(query); //关闭indexwriter indexWriter.close(); } //修改索引 public void update()throws IOException{ Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer()); //创建一个indexwriter对象 IndexWriter indexWriter = new IndexWriter(directory, config); //创建一个document对象 document document = new document(); //向document对象中添加域。 //不同的document可以有不同的域,同一个document可以有相同的域。 document.add(new TextField("filename", "web", Store.YES)); document.add(new TextField("content", "hello lucene", Store.YES)); indexWriter.updatedocument(new Term("content", "web"), document); //关闭indexWriter indexWriter.close(); } //添加索引 public void add() throws IOException{ //索引库存放路径 Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer()); //创建一个indexwriter对象 IndexWriter indexWriter = new IndexWriter(directory, config); //创建一个document对象 document document = new document(); //向document对象中添加域。 //不同的document可以有不同的域,同一个document可以有相同的域。 document.add(new TextField("filename", "test", Store.YES)); document.add(new TextField("content", "www.educoder.net", Store.YES)); //添加文档到索引库 indexWriter.adddocument(document); //关闭indexwriter indexWriter.close(); } }第2关:Lucene索引库查询 - 使用Query的子类查询
package step2; import java.io.File; import java.io.IOException; import org.apache.lucene.document.document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class QueryIndex { //查询 public void query() throws IOException{ IndexSearcher indexSearcher = getIndexSearcher(); //创建一个布尔查询对象 BooleanQuery query = new BooleanQuery(); //创建第一个查询条件 Query query1 = new TermQuery(new Term("filename", "web")); Query query2 = new TermQuery(new Term("content", "lucene")); Query query3 = NumericRangeQuery.newLongRange("size", 1l, 40l, true, true); //组合查询条件 query.add(query1, Occur.SHOULD); query.add(query2, Occur.SHOULD); query.add(query3, Occur.MUST); //执行查询 printResult(query, indexSearcher); } //输出结果 private void printResult(Query query, IndexSearcher indexSearcher) throws IOException { TopDocs topDocs = indexSearcher.search(query, 10); ScoreDoc[] topDocArr = topDocs.scoreDocs; System.out.println("查询的结果总条数" + topDocs.totalHits); for (ScoreDoc scoreDoc : topDocArr) { document document = indexSearcher.doc(scoreDoc.doc); //获取document对象 System.out.println("filename:" + document.get("filename")); System.out.println("size:" + document.get("size")); System.out.println("content:"+document.get("content")); } } //获取IndexSearcher对象 private IndexSearcher getIndexSearcher() throws IOException { //指定索引库存放的路径 Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); //创建indexReader对象 IndexReader reader = DirectoryReader.open(directory); //创建indexsearcher对象 IndexSearcher searcher = new IndexSearcher(reader); return searcher; } }第3关:Lucene索引库查询 - 使用QueryParser查询
package step3; import java.io.File; import java.io.IOException; import org.apache.lucene.document.document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; public class QueryIndex { //查询 public void query() throws Exception{ IndexSearcher indexSearcher = getIndexSearcher(); //可以指定默认搜索的域是多个 String[] fields = {"filename", "content"}; //创建一个MulitFiledQueryParser对象 MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer()); Query query = queryParser.parse("+filename:web +content:spring"); //输出生成的查询语句 //执行查询 printResult(query, indexSearcher); } //输出结果 private void printResult(Query query, IndexSearcher indexSearcher) throws IOException { TopDocs topDocs = indexSearcher.search(query, 10); ScoreDoc[] topDocArr = topDocs.scoreDocs; System.out.println("查询的结果总条数" + topDocs.totalHits); for (ScoreDoc scoreDoc : topDocArr) { document document = indexSearcher.doc(scoreDoc.doc); //获取document对象 System.out.println("filename:" + document.get("filename")); System.out.println("size:" + document.get("size")); } } //获取IndexSearcher对象 private IndexSearcher getIndexSearcher() throws IOException { //指定索引库存放的路径 Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); //创建indexReader对象 IndexReader reader = DirectoryReader.open(directory); //创建indexsearcher对象 IndexSearcher searcher = new IndexSearcher(reader); return searcher; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)