|
@ -1,21 +1,14 @@
|
|
package com.yihu.base.es.config;
|
|
package com.yihu.base.es.config;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.yihu.base.es.config.model.SaveModel;
|
|
|
|
|
|
import com.yihu.base.es.config.model.ESIDEntity;
|
|
import io.searchbox.client.JestClient;
|
|
import io.searchbox.client.JestClient;
|
|
import io.searchbox.client.JestResult;
|
|
import io.searchbox.client.JestResult;
|
|
import io.searchbox.core.*;
|
|
import io.searchbox.core.*;
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Scope;
|
|
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
|
|
import org.springframework.data.elasticsearch.core.query.UpdateQueryBuilder;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@ -24,15 +17,23 @@ import java.util.Map;
|
|
/**
|
|
/**
|
|
* Created by chenweida on 2017/6/2.
|
|
* Created by chenweida on 2017/6/2.
|
|
*/
|
|
*/
|
|
@Component
|
|
|
|
@Scope("prototype")
|
|
|
|
public class ElastricSearchHelper {
|
|
public class ElastricSearchHelper {
|
|
|
|
|
|
|
|
private ElasticsearchUtil elasticsearchUtil;
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(ElastricSearchHelper.class);
|
|
private Logger logger = LoggerFactory.getLogger(ElastricSearchHelper.class);
|
|
@Autowired
|
|
@Autowired
|
|
private ElasticFactory elasticFactory;
|
|
private ElasticFactory elasticFactory;
|
|
|
|
|
|
public Boolean save(String index, String type, List<SaveModel> sms) {
|
|
|
|
|
|
/**
|
|
|
|
* 新增
|
|
|
|
*
|
|
|
|
* @param index
|
|
|
|
* @param type
|
|
|
|
* @param sms
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Boolean save(String index, String type, List<Object> sms) {
|
|
JestClient jestClient = null;
|
|
JestClient jestClient = null;
|
|
try {
|
|
try {
|
|
//得到链接elasticFactory.getJestClient();
|
|
//得到链接elasticFactory.getJestClient();
|
|
@ -40,7 +41,7 @@ public class ElastricSearchHelper {
|
|
int success = 0;
|
|
int success = 0;
|
|
int error = 0;
|
|
int error = 0;
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
for (SaveModel obj : sms) {
|
|
|
|
|
|
for (Object obj : sms) {
|
|
try {
|
|
try {
|
|
Index indexObj = new Index.Builder(obj).build();
|
|
Index indexObj = new Index.Builder(obj).build();
|
|
success++;
|
|
success++;
|
|
@ -98,7 +99,7 @@ public class ElastricSearchHelper {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean update(String index, String type, List<SaveModel> sms) {
|
|
|
|
|
|
public Boolean update(String index, String type, List<Object> sms) {
|
|
JestClient jestClient = null;
|
|
JestClient jestClient = null;
|
|
BulkResult br = null;
|
|
BulkResult br = null;
|
|
try {
|
|
try {
|
|
@ -109,11 +110,11 @@ public class ElastricSearchHelper {
|
|
int error = 0;
|
|
int error = 0;
|
|
boolean isSuccessed = true;
|
|
boolean isSuccessed = true;
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
for (SaveModel obj : sms) {
|
|
|
|
|
|
for (Object obj : sms) {
|
|
try {
|
|
try {
|
|
JSONObject jo = new JSONObject();
|
|
JSONObject jo = new JSONObject();
|
|
jo.put("doc", obj);
|
|
jo.put("doc", obj);
|
|
Update indexObj = new Update.Builder(jo.toString()).index(index).type(type).id(obj.getId()).build();
|
|
|
|
|
|
Update indexObj = new Update.Builder(jo.toString()).index(index).type(type).id(((ESIDEntity) obj).getId()).build();
|
|
bulk.addAction(indexObj);
|
|
bulk.addAction(indexObj);
|
|
success++;
|
|
success++;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@ -137,50 +138,50 @@ public class ElastricSearchHelper {
|
|
return br.isSucceeded();
|
|
return br.isSucceeded();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除
|
|
|
|
|
|
* 修改
|
|
*/
|
|
*/
|
|
public void deleteData(String index, String type, List<SaveModel> saveModels) {
|
|
|
|
|
|
public boolean update(String index, String type, String _id, JSONObject source) {
|
|
JestClient jestClient = null;
|
|
JestClient jestClient = null;
|
|
|
|
JestResult jestResult = null;
|
|
try {
|
|
try {
|
|
jestClient = elasticFactory.getJestClient();
|
|
jestClient = elasticFactory.getJestClient();
|
|
|
|
|
|
//根据id批量删除
|
|
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
|
|
for (SaveModel obj : saveModels) {
|
|
|
|
Delete indexObj = new Delete.Builder(obj.getId()).build();
|
|
|
|
bulk.addAction(indexObj);
|
|
|
|
}
|
|
|
|
BulkResult br = jestClient.execute(bulk.build());
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("delete data count:" + saveModels.size());
|
|
|
|
logger.info("delete flag:" + br.isSucceeded());
|
|
|
|
|
|
JSONObject docSource = new JSONObject();
|
|
|
|
docSource.put("doc", source);
|
|
|
|
Update update = new Update.Builder(docSource).index(index).type(type).id(_id).build();
|
|
|
|
jestResult = jestClient.execute(update);
|
|
|
|
logger.info("update info:" + jestResult.isSucceeded());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
|
|
|
|
logger.error("update fail:" + _id, e.getMessage());
|
|
|
|
return false;
|
|
} finally {
|
|
} finally {
|
|
if (jestClient != null) {
|
|
if (jestClient != null) {
|
|
jestClient.shutdownClient();
|
|
jestClient.shutdownClient();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 查询
|
|
|
|
|
|
* 删除
|
|
*/
|
|
*/
|
|
public SearchResult search(String index, String type, String queryStr) {
|
|
|
|
|
|
public void delete(String index, String type, List<Object> ESIDEntitys) {
|
|
JestClient jestClient = null;
|
|
JestClient jestClient = null;
|
|
SearchResult result = null;
|
|
|
|
try {
|
|
try {
|
|
jestClient = elasticFactory.getJestClient();
|
|
jestClient = elasticFactory.getJestClient();
|
|
Search search = new Search.Builder(queryStr)
|
|
|
|
// multiple index or types can be added.
|
|
|
|
.addIndex(index)
|
|
|
|
.addType(type)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
result = jestClient.execute(search);
|
|
|
|
logger.info("search data count:" + result.getTotal());
|
|
|
|
|
|
|
|
|
|
//根据id批量删除
|
|
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
|
|
for (Object obj : ESIDEntitys) {
|
|
|
|
Delete indexObj = new Delete.Builder(((ESIDEntity) obj).getId()).build();
|
|
|
|
bulk.addAction(indexObj);
|
|
|
|
}
|
|
|
|
BulkResult br = jestClient.execute(bulk.build());
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("delete data count:" + ESIDEntitys.size());
|
|
|
|
logger.info("delete flag:" + br.isSucceeded());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
} finally {
|
|
} finally {
|
|
@ -188,39 +189,24 @@ public class ElastricSearchHelper {
|
|
jestClient.shutdownClient();
|
|
jestClient.shutdownClient();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 修改
|
|
|
|
|
|
* 执行sql
|
|
|
|
*
|
|
|
|
* @param sql
|
|
|
|
* @return
|
|
*/
|
|
*/
|
|
public boolean update(String index, String type,String _id, JSONObject source) {
|
|
|
|
JestClient jestClient = null;
|
|
|
|
JestResult jestResult = null;
|
|
|
|
try {
|
|
|
|
jestClient = elasticFactory.getJestClient();
|
|
|
|
JSONObject docSource = new JSONObject();
|
|
|
|
docSource.put("doc",source);
|
|
|
|
Update update = new Update.Builder(docSource).index(index).type(type).id(_id).build();
|
|
|
|
jestResult = jestClient.execute(update);
|
|
|
|
logger.info("update info:" + jestResult.isSucceeded());
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("update fail:" + _id,e.getMessage());
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
if (jestClient != null) {
|
|
|
|
jestClient.shutdownClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
public List<Map<String, Object>> excuceSQL(String sql) {
|
|
|
|
return elasticsearchUtil.excuteDataModel(sql);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ElasticsearchUtil getElasticsearchUtil() {
|
|
|
|
return elasticsearchUtil;
|
|
|
|
}
|
|
|
|
|
|
public static void main(String args[]){
|
|
|
|
String json = "";
|
|
|
|
JSONObject resultJsonObject = (JSONObject)JSONObject.parse(json);
|
|
|
|
JSONObject jsonObject = (JSONObject)resultJsonObject.get("hits");
|
|
|
|
System.out.println(jsonObject.get("total"));
|
|
|
|
|
|
public void setElasticsearchUtil(ElasticsearchUtil elasticsearchUtil) {
|
|
|
|
this.elasticsearchUtil = elasticsearchUtil;
|
|
}
|
|
}
|
|
}
|
|
}
|