简介
真的只是简单使用,Bboss宣传bboss-elasticsearch是最好用的Elasticsearch的java rest api,核心有dsl脚本和一些自己封装的使用。我只是简单使用了它封装的http接口,但是用起来真的很方便,依赖使用也很简单。
maven依赖
<!--elasticsearch -->
<!-- spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.14.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.14.RELEASE</version>
</dependency>
<!--elasticsearch client -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>5.5.2</version>
</dependency>
<!-- IK分词器 -->
<!--https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-analysis-ik-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-analysis-ik</artifactId>
<version>5.5.2</version>
</dependency>
<!-- ik拼音分词器 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-analysis-pinyin</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.nlpcn</groupId>
<artifactId>nlp-lang</artifactId>
<version>1.7.2</version>
</dependency>
<!--elasticsearch 结束-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- bboss -->
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
<version>5.5.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
配置文件设置
只需要一个配置文件即可,application.properties。
# elasticsearch config
elasticsearch.rest.hostNames=192.168.1.175:9200
主要是要注意位置,自定义也支持。
自定义可以参考文档:https://esdoc.bbossgroups.com/#/Elasticsearch-bboss-custom-init?id=%E5%8D%95%E4%B8%AAelasticsearch%E6%95%B0%E6%8D%AE%E6%BA%90%E6%A1%88%E4%BE%8B
/**
* boot操作必须在所有的ClientInterface组件创建之前调用
* 按照默认的配置文件初始化elasticsearch客户端工具
* conf/elasticsearch.properties,application.properties,config/application.properties
*/
//ElasticSearchBoot.boot();
/**
* boot操作必须在所有的ClientInterface组件创建之前调用
* 根据指定的配置文件初始化elasticsearch客户端工具
* @param configFile 指定1到多个多个ElasticSearch属性配置文件,对应的路径格式为(多个用逗号分隔),例如:
* conf/elasticsearch.properties,application.properties,config/application.properties
* 上述的文件都是在classpath下面即可,如果需要指定绝对路径,格式为:
* file:d:/conf/elasticsearch.properties,file:d:/application.properties,config/application.properties
*
* 说明:带file:前缀表示后面的路径为绝对路径
*/
ElasticSearchBoot.boot("myapplication.properties");
//ElasticSearchBoot.boot("file:/home/elasticsearch/myapplication.properties");
工具类
package cn.lonsun.es.base;
import cn.lonsun.core.base.util.StringUtils;
import cn.lonsun.core.util.ResultVO;
import cn.lonsun.core.util.SpringContextHolder;
import cn.lonsun.es.vo.BulkCommitVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.boot.ElasticSearchBoot;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESIndice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yanyl on 2019/2/26.
*/
public class BbossUtil {
private static final Logger logger = LoggerFactory.getLogger("BbossUtil");
private static ElasticsearchConfig elasticsearchConfig = SpringContextHolder.getBean(ElasticsearchConfig.class);
static {
try {
Map properties = new HashMap();
properties.put("elasticsearch.rest.hostNames",elasticsearchConfig.getRestHostNames());
if(!StringUtils.isEmpty(elasticsearchConfig.getTimeoutConnection()) && !elasticsearchConfig.getTimeoutConnection().equals("''")){
properties.put("http.timeoutConnection",elasticsearchConfig.getTimeoutConnection());
}
if(!StringUtils.isEmpty(elasticsearchConfig.getTimeoutSocket()) && !elasticsearchConfig.getTimeoutSocket().equals("''")){
properties.put("http.timeoutSocket",elasticsearchConfig.getTimeoutSocket());
}
if(!StringUtils.isEmpty(elasticsearchConfig.getConnectionRequestTimeout()) && !elasticsearchConfig.getConnectionRequestTimeout().equals("''")){
properties.put("http.connectionRequestTimeout",elasticsearchConfig.getConnectionRequestTimeout());
}
if(!StringUtils.isEmpty(elasticsearchConfig.getMaxTotal()) && !elasticsearchConfig.getMaxTotal().equals("''")){
properties.put("http.maxTotal",elasticsearchConfig.getMaxTotal());
}
if(!StringUtils.isEmpty(elasticsearchConfig.getDefaultMaxPerRoute()) && !elasticsearchConfig.getDefaultMaxPerRoute().equals("''")){
properties.put("http.defaultMaxPerRoute",elasticsearchConfig.getDefaultMaxPerRoute());
}
if(!StringUtils.isEmpty(elasticsearchConfig.getRetryTime()) && !elasticsearchConfig.getRetryTime().equals("''")){
properties.put("http.retryTime",elasticsearchConfig.getRetryTime());
}
ElasticSearchBoot.boot(properties);
}catch (Exception e){
logger.error("初始化ES搜索引擎Bboss组件失败",e);
}
}
/**
* 为index建立别名
* @param indexName
* @param alias
* @return
*/
public static Object addAlias(String indexName,String alias){
//创建es客户端工具,验证环境
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
Boolean exist = clientUtil.existIndice(indexName);
if(exist){
String result = clientUtil.addAlias(indexName,alias);
logger.info("建立别名成功:"+indexName+"-->"+alias);
return new ResultVO(result);
}else {
return new ResultVO(0,"建立别名失败,"+ indexName + "索引不存在" );
}
}
/**
* 为index删除别名(使搜索不到)
* @param indexName
* @param alias
* @return
*/
public static Object removeAlias(String indexName,String alias){
//创建es客户端工具,验证环境
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
Boolean exist = clientUtil.existIndice(indexName);
if(exist){
String result = clientUtil.removeAlias(indexName,alias);
logger.info("删除别名成功:"+indexName+"--"+alias);
return new ResultVO(result);
}
return new ResultVO(0,indexName+"索引不存在(index_not_found_exception)");
}
/**
* index是否存在
* @param indexName
* @return
*/
public static Boolean existIndex(String indexName){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
return clientUtil.existIndice(indexName);
}
/**
* alias是否存在
* @param indexName
* @param alias
* @return
*/
public static Object existAlias(String indexName,String alias){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
Boolean result = clientUtil.existIndice(indexName);
if(result){
String aliasStr = clientUtil.executeHttp("/"+indexName+"/_aliases","get");
return new ResultVO(aliasStr.contains(alias));
}
return new ResultVO(result);
}
/**
* type是否存在
* @param indexName
* @param typeName
* @return
*/
public static Boolean existIndiceType(String indexName,String typeName){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
return clientUtil.existIndiceType(indexName,typeName);
}
/**
* 创建索引index
* @param indexName
* @param mapping
* @return
*/
public static Object createIndex(String indexName,String mapping){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(!clientUtil.existIndice(indexName)){
String result = clientUtil.createIndiceMapping(indexName,mapping);
logger.info("创建索引成功:"+indexName);
return new ResultVO(result);
}else {
logger.info(indexName+"索引已存在");
return new ResultVO(0,indexName+"索引已存在");
}
}
/**
* 新建/修改type的mapping
* @param indexName
* @param typeName
* @param mapping
* @return
*/
public static Object updateIndiceMapping(String indexName,String typeName,String mapping){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(clientUtil.existIndiceType(indexName,typeName)){
logger.info(indexName+"索引type("+typeName+")已存在,更新type");
}else {
logger.info("创建"+indexName+"type:"+typeName);
}
String result = clientUtil.updateIndiceMapping(indexName+"/_mapping/"+typeName,mapping);
return new ResultVO(result);
}
/**
* 新增/更新一个文档
* @param indexName
* @param typeName
* @param info
* @param id
* @return
*/
public static Object addDocument(String indexName,String typeName,Object info,String id){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(clientUtil.existIndiceType(indexName,typeName)){
logger.info(indexName+"索引"+typeName+",插入数据");
String result = clientUtil.addDocumentWithId(indexName,typeName,info,id);
return new ResultVO(result);
}else {
logger.info(indexName+"索引"+typeName+",不存在,插入失败");
return new ResultVO(0,indexName+"索引"+typeName+",不存在,插入失败");
}
}
/**
* 删除一个文档
* @param indexName
* @param typeName
* @param id
* @return
*/
public static Object deleteDocument(String indexName,String typeName,String id){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(clientUtil.existIndiceType(indexName,typeName)){
logger.info(indexName+"索引"+typeName+",删除数据");
String result = clientUtil.deleteDocument(indexName,typeName,id);
return new ResultVO(result);
}else {
logger.info(indexName+"索引"+typeName+",不存在,删除失败");
return new ResultVO(0,indexName+"索引"+typeName+",不存在,删除失败");
}
}
/**
* 获取一个文档
* @param indexName
* @param typeName
* @param id
* @return
*/
public static Object getDocument(String indexName,String typeName,String id){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(clientUtil.existIndiceType(indexName,typeName)){
logger.info(indexName+"索引"+typeName+",查找数据");
String result = clientUtil.getDocument(indexName,typeName,id);
return new ResultVO(result);
}else {
logger.info(indexName+"索引"+typeName+",不存在,查找失败");
return new ResultVO(0,indexName+"索引"+typeName+",不存在,查找失败");
}
}
/**
* 重建索引
* @param oldIndex
* @param index
* @return
*/
public static Object reindex(String oldIndex,String index){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(clientUtil.existIndice(oldIndex) && clientUtil.existIndice(index)){
logger.info(oldIndex+"索引==>"+index+",重建索引");
String result = clientUtil.reindex(oldIndex,index);
return new ResultVO(result);
}else {
logger.info(oldIndex+"索引==>"+index+",重建索引失败");
return new ResultVO(0,oldIndex+"索引==>"+index+",重建索引失败");
}
}
/**
* 删除index
* @param indexName
* @return
*/
public static Object deleteIndex(String indexName){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
Boolean result = clientUtil.existIndice(indexName);
if(result){
return new ResultVO(clientUtil.dropIndice(indexName));
}
return new ResultVO(result);
}
public static Object deleteAllIndex(String tag){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
List<ESIndice> indiceList = clientUtil.getIndexes();
if(indiceList != null && !indiceList.isEmpty()){
for(ESIndice indice:indiceList){
if(indice.getIndex().contains(tag)){
clientUtil.dropIndice(indice.getIndex());
logger.info("删除索引:"+indice.getIndex());
}
}
}
return new ResultVO();
}
/**
* 通过tag模糊匹配索引,获取索引名称
* @param tag
* @return
*/
public static Object getIndexName(String tag){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
List<ESIndice> indiceList = clientUtil.getIndexes();
long dataNum = 0L;
String indexName = "";
if(indiceList != null && !indiceList.isEmpty()){
for(ESIndice indice:indiceList){
if(indice.getIndex().contains(tag)){
//若匹配到多个,取最后一个
long num = Long.parseLong(indice.getIndex().split("_date")[1]);
if(num > dataNum){
dataNum = num;
indexName = indice.getIndex();
}
}
}
}
return new ResultVO(indexName);
}
/**
* 根据查询删除
* @param indexName
* @param typeName
* @param key 查询字段名
* @param value 查询字段值
* @param addKeyword 是否需要加.keyword
* @return
*/
public static Object deleteByQuery(String indexName,String typeName,String key,Object value,boolean addKeyword){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
logger.info(indexName+"索引"+typeName+",删除数据");
//index type 是否存在
boolean isExist;
String deleteStr;
if(StringUtils.isEmpty(typeName)){
isExist = existIndex(indexName);
deleteStr = "/"+indexName+"/_delete_by_query?refresh=wait_for&scroll_size=1000&conflicts=proceed&wait_for_completion=false";
}else {
isExist = existIndiceType(indexName,typeName);
deleteStr = "/"+indexName+"/"+typeName+"/_delete_by_query?refresh=wait_for&scroll_size=1000&conflicts=proceed&wait_for_completion=false";
}
if(isExist){
String queryStr;
if(StringUtils.isEmpty(key) || null == value){
queryStr = "{\"query\":{\"match_all\":{}}}";
}else {
queryStr = "{\"query\": {\"term\": {\""+key+"\": \""+value+"\"}}}";
if(addKeyword){
queryStr = "{\"query\": {\"term\": {\""+key+".keyword\": \""+value+"\"}}}";
}
}
try {
String result = clientUtil.executeHttp(deleteStr,queryStr,"post");
logger.info("删除结果:{}",result);
JSONObject jsonObject = JSONObject.parseObject(result);
//删除数量
Integer deleted = jsonObject.getInteger("deleted");
//出错记录
JSONArray array = jsonObject.getJSONArray("failures");
if(deleted == 1 || array.size() == 0){
return new ResultVO(1);
}
}catch (Exception e){
logger.info("删除报错,已删除,{}",e.getMessage());
if(e.getMessage().contains("Timeout")){
e.printStackTrace();
}else {
JSONObject object = JSON.parseObject(e.getMessage().trim());
//出错记录
JSONArray array = object.getJSONArray("failures");
Integer status = ((JSONObject)array.get(0)).getInteger("status");
//是否冲突
Integer conflict = object.getInteger("version_conflicts");
if(status == 409 && conflict == 1){
logger.info("已删除,版本冲突");
return new ResultVO(409);
}
logger.info("删除索引出错记录:{}" , object);
logger.info("无论是否失败,请求发出,都认为删除成功");
}
}finally {
try {
Thread.sleep( 1000);
}catch (Exception e){
e.printStackTrace();
}
return new ResultVO(1);
}
// return new ResultVO(0,indexName+"索引"+typeName+",删除出错,删除失败");
} else {
logger.info(indexName+"索引"+typeName+",不存在,删除失败");
return new ResultVO(0,indexName+"索引"+typeName+",不存在,删除失败");
}
}
public static Object deleteByDate(String indexName, String typeName,String typeCode, String dateField, Long startDate, Long endDate){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
logger.info(indexName+"索引"+typeName+",删除数据");
//index type 是否存在
boolean isExist;
String deleteStr;
if(StringUtils.isEmpty(typeName)){
isExist = existIndex(indexName);
deleteStr = "/"+indexName+"/_delete_by_query?refresh=wait_for&scroll_size=1000&conflicts=proceed&wait_for_completion=true";
}else {
isExist = existIndiceType(indexName,typeName);
deleteStr = "/"+indexName+"/"+typeName+"/_delete_by_query?refresh=wait_for&scroll_size=1000&conflicts=proceed&wait_for_completion=true";
}
if(isExist){
String queryStr = "{\"query\":{\"bool\":{\"adjust_pure_negative\":true,\"boost\":1.0,\"disable_coord\":false,\"must\"" +
":[{\"term\":{\"typeCode.keyword\":{\"boost\":1.0,\"value\":\"" + typeCode + "\"}}}," +
"{\"range\":{\"" + dateField + "\":{\"boost\":1.0,\"from\":" + startDate + "," +
"\"include_lower\":true,\"include_upper\":true,\"to\":" + endDate + "}}}]}}}";
logger.info("删除索引语句:" + queryStr);
try {
String result = clientUtil.executeHttp(deleteStr,queryStr,"post");
logger.info("删除结果:{}",result);
JSONObject jsonObject = JSONObject.parseObject(result);
//删除数量
Integer deleted = jsonObject.getInteger("deleted");
//出错记录
JSONArray array = jsonObject.getJSONArray("failures");
if(deleted == 1 || array.size() == 0){
return new ResultVO(1);
}
}catch (Exception e){
logger.info("删除报错,已删除,{}",e.getMessage());
if(e.getMessage().contains("Timeout")){
e.printStackTrace();
}else {
JSONObject object = JSON.parseObject(e.getMessage().trim());
//出错记录
JSONArray array = object.getJSONArray("failures");
Integer status = ((JSONObject)array.get(0)).getInteger("status");
//是否冲突
Integer conflict = object.getInteger("version_conflicts");
if(status == 409 && conflict == 1){
logger.info("已删除,版本冲突");
return new ResultVO(409);
}
logger.info("删除索引出错记录:{}" , object);
logger.info("无论是否失败,请求发出,都认为删除成功");
}
}finally {
try {
Thread.sleep( 1000);
}catch (Exception e){
e.printStackTrace();
}
return new ResultVO(1);
}
// return new ResultVO(0,indexName+"索引"+typeName+",删除出错,删除失败");
} else {
logger.info(indexName+"索引"+typeName+",不存在,删除失败");
return new ResultVO(0,indexName+"索引"+typeName+",不存在,删除失败");
}
}
/**
* 批量创建/更新/删除索引
* @param bulkCommitVOList
* @return
*/
public static Object bulkIndex(List<BulkCommitVO> bulkCommitVOList){
//创建es客户端
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
if(bulkCommitVOList.size()>0 && clientUtil.existIndice(bulkCommitVOList.get(0).getIndexName())){
StringBuffer requestStr = new StringBuffer();
for(int i=0;i<bulkCommitVOList.size();i++){
requestStr.append("{ \""+bulkCommitVOList.get(i).getAction()+"\" : { \"_index\" : \""
+bulkCommitVOList.get(i).getIndexName()+"\", \"_type\" : \""+bulkCommitVOList.get(i).getTypeName()+"\", \"_id\" : \""
+bulkCommitVOList.get(i).getId()+"\" } }\n");
if(!bulkCommitVOList.get(i).getAction().equals("delete")){
requestStr.append(bulkCommitVOList.get(i).getInfo()).append("\n");
}
}
if(bulkCommitVOList.get(0).getAction().contains("delete")){
System.out.println("requestStr=\n"+requestStr.toString());
}
String bulkStr = clientUtil.executeHttp("/_bulk",requestStr.toString(),"post");
esLog.info(bulkStr);
return new ResultVO(bulkStr);
}
return new ResultVO();
}
}