solrJ是java的客户端,使用它可以对solr的索引库进行增删改查等操作
下面我们就来看看它的用法
Solr简单使用
在使用之前我们需要把solrJ的jar包导到工程里去
一、我们先说向索引库中添加一个文档
1.使用一个HttpSolrServer创建一个solrServer对象
2.创建一个SolrInputDocument
3.向文档中添加域,注意:必须有id域,并且域名在schema.xml中必须有
4.把文档添加到索引库
5.提交
public void testAddDocument() throws Exception {
//创建一个SolrServer对象。创建一个HttpSolrServer对象
//需要指定solr服务的url
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
//创建一个文档对象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域,必须有id域,域的名称必须在schema.xml中定义
document.addField("id", "123");
document.addField("item_title", "测试商品3");
document.addField("item_price", 1000);
//把文档对象写入索引库
solrServer.add(document);
//提交
solrServer.commit();
}
二、删除索引库中的文档
根据ID删除
1.创建一个SolrServer对象
2.调用SolrServer对象的deleteById方法根据id删除
3.提交
public void deleteDocumentById() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
solrServer.deleteById("123");
//提交
solrServer.commit();
}
根据查询删除
即删除到查询出的所有数据
1.创建一个SolrServer对象
2.调用SolrServer对象的deleteByQuery方法并传入查询条件
3.提交
public void deleteDocumentByQuery() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
solrServer.deleteByQuery("item_title:测试3");
solrServer.commit();
}
三、查询索引库
1.创建一个SolrServer对象
2.创建一个SolrQuery对象
3.添加查询条件
4.执行查询返回一个response对象
5.用response对象的getResults取值SolrDocumentList(查询结果)
6.遍历查询结果
public void searchDocumet() throws Exception {
//创建一个SolrServer对象
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
//创建一个SolrQuery对象
SolrQuery query = new SolrQuery();
//设置查询条件、过滤条件、分页条件、排序条件、高亮
//query.set("q", "*:*");
query.setQuery("手机");
//分页条件
query.setStart(0);
query.setRows(10);
//设置默认搜索域
query.set("df", "item_keywords");
//设置高亮
query.setHighlight(true);
//高亮显示的域
query.addHighlightField("item_title");
query.setHighlightSimplePre("<div>");
query.setHighlightSimplePost("</div>");
//执行查询,得到一个Response对象
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
//取查询结果总记录数
System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String itemTitle = "";
if (list != null && list.size() >0) {
itemTitle = list.get(0);
} else {
itemTitle = (String) solrDocument.get("item_title");
}
System.out.println(itemTitle);
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_category_name"));
System.out.println("=============================================");
}
}
Spring整合solr
配置文件
applicationContext-solr.xml
这里需要配置两个bean,HttpSolrServer(单机版使用)和CloudSolrServer(集群版使用)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 单机版solr的连接 -->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr/collection1"/>
</bean>
<!-- 集群版solr连接 -->
<!-- <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg>
<property name="defaultCollection" value="collection2"/>
</bean> -->
</beans>
web.xml
web.xml需要配置加载applicationContext-solr.xml,也就是需要配置一个spring容器
contextConfigLocation用来指定加载配置文件的位置
ContextLoaderListener用来创建applicationContext-solr.xml中的bean,并放到容器中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>taotao-search</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
java使用
在代码中我们直接注入一个SolrServer就可以了,因为SolrServer是一个抽象类,HttpSolrServer和CloudSolrServer是其实现类
public class SearchDao {
@Autowired
private SolrServer solrServer;
public SearchResult search(SolrQuery query) throws Exception{
//根据query对象进行查询
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
//取查询结果总记录数
long numFound = solrDocumentList.getNumFound();
SearchResult result = new SearchResult();
result.setRecordCount(numFound);
List<SearchItem> itemList = new ArrayList<>();
//把查询结果封装到SearchItem对象中
for (SolrDocument solrDocument : solrDocumentList) {
SearchItem item = new SearchItem();
item.setCategory_name((String) solrDocument.get("item_category_name"));
item.setId((String) solrDocument.get("id"));
//取一张图片
String image = (String) solrDocument.get("item_image");
if (StringUtils.isNotBlank(image)) {
image = image.split(",")[0];
}
item.setImage(image);
item.setPrice((long) solrDocument.get("item_price"));
item.setSell_point((String) solrDocument.get("item_sell_point"));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String title = "";
if (list != null && list.size() > 0) {
title = list.get(0);
} else {
title = (String) solrDocument.get("item_title");
}
item.setTitle(title);
//添加到商品列表
itemList.add(item);
}
//把结果添加到SearchResult中
result.setItemList(itemList);
//返回
return result;
}
}