lucene 7.x 查询

 1 @Test
 2     public void indexSearch() throws IOException, ParseException {
 3         
 4         //Termquery:精确string查询
 5     //    Query termQuery = new TermQuery(new Term("id","1"));
 6     //    doSearch(termQuery);
 7         
 8         //数值范围查询
 9 //        Query rangeQuery = FloatPoint.newRangeQuery("price",55,56);
10     //    doSearch(rangeQuery);
11         
12         //组合查询
13         /*Query q1 = new TermQuery(new Term("description","java"));
14         Query q2 = new TermQuery(new Term("name","java"));
15         
16         BooleanQuery.Builder builder = new BooleanQuery.Builder();
17         builder.add(new BooleanClause(q1, Occur.MUST));
18         builder.add(new BooleanClause(q2, Occur.MUST_NOT));
19         doSearch(builder.build());*/
20         
21         
22         //queryparser,需要指定分词器
23         /*QueryParser parser = new QueryParser("description",new StandardAnalyzer());
24         Query query = parser.parse("lucene");
25         query = parser.parse("description:lucene AND name:lucene");
26         doSearch(query);*/
27         
28         
29         //多域查询
30         //默认的搜索域,或的关系
31         String[] fields = {"name","description"};
32         MultiFieldQueryParser parser = new MultiFieldQueryParser(fields,new StandardAnalyzer());
33         Query query = parser.parse("lucene");
34     //    query = parser.parse("description:lucene AND name:lucene");
35     //    query = parser.parse("description:lucene NOT name:lucene");
36         query = parser.parse("lucene java");//以或的关系在域中搜索
37         doSearch(query);
38     }
 1 private void doSearch (Query query) {
 2         Directory directory;
 3         try {
 4             directory = FSDirectory.open(Paths.get("F:/luceneIndex"));
 5         
 6         IndexReader reader = DirectoryReader.open(directory);
 7         //创建IndexSearcher
 8         IndexSearcher searcher = new IndexSearcher(reader);
 9         
10         //10表示显示的条数
11         TopDocs topDocs = searcher.search(query,10);
12         long count = topDocs.totalHits;
13         System.out.println("匹配的总条数是----" + count);
14         
15         //根据相关度排序后的结果
16         ScoreDoc[] docs = topDocs.scoreDocs;
17         for (ScoreDoc scoreDoc : docs) {
18             int docId = scoreDoc.doc;
19             Document document = searcher.doc(docId);
20             System.out.println("id----"+document.get("id"));
21             System.out.println("name----"+document.get("name"));
22             /*System.out.println("price----"+document.get("price"));
23             System.out.println("pic----"+document.get("pic"));
24             System.out.println("description----"+document.get("description"));*/
25             System.out.println("======================");
26         }
27         reader.close();
28         } catch (IOException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32         
33     }

需要注意的组合查询的api变化,与老版本的直接new BooleanQuery()相比变化还是挺大的

 

转载于:https://www.cnblogs.com/tele-share/p/9205589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值