前面我们已经完成了hello world的编写,其实全文检索lucene很简单的,当然主要还是里面自带的一些类我们不熟悉,但是这也没有关系,这些类的方法用起来也很简单,下面是一些教程的截图:
关于排序的一些简单方法:
1 public QueryResult search(String queryString, int firstResult, int maxResults) { 2 try { 3 // 1,把要搜索的文本解析为 Query 4 String[] fields = { "name", "content" }; 5 Map<String, Float> boosts = new HashMap<String, Float>();//(按相关度排序)某个属性的boost的值 (排序) 6 boosts.put("name", 3f); //认为标题很重要,权值大 7 // boosts.put("content", 1.0f); 默认为1.0f 8 9 QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer, boosts); 10 Query query = queryParser.parse(queryString); 11 12 return search(query, firstResult, maxResults); 13 } catch (Exception e) { 14 throw new RuntimeException(e); 15 } 16 } 17 18 public QueryResult search(Query query, int firstResult, int maxResults) { 19 IndexSearcher indexSearcher = null; 20 21 try { 22 // 2,进行查询 23 indexSearcher = new IndexSearcher(indexPath); 24 Filter filter = new RangeFilter("size", NumberTools.longToString(200) //文档document的过滤器 25 , NumberTools.longToString(1000), true, true); //过滤器的效率很低 26 27 // ========== 排序 (默认为相关度排序) 28 Sort sort = new Sort(); 29 sort.setSort(new SortField("size")); // (自定义排序)默认为升序 这个按size的大小排序 30 // sort.setSort(new SortField("size", true)); //(自定义排序)按降序排序 这个按size的大小排序 31 // ========== 32 33 TopDocs topDocs = indexSearcher.search(query, filter, 10000, sort); 34 35 int recordCount = topDocs.totalHits;
关于lucene的集中查询类:
1 /** 2 * 关键词查询TermQuery 3 * 4 * name:room 5 */ 6 @Test 7 public void testTermQuery() { 8 // Term term = new Term("name", "房间"); 9 // Term term = new Term("name", "Room"); // 英文关键词全是小写字符 10 Term term = new Term("content", "编辑");//属性中的关键字(键值对) 11 Query query = new TermQuery(term); 12 13 queryAndPrintResult(query); 14 } 15 16 /** 17 * 范围查询RangeQuery 18 * 19 * 包含边界:size:[0000000000001e TO 000000000000rs] 20 * 21 * 不包含边界:size:{0000000000001e TO 000000000000rs} 22 */ 23 @Test 24 public void testRangeQuery() { 25 Term lowerTerm = new Term("size", NumberTools.longToString(50));//下边界 26 Term upperTerm = new Term("size", NumberTools.longToString(100));//上边界 27 Query query = new RangeQuery(lowerTerm, upperTerm, false);//false表示不包含边界 28 29 queryAndPrintResult(query); 30 } 31 32 // public static void main(String[] args) { 33 // System.out.println(Long.MAX_VALUE); 34 // System.out.println(NumberTools.longToString(1000)); 35 // System.out.println(NumberTools.stringToLong("000000000000rs")); 36 // 37 // System.out.println(DateTools.dateToString(new Date(), Resolution.DAY)); 38 // System.out.println(DateTools.dateToString(new Date(), Resolution.MINUTE)); 39 // System.out.println(DateTools.dateToString(new Date(), Resolution.SECOND)); 40 // } 41 42 /** 43 * 通配符查询WildcardQuery 44 * 45 * '?' 代表一个字符, '*' 代表0个或多个字符 46 * 47 * name:房* 48 * 49 * name:*o* 50 * 51 * name:roo? 52 */ 53 @Test 54 public void testWildcardQuery() { 55 Term term = new Term("content", "luc?"); 56 // Term term = new Term("name", "ro*"); // 前缀查询 PrefixQuery 57 // Term term = new Term("name", "*o*"); 58 // Term term = new Term("name", "房*"); 59 Query query = new WildcardQuery(term); 60 61 queryAndPrintResult(query); 62 } 63 64 /** 65 * 短语查询PhraseQuery 66 * 67 * content:"? 绅士 ? ? 饭店" 68 * 69 * content:"绅士 饭店"~2 70 */ 71 @Test 72 public void testPhraseQuery() { 73 PhraseQuery phraseQuery = new PhraseQuery(); 74 // phraseQuery.add(new Term("content", "绅士"), 1); 75 // phraseQuery.add(new Term("content", "饭店"), 4); 76 77 phraseQuery.add(new Term("content", "编辑")); 78 phraseQuery.add(new Term("content", "lucene")); 79 phraseQuery.setSlop(2); 80 81 queryAndPrintResult(phraseQuery); 82 } 83 84 /** 85 * +content:"绅士 饭店"~2 -size:[000000000000dw TO 000000000000rs] 86 * 87 * +content:"绅士 饭店"~2 +size:[000000000000dw TO 000000000000rs] 88 * 89 * content:"绅士 饭店"~2 size:[000000000000dw TO 000000000000rs] 90 * 91 * +content:"绅士 饭店"~2 size:[000000000000dw TO 000000000000rs] 92 */ 93 @Test 94 public void testBooleanQuery() { 95 // 条件1 96 PhraseQuery query1 = new PhraseQuery(); 97 query1.add(new Term("content", "绅士")); 98 query1.add(new Term("content", "饭店")); 99 query1.setSlop(2); 100 101 // 条件2 102 Term lowerTerm = new Term("size", NumberTools.longToString(500)); 103 Term upperTerm = new Term("size", NumberTools.longToString(1000)); 104 Query query2 = new RangeQuery(lowerTerm, upperTerm, true); 105 106 // 组合 107 BooleanQuery boolQuery = new BooleanQuery(); 108 boolQuery.add(query1, Occur.MUST); 109 boolQuery.add(query2, Occur.SHOULD); 110 111 queryAndPrintResult(boolQuery); 112 }