博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SolrJ的使用

Posted on 2018-12-07 21:40  yxma  阅读(149)  评论(0编辑  收藏  举报

SolrJ的使用

1、添加依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.myx.solr</groupId>
 8     <artifactId>day11solr</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11 
12     <dependencies>
13         <!-- Junit单元测试 -->
14         <dependency>
15             <groupId>junit</groupId>
16             <artifactId>junit</artifactId>
17             <version>4.12</version>
18         </dependency>
19         <dependency>
20             <groupId>org.apache.solr</groupId>
21             <artifactId>solr-solrj</artifactId>
22             <version>4.10.2</version>
23         </dependency>
24         <!-- Solr底层会使用到slf4j日志系统 -->
25         <dependency>
26             <groupId>org.slf4j</groupId>
27             <artifactId>slf4j-log4j12</artifactId>
28             <version>1.7.22</version>
29         </dependency>
30         <dependency>
31             <groupId>commons-logging</groupId>
32             <artifactId>commons-logging</artifactId>
33             <version>1.2</version>
34         </dependency>
35     </dependencies>
36     <build>
37         <plugins>
38             <!-- java编译插件 -->
39             <plugin>
40                 <groupId>org.apache.maven.plugins</groupId>
41                 <artifactId>maven-compiler-plugin</artifactId>
42                 <version>3.2</version>
43                 <configuration>
44                     <source>1.8</source>
45                     <target>1.8</target>
46                 </configuration>
47             </plugin>
48         </plugins>
49     </build>
50 
51 
52 </project>

2、创建实体类

 1 package pojo;
 2 
 3 import org.apache.solr.client.solrj.beans.Field;
 4 
 5 public class Item {
 6     @Field   //表明当前字段要添加到索引库中
 7     private long id;
 8     @Field
 9     private String title;
10     @Field
11     private long price;
12 
13     public long getId() {
14         return id;
15     }
16 
17     public void setId(long id) {
18         this.id = id;
19     }
20 
21     public String getTitle() {
22         return title;
23     }
24 
25     public void setTitle(String title) {
26         this.title = title;
27     }
28 
29     public long getPrice() {
30         return price;
31     }
32 
33     public void setPrice(long price) {
34         this.price = price;
35     }
36 }

3、SolrJ的增删改查

 

3.1使用Document向Solr添加或修改索引

 1 /*
 2   5     *
 3   6     * 使用Document向Solr添加或修改索引*/
 4   7     @Test
 5   8     public void testWrite1() throws IOException, SolrServerException {
 6   9         //连接Solr服务器
 7  10         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8  11         //创建Solr的输入Document
 9  12         SolrInputDocument document = new SolrInputDocument();
10  13 
11  14         //添加字段
12  15         document.addField("id",15L);
13  16         document.addField("title","锤子手机,老罗会吹");
14  17         document.addField("price",992210);
15  18 
16  19         //添加Document到server
17  20         server.add(document);
18  21 
19  22         //提交请求,如果id不存在,则添加新数据,如果存在,则修改数据
20  23         server.commit();
21  24     }

 

 

3.2使用注解和JavaBean向Solr中添加或修改数据


 1 /*
 2  27     * 使用注解和JavaBean向Solr中添加或修改数据
 3  28     * */
 4  29     @Test
 5  30     public void testWrite2() throws IOException, SolrServerException {
 6  31         //连接Solr服务器
 7  32         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8  33         //创建一个实体对象
 9  34         Item item = new Item();
10  35         item.setId(16);
11  36         item.setTitle("oppo手机,照亮你的美");
12  37         item.setPrice(23330);
13  38 
14  39         //将对象添加到Server
15  40         server.addBean(item);
16  41         //提交请求,如果id不存在,则添加新数据,如果存在就修改数据
17  42         server.commit();
18  43     }

3.3 SolrJ删除索引数据

 1  45     /*
 2  46     * SolrJ删除索引数据
 3  47     *
 4  48     * */
 5  49 
 6  50     @Test
 7  51     public void testDelete() throws IOException, SolrServerException {
 8  52         //连接Solr服务器
 9  53         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
10  54 
11  55         //根据id删除数据,注意这里需要传字符串
12  56          // server.deleteById("16");
13  57 
14  58         //根据查询条件删除,参数是字符串格式,写出查询条件
15  59         server.deleteByQuery("title:Apple");
16  60         //提交
17  61         server.commit();
18  62     }
19  63 

 

 

3.4 使用Solr查询索引,返回的是Document形式

 1 /*
 2  65     * 使用Solr查询索引,返回的是Document形式
 3  66     * */
 4  67     @Test
 5  68     public void testQueryDocument() throws SolrServerException {
 6  69         //连接Solr服务器
 7  70         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8  71 
 9  72         //创建查询对象
10  73         SolrQuery solrQuery = new SolrQuery("title:华为");
11  74 
12  75         //执行查询,获取响应
13  76         QueryResponse response = server.query(solrQuery);
14  77 
15  78         //获取查询结果,本质是一个Document的集合
16  79         SolrDocumentList results = response.getResults();
17  80         //获取总条数
18  81         System.out.println("本次共搜索到" + results.size() + "条数据");
19  82 
20  83         //遍历集合
21  84         for (SolrDocument document : results) {
22  85             System.out.println("id" + document.getFieldValue("id"));
23  86             System.out.println("title" + document.getFieldValue("title"));
24  87             System.out.println("price" + document.getFieldValue("price"));
25  88         }
26  89     }

3.5使用SolrJ查询索引,返回的是JavaBean

 1 /*
 2  92     * 使用SolrJ查询索引,返回的是JavaBean
 3  93     * */
 4  94 
 5  95     @Test
 6  96     public void testQueryBeans() throws SolrServerException {
 7  97         //连接Solr服务器
 8  98         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 9  99 
10 100         //创建查询对象:SolrQuery
11 101         SolrQuery query = new SolrQuery("title:华为");
12 102         //执行查询,获取响应
13 103         QueryResponse response = server.query(query);
14 104 
15 105         //获取查询结果,指定实体类的类型,返回实体类的集合
16 106         List<Item> list = response.getBeans(Item.class);
17 107 
18 108         //打印总条数
19 109         System.out.println("本次共搜索到" + list.size() + "条数据");
20 110 
21 111         //遍历集合
22 112         for (Item item : list) {
23 113             System.out.println(item.getId());
24 114             System.out.println(item.getTitle());
25 115             System.out.println(item.getPrice());
26 116         }
27 117     }
28 118 

4 高级查询

4.1布尔查询

 1 /*
 2 121     * 布尔查询
 3 122     * */
 4 123     @Test
 5 124     public void testQueryBoolean() throws SolrServerException {
 6 125         //连接Solr服务器
 7 126         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8 127 
 9 128         //创建查询对象:SolrQuery,并且使用Boolean操作,可选OR AND ONT
10 129         SolrQuery query = new SolrQuery("title:华为 OR 小米");
11 130         //执行查询,获取响应
12 131         QueryResponse response = server.query(query);
13 132 
14 133         //获取查询结果,指定实体类的类型,返回实体类的集合
15 134         List<Item> list = response.getBeans(Item.class);
16 135 
17 136         //打印总条数
18 137         System.out.println("本次共搜索到" + list.size() + "条数据");
19 138 
20 139         //遍历集合
21 140         for (Item item : list) {
22 141             System.out.println(item.getId());
23 142             System.out.println(item.getTitle());
24 143             System.out.println(item.getPrice());
25 144         }
26 145     }
27 146 

4.2 相似度查询

 1 148     /*
 2 149     * 相似度查询
 3 150     * */
 4 151     @Test
 5 152     public void testFuzzyQuery() throws SolrServerException {
 6 153         //连接Solr服务器
 7 154         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8 155 
 9 156         //相似度查询,与lucene的相似度查询一致,允许的编辑举例是0-2,默认是2
10 157         SolrQuery query = new SolrQuery("title:applk~");
11 158         //执行查询,获取响应
12 159         QueryResponse response = server.query(query);
13 160 
14 161         //获取查询结果,指定实体类的类型,返回实体类的集合
15 162         List<Item> list = response.getBeans(Item.class);
16 163 
17 164         //打印总条数
18 165         System.out.println("本次共搜索到" + list.size() + "条数据");
19 166 
20 167         //遍历集合
21 168         for (Item item : list) {
22 169             System.out.println(item.getId());
23 170             System.out.println(item.getTitle());
24 171             System.out.println(item.getPrice());
25 172         }
26 173     }
27 174 

4.3 范围查询,闭区间

 1 176     /*
 2 177     * 范围查询,闭区间
 3 178     * */
 4 179     @Test
 5 180     public void testScoreQuery() throws SolrServerException {
 6 181         //连接Solr服务器
 7 182         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8 183 
 9 184         //创建查询对象
10 185         SolrQuery query = new SolrQuery("price:[100000 TO 200000]");
11 186         //执行查询,获取响应
12 187         QueryResponse response = server.query(query);
13 188 
14 189         //获取查询结果,指定实体类的类型,返回实体类的集合
15 190         List<Item> list = response.getBeans(Item.class);
16 191 
17 192         //打印总条数
18 193         System.out.println("本次共搜索到" + list.size() + "条数据");
19 194 
20 195         //遍历集合
21 196         for (Item item : list) {
22 197             System.out.println(item.getId());
23 198             System.out.println(item.getTitle());
24 199             System.out.println(item.getPrice());
25 200         }
26 201     }
27 202 

 

4.4 使用SolrJ查询索引,并且设置排序

 1 /*
 2 205     * 使用SolrJ查询索引,并且设置排序
 3 206     * */
 4 207 
 5 208     @Test
 6 209     public void testSortedQuery() throws SolrServerException {
 7 210         //连接Solr服务器
 8 211         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 9 212 
10 213         //创建查询对象
11 214         SolrQuery query = new SolrQuery("title:华为");
12 215 
13 216         //设置查询的排序参数,参数:排序的字段名、排序方式
14 217         query.setSort("price", SolrQuery.ORDER.desc);
15 218 
16 219         //执行查询,获取响应
17 220         QueryResponse response = server.query(query);
18 221 
19 222         //获取查询结果,指定实体类的类型,返回实体类的集合
20 223         List<Item> list = response.getBeans(Item.class);
21 224 
22 225         //打印总条数
23 226         System.out.println("本次共搜索到" + list.size() + "条数据");
24 227 
25 228         //遍历集合
26 229         for (Item item : list) {
27 230             System.out.println(item.getId());
28 231             System.out.println(item.getTitle());
29 232             System.out.println(item.getPrice());
30 233         }
31 234     }

4.5 查询索引并且分页

 1 /*
 2 237     * 查询索引并且分页
 3 238     * */
 4 239     @Test
 5 240     public void testPageQuery() throws SolrServerException {
 6 241         //准备分页参数
 7 242         int pageNum = 2; //查询的页数
 8 243         int pageSize = 5; // 每页显示条数
 9 244         int start = (pageNum - 1) * pageSize;//当前页的起始条数
10 245 
11 246         //连接Solr服务器
12 247         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
13 248 
14 249         //创建查询对象
15 250         SolrQuery query = new SolrQuery("title:手机");
16 251 
17 252         //设置查询的排序参数,参数:排序的字段名、排序方式
18 253         query.setSort("price", SolrQuery.ORDER.desc);
19 254 
20 255         //设置分页信息到查询对象中
21 256         query.setStart(start);
22 257         query.setRows(5);
23 258 
24 259         //执行查询,获取响应
25 260         QueryResponse response = server.query(query);
26 261 
27 262         //获取查询结果,指定实体类的类型,返回实体类的集合
28 263         List<Item> list = response.getBeans(Item.class);
29 264 
30 265         //打印总条数
31 266         System.out.println("本次共搜索到" + list.size() + "条数据");
32 267 
33 268         //遍历集合
34 269         for (Item item : list) {
35 270             System.out.println(item.getId());
36 271             System.out.println(item.getTitle());
37 272             System.out.println(item.getPrice());
38 273         }
39 274     }

4.6 查询索引并且高亮

 1   /*
 2 278     * 查询索引并且高亮
 3 279     * */
 4 280     @Test
 5 281     public void testHighlightQuery() throws SolrServerException {
 6 282         //连接Solr服务器
 7 283         HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
 8 284 
 9 285         //创建查询对象
10 286         SolrQuery query = new SolrQuery("title:华为");
11 287 
12 288         //设置高亮标签
13 289         query.setHighlightSimplePre("<em>");
14 290         query.setHighlightSimplePost("</em>");
15 291 
16 292         //高亮字段
17 293         query.addHighlightField("title");
18 294 
19 295         //执行查询,获取响应
20 296         QueryResponse response = server.query(query);
21 297 
22 298         // 解析高亮响应结果,是一个Map
23 299         // 外层的Map:它的键是文档的id,值是这个文档的其它高亮字段,又是一个Map
24 300         // 内存的Map:是其它高亮字段,键是其它字段的名称,值是这个字段的值,这个值是一个List
25 301         Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
26 302         //获取查询结果,指定实体类的类型,返回实体类的集合
27 303         List<Item> list = response.getBeans(Item.class);
28 304 
29 305         //打印总条数
30 306         System.out.println("本次共搜索到" + list.size() + "条数据");
31 307 
32 308         //遍历集合
33 309         for (Item item : list) {
34 310             long id = item.getId();
35 311             System.out.println("id: " + id);
36 312             // 这里ID是long类型,与集合的键不匹配,所以我们需要把id转为String类型,再get
37 313             System.out.println("title: " + highlighting.get(id+"").get("title").get(0));
38 314             System.out.println("price: " + item.getPrice());
39 315 
40 316         }
41 317     }