【SolrJ8.2.0】使用SolrJ8.2.0最新版连接solr服务实现新增/查询/删除

前言

在Solr使用过程中,对Solr版本进行了升级,升级后发现API也变更了,所以这儿通过查阅相关的API情况,整理这篇文章!

 

使用SolrJ8.2.0最新版连接solr服务实现新增/查询/删除

1、使用solrj需要导入依赖包:

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

 

2、新增文档:

/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title addDocument
	 *        <ul>
	 * @description 添加文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void addDocument()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		// 创建SolrInputDocument文档流对象
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", "1001");
		doc.addField("title", "测试数据");
		doc.addField("node", "1919");
		doc.addField("desc", "这是一条测试数据");

		try
		{
			// 添加文档
			solrClient.add(doc);
			// 提交文档
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

 

2、查询文档

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title queryDocumentById
	 *        <ul>
	 * @description 根据文档ID查询文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void queryDocumentById()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		// 设置查询参数
		// 第一种查询方式():MapSolrParams来实现
		HashMap<String, String> map = new HashMap<>();
		// 设置查询参数
		map.put("q", "id:1001");
		// 创建MapSolrParams查询对象
		MapSolrParams solrParams = new MapSolrParams(map);

		// 第二种查询方式:SolrParams 有一个 SolrQuery 子类来实现
		SolrQuery solrQuery = new SolrQuery("id:1001");
		// 指定需要回显的内容
		solrQuery.addField("id");
		solrQuery.addField("title");
		solrQuery.addField("node");

		try
		{
			// 根据参数查询指定文档
			QueryResponse query = solrClient.query(solrParams);
			// 提交查询请求
			solrClient.commit();
			// 输出查询内容
			SolrDocumentList list = query.getResults();
			for (SolrDocument document : list)
			{
				System.err.println("问题ID:" + document.get("id"));
				System.err.println("问题标题:" + document.get("title"));
				System.err.println("副标题:" + document.get("node"));
			}
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

 

3-1、删除文档

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title deleteDocumentById
	 *        <ul>
	 * @description 根据文档ID删除文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @throws SolrServerException
	 * @throws IOException
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void deleteDocumentById()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		try
		{
			// 删除指定ID的文档
			solrClient.deleteById("1001");
			// 提交请求
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

 

3-2、查询删除

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title deleteDocumentByQuery
	 *        <ul>
	 * @description 查询删除
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void deleteDocumentByQuery()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		try
		{
			// 删除指定ID的文档
			solrClient.deleteByQuery("id:1001");
			// 提交请求
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

 

4、查询文档

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title queryDocumentById
	 *        <ul>
	 * @description 根据文档ID查询文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void queryDocumentById()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		// 设置查询参数
		// 第一种查询方式():MapSolrParams来实现
		HashMap<String, String> map = new HashMap<>();
		// 设置查询参数
		map.put("q", "id:1001");
		// 创建MapSolrParams查询对象
		MapSolrParams solrParams = new MapSolrParams(map);

		// 第二种查询方式:SolrParams 有一个 SolrQuery 子类来实现
		SolrQuery solrQuery = new SolrQuery("id:1001");
		// 指定需要回显的内容
		solrQuery.addField("id");
		solrQuery.addField("title");
		solrQuery.addField("node");

		try
		{
			// 根据参数查询指定文档
			QueryResponse query = solrClient.query(solrParams);
			// 提交查询请求
			solrClient.commit();
			// 输出查询内容
			SolrDocumentList list = query.getResults();
			for (SolrDocument document : list)
			{
				System.err.println("问题ID:" + document.get("id"));
				System.err.println("问题标题:" + document.get("title"));
				System.err.println("副标题:" + document.get("node"));
			}
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

 

5、完整示例代码:

package com.huazai.b2c.aiyou.test.solrj;

import java.io.IOException;
import java.util.HashMap;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.junit.Test;

/**
 * 
 * @author HuaZai
 * @contact who.seek.me@java98k.vip
 *          <ul>
 * @description 测试solrj客户端
 *              </ul>
 * @className TSolrjClient
 * @package com.huazai.b2c.aiyou.test.solrj
 * @createdTime 2017年06月14日
 *
 * @version V1.0.0
 */
public class TSolrjClient
{
	public static final String SOLR_URL = "http://***.***.***.***:****/solr/core_name";

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title addDocument
	 *        <ul>
	 * @description 添加文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void addDocument()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		// 创建SolrInputDocument文档流对象
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", "1001");
		doc.addField("title", "测试数据");
		doc.addField("node", "1919");
		doc.addField("desc", "这是一条测试数据");

		try
		{
			// 添加文档
			solrClient.add(doc);
			// 提交文档
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title deleteDocumentById
	 *        <ul>
	 * @description 根据文档ID删除文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @throws SolrServerException
	 * @throws IOException
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void deleteDocumentById()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		try
		{
			// 删除指定ID的文档
			solrClient.deleteById("1001");
			// 提交请求
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title deleteDocumentByQuery
	 *        <ul>
	 * @description 查询删除
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void deleteDocumentByQuery()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		try
		{
			// 删除指定ID的文档
			solrClient.deleteByQuery("id:1001");
			// 提交请求
			solrClient.commit();
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author HuaZai
	 * @contact who.seek.me@java98k.vip
	 * @title queryDocumentById
	 *        <ul>
	 * @description 根据文档ID查询文档
	 *              </ul>
	 * @createdTime 2017年06月15日
	 * @return void
	 *
	 * @version : V1.0.0
	 */
	@Test
	public void queryDocumentById()
	{
		// 通过HttpSolrClient.Builder一个SolrClient对象
		SolrClient solrClient = new HttpSolrClient.Builder()
				.withBaseSolrUrl(SOLR_URL)
				.withConnectionTimeout(10000)
				.withSocketTimeout(60000)
				.build();

		// 设置查询参数
		// 第一种查询方式():MapSolrParams来实现
		HashMap<String, String> map = new HashMap<>();
		// 设置查询参数
		map.put("q", "id:1001");
		// 创建MapSolrParams查询对象
		MapSolrParams solrParams = new MapSolrParams(map);

		// 第二种查询方式:SolrParams 有一个 SolrQuery 子类来实现
		SolrQuery solrQuery = new SolrQuery("id:1001");
		// 指定需要回显的内容
		solrQuery.addField("id");
		solrQuery.addField("title");
		solrQuery.addField("node");

		try
		{
			// 根据参数查询指定文档
			QueryResponse query = solrClient.query(solrParams);
			// 提交查询请求
			solrClient.commit();
			// 输出查询内容
			SolrDocumentList list = query.getResults();
			for (SolrDocument document : list)
			{
				System.err.println("问题ID:" + document.get("id"));
				System.err.println("问题标题:" + document.get("title"));
				System.err.println("副标题:" + document.get("node"));
			}
		} catch (SolrServerException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			// 关闭连接
			try
			{
				solrClient.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

}

 

 

 

 

 


 好了,关于 【SolrJ8.2.0】使用SolrJ8.2.0最新版连接solr服务实现新增/查询/删除 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者:华    仔
联系作者:who.seek.me@java98k.vip
来        源:优快云 (Chinese Software Developer Network)
原        文:https://blog.youkuaiyun.com/Hello_World_QWP/article/details/98331238
版权声明:本文为博主原创文章,请在转载时务必注明博文出处!
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechBro华仔

日拱一卒无有尽,功不唐捐终入海

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值