package com.employment.common.utils;
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.client.solrj.response.UpdateResponse;
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.apache.solr.common.params.SolrParams;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SolrClientUtils {
private final static String url = "http://localhost:8080/solr";
/**
* 获取SolrClient
* solrj 6.5及以后版本获取方式
* @return
*/
public static HttpSolrClient getSolrClient(){
/*
* 设置超时时间
* .withConnectionTimeout(10000)
* .withSocketTimeout(60000)
*/
return new HttpSolrClient.Builder(url)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
}
/**
*
* @param core solr中core名字
* @param query 条件 字段名:搜索内容 AND 段名:搜索内容 多个字段用AND连接
* @return
* @throws IOException
* @throws SolrServerException
*/
public static SolrDocumentList testQuery(String core, String query) throws IOException, SolrServerException {
HttpSolrClient solrClient = getSolrClient();
// 定义查询条件
Map<String, String> params = new HashMap<String, String>();
params.put("q", query);
SolrParams mapSolrParams = new MapSolrParams(params);
//执行查询 第一个参数是collection,就是我们在solr中创建的core
QueryResponse response = solrClient.query(core, mapSolrParams);
// 获取结果集
SolrDocumentList results = response.getResults();
for (SolrDocument result : results) {
// SolrDocument 数据结构为Map
System.out.println(result);
}
return results;
}
/**
* 根据id删除数据
* @param core solr中core名字
* @param id 数据id
* @throws IOException
* @throws SolrServerException
*/
public static void deleteById(String core, String id) throws IOException, SolrServerException {
HttpSolrClient solrClient = getSolrClient();
UpdateResponse updateResponse = solrClient.deleteById(core, id);
solrClient.commit(core);
}
/**
* 单个数据 实体类添加
* @param core solr中core名字
* @param object 实体类对象
* @throws IOException
* @throws SolrServerException
*/
public static void addObject(String core, Object object) throws IOException, SolrServerException {
HttpSolrClient solrClient = getSolrClient();
UpdateResponse updateResponse = solrClient.addBean(core,object);
solrClient.commit(core);
}
/**
* 单个数据 map类型添加
* @param core solr中core名字
* @param map 要添加的map对象
* @throws IOException
* @throws SolrServerException
*/
public static void addMap(String core, Map<String, Object> map) throws IOException, SolrServerException {
HttpSolrClient solrClient = getSolrClient();
SolrInputDocument solrInputFields = new SolrInputDocument();
for (Map.Entry<String, Object> entry : map.entrySet()){
solrInputFields.addField(entry.getKey(),entry.getValue());
}
UpdateResponse add = solrClient.add(core, solrInputFields);
solrClient.commit(core);
}
/**
* 根据id批量删除
* @param core solr中core名字
* @param ids id集合
* @throws IOException
* @throws SolrServerException
*/
public static void deleteList(String core, List<String> ids) throws IOException, SolrServerException {
HttpSolrClient solrClient = getSolrClient();
UpdateResponse updateResponse = solrClient.deleteById(core, ids);
solrClient.commit(core);
System.out.println(updateResponse);
}
}
pom文件
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.10.1</version>
</dependency>