Solr-Solrj简单使用

一、SolrJ简介

SolrJ是操作Solr的Java客户端,它提供了增加、修改、删除、查询Solr索引的Java接口。通过solrJ提供的API接口来操作solr服务,SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。

二、依赖配置

<dependency>
	<groupId>org.apache.solr</groupId>
	<artifactId>solr-solrj</artifactId>
	<version>7.7.1</version>
</dependency>

三、简单实例
接这篇文章的结果Solr-导入MySQL数据
当前core中已存在的信息如图:
在这里插入图片描述

1、添加文档

@Test
public void testAdd() throws Exception{
   
	//1.创建连接
	HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();
	//2.创建一个文档对象
	SolrInputDocument inputDocument = new SolrInputDocument();
	//向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)
	inputDocument.addField("id", "5");
	inputDocument.addField("product_title", "裙子");
	inputDocument.addField("product_sell_point", "爆款");
	//3.将文档写入索引库中
	solrServer.add(inputDocument);
	//4.提交
	solrServer.commit();
}

添加成功后,页面可直接查看如图:
在这里插入图片描述

2、更新文档(其实更新的内容不存在则是新增)

@Test
public void testUpdate() throws Exception
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值