|
@ -117,8 +117,8 @@ public class ElastricSearchHelper {
|
|
|
int error = 0;
|
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
|
try {
|
|
|
|
|
|
Index indexObj = new Index.Builder(source).id(((JSONObject) JSONUtils.parse(source)).getString(idFieldString)).
|
|
|
JSONObject jsonObject = (JSONObject)(JSONObject.parse(source));
|
|
|
Index indexObj = new Index.Builder(source).id(jsonObject.getString(idFieldString)).
|
|
|
build();
|
|
|
success++;
|
|
|
bulk.addAction(indexObj);
|
|
@ -141,6 +141,51 @@ public class ElastricSearchHelper {
|
|
|
return br.isSucceeded();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 自定义ID
|
|
|
* @param index
|
|
|
* @param type
|
|
|
* @param sources
|
|
|
* @param idFieldString
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean saveBulkWithCustomId(String index, String type, List<String> sources, String idFieldString) {
|
|
|
JestClient jestClient = null;
|
|
|
BulkResult br = null;
|
|
|
try {
|
|
|
//得到链接elasticFactory.getJestClient();
|
|
|
jestClient = elasticFactory.getJestClient();
|
|
|
int success = 0;
|
|
|
int error = 0;
|
|
|
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
|
|
|
try {
|
|
|
for(String source:sources){
|
|
|
JSONObject jsonObject = (JSONObject)(JSONObject.parse(source));
|
|
|
Index indexObj = new Index.Builder(source).id(jsonObject.getString(idFieldString)).
|
|
|
build();
|
|
|
success++;
|
|
|
bulk.addAction(indexObj);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
error++;
|
|
|
}
|
|
|
br = jestClient.execute(bulk.build());
|
|
|
logger.info("save flag:" + br.isSucceeded());
|
|
|
logger.info("save success:" + success);
|
|
|
logger.info("save error:" + error);
|
|
|
return br.isSucceeded();
|
|
|
} catch (Exception e) {
|
|
|
logger.error(" save error :" + e.getMessage());
|
|
|
} finally {
|
|
|
if (jestClient != null) {
|
|
|
jestClient.shutdownClient();
|
|
|
}
|
|
|
}
|
|
|
return br.isSucceeded();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新
|
|
|
*
|