提交 929cb21c 编写于 作者: FelixHPP's avatar FelixHPP

fix: 修改批量新增文档错误问题

上级 3c743163
......@@ -177,30 +177,43 @@ public class EasyEs {
return getEsRest(client);
}
public String bulkAddDoc(String index, List<Map> objects) {
// public String getEsVersion(){
// client.getLowLevelClient().
// return responseBody(defaultMethod, newEndpoint);
// }
public void bulkAddDoc(String index, List<Map> objects) {
BulkRequest request = new BulkRequest();
BulkResponse bulkResponse = null;
try {
if (objects == null || objects.size() < 1) {
throw new indi.felix.easy.core.exceptions.ElasticsearchException("mappers can not be empty");
}
for (int i = 0; i < objects.size(); i++) {
Map mapper = objects.get(i);
IndexRequest indexRequest = new IndexRequest(index, DOC_TYPE);
EsUtils.setDefaultProperties(indexRequest, mapper);
request.add(indexRequest
.opType("create")
.source(JSON.toJSONString(mapper), XContentType.JSON));
}
bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
if(!rest().indexExists(index)){
throw new ElasticsearchException("index is not exists");
}
if (objects == null || objects.size() < 1) {
throw new ElasticsearchException("mappers can not be empty");
}
RestStatus stat = bulkResponse.status();
return bulkResponse.toString();
search().bulkInsert(index, objects, 2000);
//
// BulkResponse bulkResponse = null;
// try {
// if (objects == null || objects.size() < 1) {
// throw new ElasticsearchException("mappers can not be empty");
// }
//
// for (int i = 0; i < objects.size(); i++) {
// Map mapper = objects.get(i);
// IndexRequest indexRequest = new IndexRequest(index);
// EsUtils.setDefaultProperties(indexRequest, mapper);
//
// request.add(indexRequest
// .opType("create")
// .source(JSON.toJSONString(mapper), XContentType.JSON));
// }
// bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
// } catch (IOException e) {
// e.printStackTrace();
// }
// RestStatus stat = bulkResponse.status();
}
}
......@@ -135,7 +135,6 @@ public abstract class AbstractService {
} catch (Exception e) {
logger.error("the failed json element index range is {} - {}.", count - cursor, count);
logger.error("Cause by: {}.", e.getMessage());
e.printStackTrace();
}
if (failure.size() > 0) {
......
......@@ -41,7 +41,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public String requestDsl(String index, String dsl) {
String newEndpoint = MessageFormat.format("/{0}/{1}/_search", index, DOC_TYPE);
String newEndpoint = MessageFormat.format("/{0}/_search", index);
return responseBody(defaultMethod, newEndpoint, dsl);
}
......@@ -54,7 +54,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public String requestById(String index, String id) {
String newEndpoint = MessageFormat.format("/{0}/{1}/{2}", index, DOC_TYPE, id);
String newEndpoint = MessageFormat.format("/{0}/{1}", index, id);
return responseBody(defaultMethod, newEndpoint);
}
......@@ -71,7 +71,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public String requestByIds(String index, String... ids) {
String newEndpoint = MessageFormat.format("/{0}/{1}/{2}", index, DOC_TYPE, "_mget");
String newEndpoint = MessageFormat.format("/{0}/{1}", index, "_mget");
XContentBuilder builder = null;
try {
......@@ -130,7 +130,7 @@ public class SearchRestApi extends BaseRest {
public String requestById(String id) {
String indexs = String.join(",", defaultIndex);
String newEndpoint = MessageFormat.format("/{0}/{1}/{2}", indexs, DOC_TYPE, id);
String newEndpoint = MessageFormat.format("/{0}/{2}", indexs, id);
return responseBody(defaultMethod, newEndpoint);
}
......
......@@ -2,7 +2,11 @@ package indi.felix.easy.core.elastic.utool;
public class Const {
public static final String DOC_TYPE = "doc";
/*
* 注意: 在7.x中type移除, 为了保证6.x和7.x路径一致, doctype强制使用_doc
* include_type_name参数设为true
*/
public static final String DOC_TYPE = "_doc";
/**
* 默认的ID字段, 对象中如果有该字段, 默认存储ES将按该字段设置ID
......
package indi.felix.easy.core.elastic;
import junit.framework.TestCase;
import org.elasticsearch.index.query.TermQueryBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EasyEsTest extends TestCase {
private static final String ip = "localhost:9200";
public void testBulkAddDoc() {
EasyEs easyEs = new EasyEs(ip);
String index = "test_index";
List<Map> object = new ArrayList<>();
Map<String, Object> m1 = new HashMap<>();
m1.put("name", "ceshi1");
m1.put("age", 2);
Map<String, Object> m2 = new HashMap<>();
m2.put("name", "dsadsa");
m2.put("age", 12);
Map<String, Object> m3 = new HashMap<>();
m3.put("name", "sdav");
m3.put("age", 40);
object.add(m1);
object.add(m2);
object.add(m3);
easyEs.bulkAddDoc(index, object);
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("age", 12);
String res = easyEs.rest().search().requestByQuery(index, termQueryBuilder);
System.out.println(res);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册