Java操作Solr之SolrJ

添加SolrJ的jar包

  solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,

 1 <dependency>
 2     <groupId>org.apache.solr</groupId>
 3     <artifactId>solr-solrj</artifactId>
 4     <version>4.10.4</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>commons-logging</groupId>
 8     <artifactId>commons-logging</artifactId>
 9     <version>1.2</version>
10 </dependency>

创建索引

  使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。

 1 // 创建索引
 2 public static void createIndex() throws Exception {
 3     SolrServer solrServer = new HttpSolrServer("http://192.168.50.50:8080/solr/collection1");
 4     SolrInputDocument document = new SolrInputDocument();
 5     document.addField("id", "00001");
 6     document.addField("name", "solr全文检索");
 7     document.addField("price", 86.5f);
 8     document.addField("description", "这是一本关于solr的书籍!");
 9     UpdateResponse response = solrServer.add(document);
10     solrServer.commit();
11 }

删除索引

 1 //删除索引
 2 public void deleteIndex() throws Exception {
 3     SolrServer solrServer = new HttpSolrServer(solrUrl);
 4     //根据id删除
 5     UpdateResponse response = solrServer.deleteById("c0001");
 6     //根据多个id删除
 7     solrServer.deleteById("0001,0002");
 8     //自动查询条件删除
 9     solrServer.deleteByQuery("name:教程");
10     solrServer.commit();
11 }

搜索索引

 1 //查询索引
 2 public static void selectIndex() throws Exception {
 3     SolrServer solr = new HttpSolrServer(solrUrl);
 4     // 查询对象
 5     SolrQuery query = new SolrQuery();
 6     //设置查询条件,名称“q”是固定的且必须的
 7     //搜索keywords域,keywords是复制域包括name和description
 8     query.set("q", "keywords:java教程");
 9     // 设置商品分类、关键字查询
10     query.setQuery("name:数据 AND price:11.1");
11     // 设置价格范围
12     query.set("fq", "price:[1 TO 20]");
13     // 查询结果按照价格降序排序
14     //query.set("sort", "price desc");
15     query.addSort("price", ORDER.desc);
16     // 请求查询
17     QueryResponse response = solr.query(query);
18     // 查询结果
19     SolrDocumentList docs = response.getResults();
20     // 查询文档总数
21     System.out.println("查询文档总数" + docs.getNumFound());
22     for (SolrDocument doc : docs) {
23         String id = (String) doc.getFieldValue("id");
24         String name = (String)doc.getFieldValue("name");
25         Float price = (Float)doc.getFieldValue("price");
26         String description = (String)doc.getFieldValue("description");
27         System.out.println(id);
28     }
29 }

高亮搜索索引

 1 // 分页和高亮
 2 public static void selectHeightLight() throws Exception {
 3     SolrServer solr = new HttpSolrServer(solrUrl);
 4     // 查询对象
 5     SolrQuery query = new SolrQuery();
 6     // text是name、title等众多字段的复制域
 7     query.setQuery("text:算");
 8     // 每页显示记录数
 9     int pageSize = 2;
10     // 当前页码
11     int curPage = 1;
12     // 开始记录下标
13     int begin = pageSize * (curPage - 1);
14     // 起始下标
15     query.setStart(begin);
16     // 结束下标
17     query.setRows(pageSize);
18     // 设置高亮参数
19     query.setHighlight(true); // 开启高亮组件
20     query.addHighlightField("name");// 高亮字段
21     query.setHighlightSimplePre("<span color='red'>");//前缀标记
22     query.setHighlightSimplePost("</span>");// 后缀标记
23     // 请求查询
24     QueryResponse response = solr.query(query);
25     // 查询结果
26     SolrDocumentList docs = response.getResults();
27     // 查询文档总数
28     System.out.println("查询文档总数" + docs.getNumFound());
29     for (SolrDocument doc : docs) {
30         // 商品主键
31         String id = (String) doc.getFieldValue("id");
32         // 商品名称
33         String name = (String)doc.getFieldValue("name");
34         // 高亮信息
35         if(response.getHighlighting() != null) {
36             if(response.getHighlighting().get(id) != null) {
37                 // 取出高亮片段
38                 Map<String, List<String>> map = response.getHighlighting().get(id);
39                 if(map.get("name") != null) {
40                     for(String s : map.get("name")) {
41                         System.out.println(s);
42                     }
43                 }
44             }
45         }
46     }
47 }

原文地址:https://www.cnblogs.com/guanghe/p/10493922.html