diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java index 137bc947253da706f639518c1d236943b9831319..604a9f532bcb4d2348543f8e8c5e7c6ed5e45952 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java @@ -177,30 +177,43 @@ public class EasyEs { return getEsRest(client); } - public String bulkAddDoc(String index, List objects) { +// public String getEsVersion(){ +// client.getLowLevelClient(). +// return responseBody(defaultMethod, newEndpoint); +// } + + public void bulkAddDoc(String index, List 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(); } } diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java index 6260d17c29111b652b2ba5185ff48f3dcd820ad4..dd11785e709f9744bcb4d1d82a5e0faa3153b3ed 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java @@ -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) { diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java index fc8364865710befb3c84c3627004c1969ba57aad..2ade345147df81d8fbcbd47299beffbb8d9afe71 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java @@ -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); } diff --git a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java index 424d0a8152e64ea3e0b571cea6cc3fa6db83b54e..3c96cbc5509e929a4d000c33fa7954498091a69a 100644 --- a/easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java +++ b/easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java @@ -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 diff --git a/easy-es-core/src/test/java/indi/felix/easy/core/elastic/EasyEsTest.java b/easy-es-core/src/test/java/indi/felix/easy/core/elastic/EasyEsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..90c76ad7d6ce04c4427ef745024168de7ab9e92b --- /dev/null +++ b/easy-es-core/src/test/java/indi/felix/easy/core/elastic/EasyEsTest.java @@ -0,0 +1,45 @@ +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 object = new ArrayList<>(); + + Map m1 = new HashMap<>(); + m1.put("name", "ceshi1"); + m1.put("age", 2); + + + Map m2 = new HashMap<>(); + m2.put("name", "dsadsa"); + m2.put("age", 12); + + + Map 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