码迷,mamicode.com
首页 > 其他好文 > 详细

SolrJ使用

时间:2018-07-26 23:34:55      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:for   exce   except   str   查询条件   des   com   set   .com   


1
//向solr索引库中添加索引 2 public void addDoc() throws Exception { 3 //创建solr客户端的对象 4 HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core"); 5 //创建文档对象 6 SolrInputDocument sd = new SolrInputDocument(); 7 8 sd.addField("id", "001"); 9 sd.addField("product_name", "solr教程"); 10 sd.addField("product_catalog_name", "IT技术"); 11 sd.addField("product_description", "很厉害的技术"); 12 sd.addField("product_price", 299); 13 sd.addField("product_picture", "001.jpg"); 14 //把文档加入服务器 15 client.add(sd); 16 client.commit(); 17 }

删除文档

1 public void delDoc() throws Exception {
2         //创建solr客户端的对象
3         HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
4         client.deleteById("001");
5     //    client.deleteByQuery("product_name:青蛙"); 根据查询删除
6         client.commit();
7     }

查询

public void queryDoc() throws Exception {
        //创建solr客户端的对象
        HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
        //创建solr的查询对象
        SolrQuery sq = new SolrQuery();
        //设置查询条件
        sq.set("q", "product_name:青蛙");
        sq.set("fq", "product_price:[10 TO 100]");
        sq.addSort("product_price",ORDER.asc);
        //查询
        QueryResponse qr = client.query(sq);
        //获得查询结果
        SolrDocumentList results = qr.getResults();
        //获得查询的记录数
        long numFound = results.getNumFound();
        System.out.println("查询的记录数是  " + numFound);
        for (SolrDocument sd : results) {
            //获得文档域
            String id = (String) sd.getFieldValue("id");
            String product_name = (String) sd.getFieldValue("product_name");
            String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");
            String product_description = (String) sd.getFieldValue("product_description"); 
            double product_price =  (double) sd.getFieldValue("product_price"); 
            System.out.println("--------------");
            System.out.println("id " + id);
            System.out.println("商品名字  " + product_name);
            System.out.println("商品类别名字 " + product_catalog_name);
            System.out.println("商品描述  " + product_description);
            System.out.println("商品价格  " + product_price);
        }
    }

带高亮的查询

 1 public void queryDoc1() throws Exception {
 2         //创建solr客户端的对象
 3         HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
 4         //创建solr的查询对象
 5         SolrQuery sq = new SolrQuery();
 6         //设置查询条件
 7         sq.set("q", "product_name:青蛙");
 8         sq.set("fq", "product_price:[10 TO 100]");
 9         sq.addSort("product_price",ORDER.asc);
10         //开启高亮
11         sq.setHighlight(true);
12         sq.addHighlightField("product_name");
13         sq.setHighlightSimplePre("<b>");
14         sq.setHighlightSimplePost("</b>");
15         //查询
16         QueryResponse qr = client.query(sq);
17         //获得查询结果
18         SolrDocumentList results = qr.getResults();
19         //获得查询的记录数
20         long numFound = results.getNumFound();
21         System.out.println("查询的记录数是  " + numFound);
22         for (SolrDocument sd : results) {
23             //获得文档域
24             String id = (String) sd.getFieldValue("id");
25             String product_name = (String) sd.getFieldValue("product_name");
26             String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");
27             String product_description = (String) sd.getFieldValue("product_description"); 
28             double product_price =  (double) sd.getFieldValue("product_price"); 
29             System.out.println("--------------");
30             System.out.println("id " + id);
31             System.out.println("商品名字  " + product_name);
32             System.out.println("商品类别名字 " + product_catalog_name);
33             System.out.println("商品描述  " + product_description);
34             System.out.println("商品价格  " + product_price);
35             //获得高亮的结构体
36             Map<String, Map<String, List<String>>> highlighting = qr.getHighlighting();
37             if(highlighting != null){
38                 //根据id来获得某一个域的高亮的内容
39                 Map<String, List<String>> map = highlighting.get(id);
40                 //根据具体的域来获得高亮内容
41                 List<String> list = map.get("product_name");
42                 if(list != null && list.size() > 0){
43                     //打印高亮内容
44                     for (String str : list) {
45                         System.out.println(str);
46                     }
47                 }
48             }
49         }
50     }

 

SolrJ使用

标签:for   exce   except   str   查询条件   des   com   set   .com   

原文地址:https://www.cnblogs.com/cat-fish6/p/9374656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!