package cn.focus.dc.app.xinfang.service.third;
import static cn.focus.dc.app.constants.AppConstants.ES_INDEX_PASSWORD;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.gson.JsonObject;
import cn.focus.dc.app.exception.EsSearchException;
import cn.focus.dc.app.xinfang.model.es.qry.EsQuery;
import cn.focus.dc.app.xinfang.model.es.rst.EsResult;
import cn.focus.dc.app.xinfang.model.es.rst.Result;
@Service
public class EsAdvancedService {
private static final String PWDKEY = "X-SCE-ES-PASSWORD";
private static Logger logger = LoggerFactory.getLogger(EsAdvancedService.class);
@Autowired
private JestClient jestClient;
/**
* 创建索引
* @param indexName
* @return
*/
public boolean createIndex(String indexName){
CreateIndex createIndex = new CreateIndex(new CreateIndex.Builder(indexName));
try{
JestResult result = jestClient.execute(createIndex);
if(result == null || !result.isSucceeded()){
throw new Exception(result.getErrorMessage()+"创建索引失败!");
}
}catch(Exception e){
logger.error("",e);
return false;
}
return true;
}
/**
* 手动创建类型(map一旦定义创建,field只能新增,不能修改)
* @param indexName
* @param indexType
* @param mappingString
* @return
*/
public boolean createType(String indexName, String indexType, String mappingString){
PutMapping.Builder builder = new PutMapping.Builder(indexName, indexType, mappingString);
builder.setHeader(PWDKEY, getSecret());
builder.refresh(true);
try {
JestResult result = jestClient.execute(builder.build());
if(result == null || !result.isSucceeded()){
throw new RuntimeException(result.getErrorMessage()+"创建索引类型失败!");
}
} catch (Exception e) {
logger.error("",e);
return false;
}
return true;
}
/**
* 获取索引类型mapping
*
* @param typeName 类型名称
* @return
*/
public String getMapping(String indexName,String typeName) {
GetMapping.Builder builder = new GetMapping.Builder();
builder.addIndex(indexName).addType(typeName);
builder.setHeader("X-SCE-ES-PASSWORD", getSecret());
try {
JestResult result = jestClient.execute(builder.build());
if (result != null && result.isSucceeded()) {
return result.getSourceAsObject(JsonObject.class).toString();
}
logger.error("es get mapping Exception ", result.getErrorMessage());
} catch (Exception e) {
logger.error("", e);
}
return null;
}
/**
* 删除文档
* @param indexId
* @param indexName
* @param indexType
*/
public boolean deleteDoc(String indexId, String indexName, String indexType) {
Delete.Builder builder = new Delete.Builder();
builder.setHeader(PWDKEY, getSecret());
builder.id(indexId);
builder.refresh(true);
Delete delete = builder.index(indexName).type(indexType).build();
try {
JestResult result = jestClient.execute(delete);
if (result != null && !result.isSucceeded()) {
throw new RuntimeException(result.getErrorMessage()+"删除文档失败!");
}
} catch (Exception e) {
logger.error("",e);
return false;
}
return true;
}
/**
* 删除类型
* @param indexId
* @param indexName
* @param indexType
*/
public boolean deleteType( String indexName, String indexType) {
Delete.Builder builder = new Delete.Builder();
builder.setHeader(PWDKEY, getSecret());
builder.refresh(true);
Delete delete = builder.index(indexName).type(indexType).build();
try {
JestResult result = jestClient.execute(delete);
if (result != null && result.isSucceeded()) {
throw new RuntimeException(result.getErrorMessage()+"删除类型失败!");
}
} catch (Exception e) {
logger.error("",e);
return false;
}
return true;
}
/**
* 删除索引
* @param indexId
* @param indexName
* @param indexType
*/
public boolean deleteIndex( String indexName) {
Delete.Builder builder = new Delete.Builder();
builder.setHeader(PWDKEY, getSecret());
builder.refresh(true);
Delete delete = builder.index(indexName).build();
try {
JestResult result = jestClient.execute(delete);
if (result != null && result.isSucceeded()) {
throw new RuntimeException(result.getErrorMessage()+"删除索引失败!");
}
} catch (Exception e) {
logger.error("",e);
return false;
}
return true;
}
/**
* 插入或更新文档
* @param indexId
* @param indexObject
* @param indexName
* @param indexType
* @return
*/
public boolean insertOrUpdateDoc(String indexId, Object indexObject, String indexName, String indexType) {
Index.Builder builder = new Index.Builder(indexObject);
builder.setHeader(PWDKEY, getSecret());
builder.id(indexId);
builder.refresh(true);
Index index = builder.index(indexName).type(indexType).build();
try {
JestResult result = jestClient.execute(index);
if (result != null && !result.isSucceeded()) {
throw new RuntimeException(result.getErrorMessage()+"插入更新索引失败!");
}
} catch (Exception e) {
logger.error("",e);
return false;
}
return true;
}
/**
* 通过一次查询就可获取查询的结果(分页)及总条数
* @param classType
* @param indexName
* @param indexType
* @param esQuery
* @return
*/
public EsResult search(Class classType,String indexName,String indexType,EsQuery esQuery){
try{
Search.Builder builder = new Search.Builder(getQueryString(esQuery));
builder.addIndex(indexName)
.addType(indexType)
.setHeader(PWDKEY, getSecret())
.setParameter("from", esQuery.getFrom())
.setParameter("size", esQuery.getSize())
;
if(esQuery.getSort()!=null){
builder.addSort(esQuery.getSort());
}
JestResult jestResult = jestClient.execute(builder.build());
int hitCount = jestResult.getJsonObject().get("hits").getAsJsonObject().get("total").getAsInt();
EsResult result = new EsResult();
result.setTotalNum(hitCount);
result.setResultList(jestResult.getSourceAsObjectList(classType));
return result;
}catch(Exception e){
throw new EsSearchException(e);
}
}
private String getQueryString(EsQuery esQuery){
return "{\"query\":" + esQuery.buildQuery().toString() + "}";
}
protected static String getSecret() {
long time = System.currentTimeMillis() / 1000;
return time + "," + DigestUtils.md5Hex(time + ES_INDEX_PASSWORD).toUpperCase();
}
}