前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用SolrJ(即java客户端)开发Solr。

使用SolrJ(即java客户端)开发Solr。

作者头像
别先生
发布2019-09-29 11:12:44
1.4K0
发布2019-09-29 11:12:44
举报
文章被收录于专栏:别先生别先生

1、什么是SolrJ呢?   答:Solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务。开始配置schema.xml,/home/hadoop/soft/solr-4.10.3/example/solr/collection1/conf。添加IK中文分析器,然后定义定义自己的业务域。

注意:

  a、Indexed,Indexed Field可以进行搜索和排序。你还可以在indexed Field上运行Solr分析过程,此过程可修改内容以改进或更改结果。   b、Stored,Stored Field内容保存在索引中。这对于检索和醒目显示内容很有用,但对于实际搜索则不是必须的,例如,很多应用程序存储指向内容位置的指针而不是存储实际的文件内容。

代码语言:javascript
复制
 1 [root@localhost tomcat]# cd /home/hadoop/soft/solr-4.10.3/
 2 [root@localhost solr-4.10.3]# ls
 3 bin  CHANGES.txt  contrib  dist  docs  example  licenses  LICENSE.txt  LUCENE_CHANGES.txt  NOTICE.txt  README.txt  SYSTEM_REQUIREMENTS.txt
 4 [root@localhost solr-4.10.3]# cd example/solr
 5 [root@localhost solr]# ls
 6 bin  collection1  README.txt  solr.xml  zoo.cfg
 7 [root@localhost solr]# cd collection1/
 8 [root@localhost collection1]# ls
 9 conf  core.properties  data  README.txt
10 [root@localhost collection1]# cd conf/
11 [root@localhost conf]# ls
12 admin-extra.html              clustering    lang                         protwords.txt                            _schema_analysis_synonyms_english.json  solrconfig.xml  synonyms.txt      xslt
13 admin-extra.menu-bottom.html  currency.xml  mapping-FoldToASCII.txt      _rest_managed.json                       schema.xml                              spellings.txt   update-script.js
14 admin-extra.menu-top.html     elevate.xml   mapping-ISOLatin1Accent.txt  _schema_analysis_stopwords_english.json  scripts.conf                            stopwords.txt   velocity
15 [root@localhost conf]# 

然后添加IK中文分词器,自定义业务域:

其中IK中文分词器,自定义业务域具体内容如下所示:

将这些添加完毕以后,重启Tomcat,然后看看,可以搜索到新增的业务域字段。

代码语言:javascript
复制
 1 <!-- 然后添加如下配置即可:-->
 2 <fieldType name="text_ik" class="solr.TextField">
 3 <!-- 索引时候的分词器 -->
 4 <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"></analyzer>
 5 <!-- 查询时候的分词器 -->
 6 <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"></analyzer>
 7 </fieldType>
 8 
 9 <!--IKAnalyzer Field-->
10 <!-- type="text_ik"代表使用了Ik中文分词器。 -->
11 <!-- indexed="true"代表进行索引操作。 -->
12 <!-- stored="true"代表将该字段内容进行存储。 -->
13 <field name="product_name" type="text_ik" indexed="true" stored="true" />
14 <field name="product_price" type="long" indexed="true" stored="true" />
15 <field name="product_picture" type="string" indexed="false" stored="true" />
16 <field name="product_description" type="text_ik" indexed="true" stored="true" />
17 <field name="product_catalog_name" type="string" indexed="true" stored="false" />

可以看到搜索到新增的业务域字段。

2、然后你可以愉快的编程了,嘻嘻。

代码语言:javascript
复制
  1 package com.taotao.search.service;
  2 
  3 import java.io.IOException;
  4 import java.util.List;
  5 import java.util.Map;
  6 
  7 import org.apache.solr.client.solrj.SolrQuery;
  8 import org.apache.solr.client.solrj.SolrServer;
  9 import org.apache.solr.client.solrj.SolrServerException;
 10 import org.apache.solr.client.solrj.impl.HttpSolrServer;
 11 import org.apache.solr.client.solrj.response.QueryResponse;
 12 import org.apache.solr.client.solrj.response.UpdateResponse;
 13 import org.apache.solr.common.SolrDocument;
 14 import org.apache.solr.common.SolrDocumentList;
 15 import org.apache.solr.common.SolrInputDocument;
 16 import org.junit.Before;
 17 import org.junit.Test;
 18 
 19 /**
 20  * 使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。
 21  * 创建索引,使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。
 22  * 
 23  * @ClassName: ProductSolrUtils.java
 24  * @author: biehl
 25  * @since: 2019年9月12日 上午10:49:13
 26  * @Copyright: ©2019 biehl 版权所有
 27  * @version: 0.0.1
 28  * @Description:
 29  */
 30 public class ProductSolrUtils {
 31 
 32     // solr的地址路径
 33     private String solrServerUrl = "http://192.168.110.142:8080/solr-4.10.3/collection1";
 34     private SolrServer solrServer = null;
 35 
 36     /**
 37      * 
 38      */
 39     @Before
 40     public void before() {
 41         // 初始化执行
 42         // 1、创建SolrServer对象。创建一个HttpSolrServer对象
 43         solrServer = new HttpSolrServer(this.solrServerUrl);
 44     }
 45 
 46     /**
 47      * 说明:根据id(唯一约束)域来更新Document的内容,如果根据id值搜索不到id域则会执行添加操作,如果找到则更新。
 48      * 
 49      * @throws IOException
 50      * @throws SolrServerException
 51      * 
 52      */
 53     @Test
 54     public void productSolrCreateIndex() {
 55         try {
 56             // 2、需要指定Solr服务的url
 57             // 3、创建一个文档对象SolrInputDocument
 58             SolrInputDocument document = new SolrInputDocument();
 59             // 4、向文档中添加域,必须写id域,域的名称必须在schema.xml中定义
 60             document.addField("id", "p0001");
 61             document.addField("product_name", "小米手机9x");
 62             document.addField("product_price", 8888);
 63             document.addField("product_picture", "好用得咧");
 64             document.addField("product_description", "什么玩意?");
 65             document.addField("product_catalog_name", "手机");
 66 
 67             // 5、把文档对象写入到索引库中
 68             // 向solr里面添加文档
 69             UpdateResponse response = solrServer.add(document);
 70             // 6、提交
 71             solrServer.commit();
 72         } catch (Exception e) {
 73             e.printStackTrace();
 74         }
 75     }
 76 
 77     /**
 78      * 删除索引
 79      * 
 80      * 说明:deleteById(String id)根据id删除索引,此方法为重载方法,也可以传个多个id批量删除, 也可以调用deleteByQuery()
 81      * 根据查询条件删除
 82      */
 83     @Test
 84     public void taotaoSolrJDeleteById() {
 85         try {
 86             // 向solr里面添加文档
 87             // 1、创建SolrServer对象。创建一个HttpSolrServer对象
 88             // SolrServer server = new
 89             // HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1");
 90 
 91             // 2、 删除操作,//根据id删除
 92             solrServer.deleteById("p0001");
 93 
 94             // 3、提交
 95             solrServer.commit();
 96         } catch (SolrServerException e) {
 97             e.printStackTrace();
 98         } catch (IOException e) {
 99             e.printStackTrace();
100         }
101 
102     }
103 
104     /**
105      * 删除索引,查询条件删除
106      * 
107      */
108     @Test
109     public void taotaoSolrJDeleteByQuery() {
110         try {
111             // 向solr里面添加文档
112             // 1、创建SolrServer对象。创建一个HttpSolrServer对象
113             // SolrServer server = new
114             // HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1");
115 
116             // 2、 删除操作
117             solrServer.deleteByQuery("id:p0002");
118 
119             // 3、提交
120             solrServer.commit();
121         } catch (SolrServerException e) {
122             e.printStackTrace();
123         } catch (IOException e) {
124             e.printStackTrace();
125         }
126     }
127 
128     /**
129      * 
130      */
131     @Test
132     public void searchDocument() {
133         try {
134             // 1、创建一个SolrServer对象
135             // SolrServer solrServer = new
136             // HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1");
137             // 2、创建一个SolrQuery对象
138             SolrQuery solrQuery = new SolrQuery();
139             // 3、设置查询条件,过滤条件,分页条件,排序条件,高亮
140             // key的q就是指查询条件。
141             // solrQuery.set("q", "*:*"); //等价于solrQuery.setQuery("*:*");
142             // 查询所有的不能指定高亮的。
143             // solrQuery.setQuery("*:*");// *:*是查询出所有的。
144             // 这里没有指定在那里域上面进行搜索,所以需要指定默认搜索域
145             solrQuery.setQuery("小米手机9");
146             // 分页默认是0-10。分页条件。
147             solrQuery.setStart(0);// 起始数
148             solrQuery.setRows(20);// 查询出多少条
149             // 设置默认搜索域。就是如果Query不设置查询那个字段,这里必须指定一个默认值,进行搜索。
150             solrQuery.set("df", "product_name");
151             // 设置高亮。
152             solrQuery.setHighlight(true);// 开启高亮
153             // 设置高亮显示的域
154             solrQuery.addHighlightField("product_catalog_name");
155             // 设置高亮显示的前缀和后缀
156             solrQuery.setHighlightSimplePre("<em>");
157             solrQuery.setHighlightSimplePost("</em>");
158 
159             // 4、执行查询,得到一个Response对象
160             QueryResponse response = solrServer.query(solrQuery);
161 
162             // 5、取出查询结果总记录数
163             SolrDocumentList solrDocumentList = response.getResults();
164             // 查询出结果总记录数
165             System.out.println("查询结果总记录数: " + solrDocumentList.getNumFound());
166 
167             for (SolrDocument solrDocument : solrDocumentList) {
168                 System.out.println("id : " + solrDocument.get("id"));
169                 // 取出高亮显示
170                 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
171                 List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");
172                 String product_name = "";
173                 if (list != null && list.size() > 0) {
174                     product_name = list.get(0);
175                 } else {
176                     product_name = (String) solrDocument.get("product_name");
177                 }
178                 System.out.println(product_name);
179                 System.out.println("product_price : " + solrDocument.get("product_price"));
180                 System.out.println("product_picture : " + solrDocument.get("product_picture"));
181                 System.out.println("product_description : " + solrDocument.get("product_description"));
182                 System.out.println("product_catalog_name : " + solrDocument.get("product_catalog_name"));
183                 System.out.println("=============================================");
184             }
185 
186             // 提交
187             solrServer.commit();
188         } catch (SolrServerException e) {
189             e.printStackTrace();
190         } catch (IOException e) {
191             e.printStackTrace();
192         }
193 
194     }
195 
196 }

查询删除效果如下所示:

待续......

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档