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内容保存在索引中。这对于检索和醒目显示内容很有用,但对于实际搜索则不是必须的,例如,很多应用程序存储指向内容位置的指针而不是存储实际的文件内容。
[root@localhost tomcat]# cd /home/hadoop/soft/solr-4.10./
[root@localhost solr-4.10.]# ls
bin CHANGES.txt contrib dist docs example licenses LICENSE.txt LUCENE_CHANGES.txt NOTICE.txt README.txt SYSTEM_REQUIREMENTS.txt
[root@localhost solr-4.10.]# cd example/solr
[root@localhost solr]# ls
bin collection1 README.txt solr.xml zoo.cfg
[root@localhost solr]# cd collection1/
[root@localhost collection1]# ls
conf core.properties data README.txt
[root@localhost collection1]# cd conf/
[root@localhost conf]# ls
admin-extra.html clustering lang protwords.txt _schema_analysis_synonyms_english.json solrconfig.xml synonyms.txt xslt
admin-extra.menu-bottom.html currency.xml mapping-FoldToASCII.txt _rest_managed.json schema.xml spellings.txt update-script.js
admin-extra.menu-top.html elevate.xml mapping-ISOLatin1Accent.txt _schema_analysis_stopwords_english.json scripts.conf stopwords.txt velocity
[root@localhost conf]#
然后添加IK中文分词器,自定义业务域:
其中IK中文分词器,自定义业务域具体内容如下所示:
将这些添加完毕以后,重启Tomcat,然后看看,可以搜索到新增的业务域字段。
<!-- 然后添加如下配置即可:-->
<fieldType name="text_ik" class="solr.TextField">
<!-- 索引时候的分词器 -->
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"></analyzer>
<!-- 查询时候的分词器 -->
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"></analyzer>
</fieldType> <!--IKAnalyzer Field-->
<!-- type="text_ik"代表使用了Ik中文分词器。 -->
<!-- indexed="true"代表进行索引操作。 -->
<!-- stored="true"代表将该字段内容进行存储。 -->
<field name="product_name" type="text_ik" indexed="true" stored="true" />
<field name="product_price" type="long" indexed="true" stored="true" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_description" type="text_ik" indexed="true" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="false" />
可以看到搜索到新增的业务域字段。
2、然后你可以愉快的编程了,嘻嘻。
package com.taotao.search.service; import java.io.IOException;
import java.util.List;
import java.util.Map; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test; /**
* 使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。
* 创建索引,使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。
*
* @ClassName: ProductSolrUtils.java
* @author: biehl
* @since: 2019年9月12日 上午10:49:13
* @Copyright: ©2019 biehl 版权所有
* @version: 0.0.1
* @Description:
*/
public class ProductSolrUtils { // solr的地址路径
private String solrServerUrl = "http://192.168.110.142:8080/solr-4.10.3/collection1";
private SolrServer solrServer = null; /**
*
*/
@Before
public void before() {
// 初始化执行
// 1、创建SolrServer对象。创建一个HttpSolrServer对象
solrServer = new HttpSolrServer(this.solrServerUrl);
} /**
* 说明:根据id(唯一约束)域来更新Document的内容,如果根据id值搜索不到id域则会执行添加操作,如果找到则更新。
*
* @throws IOException
* @throws SolrServerException
*
*/
@Test
public void productSolrCreateIndex() {
try {
// 2、需要指定Solr服务的url
// 3、创建一个文档对象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
// 4、向文档中添加域,必须写id域,域的名称必须在schema.xml中定义
document.addField("id", "p0001");
document.addField("product_name", "小米手机9x");
document.addField("product_price", );
document.addField("product_picture", "好用得咧");
document.addField("product_description", "什么玩意?");
document.addField("product_catalog_name", "手机"); // 5、把文档对象写入到索引库中
// 向solr里面添加文档
UpdateResponse response = solrServer.add(document);
// 6、提交
solrServer.commit();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 删除索引
*
* 说明:deleteById(String id)根据id删除索引,此方法为重载方法,也可以传个多个id批量删除, 也可以调用deleteByQuery()
* 根据查询条件删除
*/
@Test
public void taotaoSolrJDeleteById() {
try {
// 向solr里面添加文档
// 1、创建SolrServer对象。创建一个HttpSolrServer对象
// SolrServer server = new
// HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1"); // 2、 删除操作,//根据id删除
solrServer.deleteById("p0001"); // 3、提交
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } /**
* 删除索引,查询条件删除
*
*/
@Test
public void taotaoSolrJDeleteByQuery() {
try {
// 向solr里面添加文档
// 1、创建SolrServer对象。创建一个HttpSolrServer对象
// SolrServer server = new
// HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1"); // 2、 删除操作
solrServer.deleteByQuery("id:p0002"); // 3、提交
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
*
*/
@Test
public void searchDocument() {
try {
// 1、创建一个SolrServer对象
// SolrServer solrServer = new
// HttpSolrServer("http://192.168.110.142:8080/solr-4.10.3/collection1");
// 2、创建一个SolrQuery对象
SolrQuery solrQuery = new SolrQuery();
// 3、设置查询条件,过滤条件,分页条件,排序条件,高亮
// key的q就是指查询条件。
// solrQuery.set("q", "*:*"); //等价于solrQuery.setQuery("*:*");
// 查询所有的不能指定高亮的。
// solrQuery.setQuery("*:*");// *:*是查询出所有的。
// 这里没有指定在那里域上面进行搜索,所以需要指定默认搜索域
solrQuery.setQuery("小米手机9");
// 分页默认是0-10。分页条件。
solrQuery.setStart();// 起始数
solrQuery.setRows();// 查询出多少条
// 设置默认搜索域。就是如果Query不设置查询那个字段,这里必须指定一个默认值,进行搜索。
solrQuery.set("df", "product_name");
// 设置高亮。
solrQuery.setHighlight(true);// 开启高亮
// 设置高亮显示的域
solrQuery.addHighlightField("product_catalog_name");
// 设置高亮显示的前缀和后缀
solrQuery.setHighlightSimplePre("<em>");
solrQuery.setHighlightSimplePost("</em>"); // 4、执行查询,得到一个Response对象
QueryResponse response = solrServer.query(solrQuery); // 5、取出查询结果总记录数
SolrDocumentList solrDocumentList = response.getResults();
// 查询出结果总记录数
System.out.println("查询结果总记录数: " + solrDocumentList.getNumFound()); for (SolrDocument solrDocument : solrDocumentList) {
System.out.println("id : " + solrDocument.get("id"));
// 取出高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");
String product_name = "";
if (list != null && list.size() > ) {
product_name = list.get();
} else {
product_name = (String) solrDocument.get("product_name");
}
System.out.println(product_name);
System.out.println("product_price : " + solrDocument.get("product_price"));
System.out.println("product_picture : " + solrDocument.get("product_picture"));
System.out.println("product_description : " + solrDocument.get("product_description"));
System.out.println("product_catalog_name : " + solrDocument.get("product_catalog_name"));
System.out.println("=============================================");
} // 提交
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } }
查询删除效果如下所示: