lucene入门-搜索方式
1
1 package com.home.utils; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.lucene.document.Document; 7 import org.apache.lucene.index.Term; 8 import org.apache.lucene.search.BooleanClause.Occur; 9 import org.apache.lucene.search.BooleanQuery; 10 import org.apache.lucene.search.IndexSearcher; 11 import org.apache.lucene.search.MatchAllDocsQuery; 12 import org.apache.lucene.search.NumericRangeQuery; 13 import org.apache.lucene.search.PhraseQuery; 14 import org.apache.lucene.search.Query; 15 import org.apache.lucene.search.ScoreDoc; 16 import org.apache.lucene.search.TermQuery; 17 import org.apache.lucene.search.TopDocs; 18 import org.apache.lucene.search.WildcardQuery; 19 import org.junit.Test; 20 21 /** 22 * 1、关键词查询 2、查询所有文档 3、范围查询 4、通配符查询 5、短语查询 6、Boolean查询 23 * 24 * @author Administrator 25 * 26 */ 27 public class QueryTest { 28 29 private void showData(Query query) throws Exception { 30 IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory); 31 TopDocs topDocs = indexSearcher.search(query, 25); 32 ScoreDoc[] scoreDocs = topDocs.scoreDocs; 33 List<Article> articleList = new ArrayList<Article>(); 34 for (ScoreDoc scoreDoc : scoreDocs) { 35 Document document = indexSearcher.doc(scoreDoc.doc); 36 Article article = DocumentUtils.document2Article(document); 37 articleList.add(article); 38 } 39 40 for (Article article : articleList) { 41 System.out.println(article.getId()); 42 System.out.println(article.getTitle()); 43 System.out.println(article.getContent()); 44 } 45 } 46 47 /** 48 * 关键词查询 因为没有分词器,所以区分大小写 49 */ 50 @Test 51 public void testTermQuery() throws Exception { 52 Term term = new Term("title", "lucene"); 53 Query query = new TermQuery(term); 54 this.showData(query); 55 } 56 57 /** 58 * 查询所有的文档 59 */ 60 @Test 61 public void testAllDocQuery() throws Exception { 62 Query query = new MatchAllDocsQuery(); 63 this.showData(query); 64 } 65 66 /** 67 * 通配符查询 * 代表任意多个任意字符 ? 任意一个任意字符 68 */ 69 @Test 70 public void testWildCartQuery() throws Exception { 71 Term term = new Term("title", "*.java"); 72 Query query = new WildcardQuery(term); 73 this.showData(query); 74 } 75 76 /** 77 * 短语查询 所有的关键词对象必须针对同一个属性 78 * 79 * @param query 80 * @throws Exception 81 */ 82 @Test 83 public void testPharseQuery() throws Exception { 84 Term term = new Term("title", "lucene"); 85 Term term2 = new Term("title", "搜索"); 86 PhraseQuery query = new PhraseQuery(); 87 query.add(term); 88 query.add(term2, 4); 89 this.showData(query); 90 91 } 92 93 /** 94 * boolean查询 各种关键词的组合 95 * 96 * @param query 97 * @throws Exception 98 */ 99 @Test 100 public void testBooleanQuery() throws Exception { 101 Term term = new Term("title", "北京"); 102 TermQuery termQuery = new TermQuery(term); 103 Term term2 = new Term("title", "美女"); 104 TermQuery termQuery2 = new TermQuery(term2); 105 Term term3 = new Term("title", "北京美女"); 106 TermQuery termQuery3 = new TermQuery(term3); 107 108 BooleanQuery booleanQuery = new BooleanQuery(); 109 booleanQuery.add(termQuery, Occur.SHOULD); 110 booleanQuery.add(termQuery2, Occur.SHOULD); 111 booleanQuery.add(termQuery3, Occur.SHOULD); 112 this.showData(booleanQuery); 113 } 114 115 /** 116 * 范围查询 117 * @param query 118 * @throws Exception 119 */ 120 @Test 121 public void testRangeQuery() throws Exception{ 122 Query query = NumericRangeQuery.newLongRange("id", 5L, 10L, true, true); 123 this.showData(query); 124 } 125 126 127 }
原文地址:https://www.cnblogs.com/sharpest/p/5992564.html