package com.wiwj.app.search.util;
import com.wiwj.app.common.constant.PropertyConstantsKey;
import com.wiwj.app.common.constant.PropertyTools;
import com.wiwj.app.search.conf.ElasticSearch;
import com.wiwj.app.util.Util;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* Title: ElasticsearchOperUtil.java
*
* Package: core.util
*
* Description: ES操作工具类
*
* @author:
*
* @date: 2017年5月10日 下午2:16:20
*
* @version: 1.0
*/
public class ElasticsearchOperUtil {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ElasticsearchOperUtil.class);
/** ESclient */
private static Client client = ElasticSearch.getInstance().getESClient();
/**
*
* @Title: deleteByType
* @Description: 删除某一类型所有数据
* @author:
* @date: 2017年5月26日 下午4:18:21
* @param index
* @param type
* @return
*/
@SuppressWarnings("deprecation")
public static boolean deleteByType(String index,String type){
//删除es中的视图
MatchAllQueryBuilder allQueryBuilder = QueryBuilders.matchAllQuery();
client.prepareDeleteByQuery(index).setQuery(allQueryBuilder).setTypes(type).execute().actionGet();
return true;
}
/**
*
* @Title: queryData
* @Description: 单字段查询
* @author:
* @date: 2017年5月10日 上午10:43:31
* @param index
* @param type
* @param primaryColumn
* @param primaryColumn_value
* @return
*/
public static List queryData(String index,String type,String primaryColumn, String primaryColumn_value){
try {
BoolQueryBuilder querySql = QueryBuilders.boolQuery();
querySql.must(QueryBuilders.termQuery(primaryColumn, primaryColumn_value));
SearchRequestBuilder searchRequest = client.prepareSearch(index)
.setTypes(type).setQuery(querySql).setFrom(0).setSize(10000);
SearchResponse actionGet = searchRequest.execute().actionGet();
SearchHits hits = actionGet.getHits();
String json = "";
List dataList = new ArrayList();
for (SearchHit hit : hits) {
json = hit.getSourceAsString();
dataList.add(json);
}
return dataList;
} catch (Exception e) {
return null;
}
}
/**
*
* @Title: addData
* @Description: 添加数据
* @author:
* @date: 2017年5月9日 下午3:05:52
* @param index
* @param type
* @param dataJson
* @return
*/
public static boolean addData(String index,String type,String dataJson) throws Exception{
BulkRequestBuilder bulkRequest = client.prepareBulk();
IndexRequest request = client.prepareIndex(index, type,"主键ID")//如果不指定主键ID,则es会生成一个随机的主键ID
.setSource(dataJson).request();
bulkRequest.add(request);
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
logger.info(bulkResponse.buildFailureMessage());
return false;
}
return true;
}
/**
*
* @Title: deleteBySingleColumnConditon
* @Description: 删除 列=value
* @author:
* @date: 2017年5月9日 下午3:01:53
* @param index
* @param type
* @param column
* @param value
* @return
*/
@SuppressWarnings("deprecation")
public static boolean deleteBySingleColumnConditon(String index,String type,String column,String value) throws Exception{
QueryBuilder query = QueryBuilders.matchQuery(column, value);
client.prepareDeleteByQuery(index).setTypes(type).setQuery(query).execute().actionGet();
return true;
}
/**
* deleteBySingleColumnConditon(根据id=id1,id2删除)
* 创建人:
* 创建时间:2017年7月20日 上午10:51:57
* 修改人:
* 修改时间:2017年7月20日 上午10:51:57
* 修改备注:
* @param index
* @param type
* @param column
* @param value 多个条件的值集合
* @return
*/
@SuppressWarnings("deprecation")
public static boolean deleteBySingleColumnConditon(String index,String type,String column,List value){
try {
QueryBuilder query = QueryBuilders.termsQuery(column, value);
client.prepareDeleteByQuery(index).setTypes(type).setQuery(query).execute().actionGet();
return true;
} catch (Exception e) {
return false;
}
}
/**
* @Title: deleteBySingleColumnConditon
* @Description: 根据多个主键删除
* @author:
* @date: 2017/9/11 18:15
* @param index
* @param type
*/
public static boolean deleteByMultipleColumnConditon(String index,String type,Map prama){
try {
BoolQueryBuilder querySql = QueryBuilders.boolQuery();
Set> entries = prama.entrySet();
for (Map.Entry entry : entries) {
querySql.must(QueryBuilders.termsQuery(entry.getKey(), entry.getValue()));
}
client.prepareDeleteByQuery(index).setTypes(type).setQuery(querySql).execute().actionGet();
return true;
} catch (Exception e) {
return false;
}
}
/**
* 根据map多条件删除
* @param index
* @param type
* @param prama
* @return
*/
public static boolean deleteByMultiStringColumnConditon(String index,String type,Map prama){
try {
BoolQueryBuilder querySql = QueryBuilders.boolQuery();
Set> entries = prama.entrySet();
for (Map.Entry entry : entries) {
querySql.must(QueryBuilders.termsQuery(entry.getKey(), String.valueOf(entry.getValue())));
}
client.prepareDeleteByQuery(index).setTypes(type).setQuery(querySql).execute().actionGet();
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* @Title: queryAllDataByIndexAndType
* @Description: 查询所有数据
* @author:
* @date: 2017年5月18日 下午2:31:00
* @param index
* @param type
* @return
*/
public static List queryAllDataByIndexAndType(String index,String type){
try {
BoolQueryBuilder querySql = QueryBuilders.boolQuery();
SearchRequestBuilder searchRequest = client.prepareSearch(index)
.setTypes(type).setQuery(querySql).setFrom(0).setSize(100000);
SearchResponse actionGet = searchRequest.execute().actionGet();
SearchHits hits = actionGet.getHits();
String json = "";
List dataList = new ArrayList();
for (SearchHit hit : hits) {
json = hit.getSourceAsString();
dataList.add(json);
}
return dataList;
} catch (Exception e) {
return null;
}
}
/**
*
* @Title: deleteData
* @Description: 根据id删除数据
* @author:
* @date: 2017年6月22日 下午4:44:49
* @param index
* @param type
* @param ids
*/
@SuppressWarnings("unused")
public static void deleteDataByids(String index,String type, String ids) throws Exception{
String[] idArr = ids.split(",");
for (int i = 0; i < idArr.length; i++) {
DeleteResponse response = client.prepareDelete(index, type, idArr[i])
.execute()
.actionGet();
}
}
/**
* bulkSyncToEs(批量同步es)
* 创建人
* 创建时间:2017年7月20日 上午10:46:52
* 修改人:
* 修改时间:2017年7月20日 上午10:46:52
* 修改备注:
* @param list(包含三个参数,index+type:当前数据对应的es的index+type,json为组装好的json格式数据)
*/
public static boolean bulkSyncToEs(List