solrJ的使用(简单使用+整合spring)

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;
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solr基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎 课程特点毕业后接触的第一个中间件就是Solr,在工作中用处广泛,为了便于大家快速掌握该技能,开始录制相关课程,该专栏特点如下:1.采用Solr最新版本视频录制,全网最新课程(Solr8.1于2019年5月16日发布)2.技能点全网最全,会结合工作经验,项目中用到的技能点都会有所涉及,更新章节比较全面3.适用范围广,从零基础到高级架构以及分布式集群都涵盖,适用初级、高级、项目实战等多个层次开发者4.多种维度辅助学习,采用独立solr粉丝群辅助教学,学员问题会及时得到解决,程序员突破圈 打卡制度,督促学员学习关注后再购买、 关注后再购买、 关注后再购买课程能得到什么1.快速学习到最新版本的全文检索技术,从视频、文章、圈子、粉丝交流等快速促进学习2.通过该技术,获得面试进阶指导3.结交人脉(庞大的粉丝群)..End初期学员100人,价格不会太高,也是为了帮助更多的开发者但是个人精力有限,所以限制条件如下1.求知欲强,有想向技术更深一层了解的2.乐于交流,喜欢探讨技术者3.学习惰性者慎入,购买后会督促大家学习,购买不是目的,学习到该技能才是该专栏的主要目的正式进入学习状态了吗,专栏群见。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值