solrj

1.什么是solrj?

SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr 索引的JAVA接口。SolrJ针对Solr,提供了Rest的 HTTP接口进行了封装,SolrJ底层是通过使用HttpClient中的方法来完成Solr的操作。

2.搭建工程

2.1.导入相关jar包

非maven工程需要将solr安装包目录\dist\solrj-lib\内的所有jar导入

<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>8.7.0</version>
</dependency>
<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.2</version>
</dependency>

3.对索引库做增删改查

3.1.添加(以实体类的方式)

solrj支持对数据表做实体映射连接,通过提供的@field注解,来与实体类做隐式映射

注解作用
@Field用来对应实体类的属性做域映射

data-config.xml文件里面的实体映射
在这里插入图片描述

package test_solrj;

import org.apache.solr.client.solrj.beans.Field;

public class Commodity {

	@Field("id")
	private String id;
	@Field("prod_title")
	private String title;
	@Field("prod_origin_price")
	private Double original_price;
	@Field("prod_price")
	private Double price;
	@Field("prod_category_name")
	private String category_name;
	@Field("prod_pic_url")
	private String main_pic_url;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Double getOriginal_price() {
		return original_price;
	}

	public void setOriginal_price(Double original_price) {
		this.original_price = original_price;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getCategory_name() {
		return category_name;
	}

	public void setCategory_name(String category_name) {
		this.category_name = category_name;
	}

	public String getMain_pic_url() {
		return main_pic_url;
	}

	public void setMain_pic_url(String main_pic_url) {
		this.main_pic_url = main_pic_url;
	}

	@Override
	public String toString() {
		return "Commodity [id=" + id + ", title=" + title + ", original_price=" + original_price + ", price=" + price
				+ ", category_name=" + category_name + ", main_pic_url=" + main_pic_url + "]";
	}

	public Commodity() {
		super();
	}

}

添加方法

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		// 2.创建一个文档对象
		SolrInputDocument inputDocument = new SolrInputDocument();
		// 向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)
		inputDocument.addField("id", "6");
		inputDocument.addField("prod_title", "测试表标题");
		inputDocument.addField("prod_category_name", "测试分类");
		inputDocument.addField("prod_origin_price", 99.9);
		inputDocument.addField("prod_price", 87.0);
		inputDocument.addField("prod_pic_url", "test");

		// 3.添加
		UpdateResponse response = client.add(inputDocument);
		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("添加完成");
	}

}

在这里插入图片描述

3.3.修改(update)

solrj提供的修改其实是根据索引库的id来操作的,同样是用添加的方式
添加的时候会检查提供的唯一id,如果当前id不存在则直接添加,如果id存在,这会做覆盖的操作
因为索引库的id是具有唯一性的

我们修改一下上面的插入语句,id不变,再次执行语句,没有新插入,标题修改了

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		// 2.创建一个文档对象
		SolrInputDocument inputDocument = new SolrInputDocument();
		// 向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)
		inputDocument.addField("id", "6");
		inputDocument.addField("prod_title", "修改了");
		inputDocument.addField("prod_category_name", "测试分类");
		inputDocument.addField("prod_origin_price", 99.9);
		inputDocument.addField("prod_price", 87.0);
		inputDocument.addField("prod_pic_url", "test");

		// 3.添加
		UpdateResponse response = client.add(inputDocument);
		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("修改完成");
	}

}

在这里插入图片描述

3.4.删除

删除有很多中方法
常用的通过id删除,
通过查询语句删除

以ID删除

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		// 以id删除索引记录
		UpdateResponse response = client.deleteById("5");

		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("完成");
	}

}

批量删除(以ID)

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		List<String> list = new ArrayList<String>();
		list.add("111");
		list.add("6");

		// 以id删除索引记录
		UpdateResponse response = client.deleteById(list);

		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("完成");
	}

}

以条件删除(query)

solrj支持以查询的方式删除符合条件的索引记录

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		List<String> list = new ArrayList<String>();
		list.add("111");
		list.add("6");

		// 查询匹配分类类目“毛衣”
		UpdateResponse response = client.deleteByQuery("prod_category_name:毛衣");

		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("完成");
	}

}

删除所有

public class Test01 {

	final static String baseUrl = "http://localhost:8081/solr/core_01";

	@Test
	public void addIndex() throws IOException, SolrServerException {

		// 1.建立连接
		HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();

		List<String> list = new ArrayList<String>();
		list.add("111");
		list.add("6");

		// 删除所有
		UpdateResponse response = client.deleteByQuery("*:*");

		// 4.提交,关闭连接
		client.commit();
		client.close();

		System.out.println("完成");
	}

}

3.5.查询

查询单个

@Test
public void testQuery() throws Exception{
	//1.创建连接
	HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();
	//2.创建查询语句
	SolrQuery query = new SolrQuery();
	//3.设置查询条件
	query.set("q", "id:5");
	//4.执行查询
	QueryResponse queryResponse = solrServer.query(query);
	//5.取文档列表(public class SolrDocumentList extends ArrayList<SolrDocument>)
	SolrDocumentList documentList = queryResponse.getResults();
	for (SolrDocument solrDocument : documentList) {
		System.out.println("id:"+solrDocument.get("id")+" ");
		System.out.println("标题:"+solrDocument.get("product_title")+" ");
		System.out.println("卖点:"+solrDocument.get("product_sell_point")+" ");
	}
}

条件查询

@Test
public void testQueryByCon() throws Exception{
	//1.创建连接
	HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();
	//2.创建查询语句
	SolrQuery query = new SolrQuery();
	//3.设置查询条件
	query.set("q", "*款");//设置查询关键字
	query.setSort("id", SolrQuery.ORDER.desc);//按照id降序排列
	query.setStart(0);
	query.setRows(5);//分页条件
	query.set("df", "product_sell_point");//默认在商品卖点域进行查询
	//4、执行查询
	QueryResponse queryResponse = solrServer.query(query);
	//5.获取文档列表
	SolrDocumentList documentList = queryResponse.getResults();
	System.out.println("总记录数:" + documentList.getNumFound());
	for (SolrDocument solrDocument : documentList) {
		System.out.println("id:"+solrDocument.get("id")+" ");
		System.out.println("标题:"+solrDocument.get("product_title")+" ");
		System.out.println("卖点:"+solrDocument.get("product_sell_point")+" ");
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值