|
|
@@ -105,6 +105,358 @@ public abstract class BaseElasticsearchService {
|
|
|
|
|
|
public void createMapping(String index) throws Exception {
|
|
|
String source =
|
|
|
+ "{\n" +
|
|
|
+ " \"properties\":{\n" +
|
|
|
+ " \"_class\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"attrList\":{\n" +
|
|
|
+ " \"type\":\"nested\",\n" +
|
|
|
+ " \"properties\":{\n" +
|
|
|
+ " \"name\":{\n" +
|
|
|
+ " \"type\":\"keyword\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"type\":{\n" +
|
|
|
+ " \"type\":\"long\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"value\":{\n" +
|
|
|
+ " \"type\":\"keyword\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"brandId\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"buyCount\":{\n" +
|
|
|
+ " \"type\":\"long\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"releaseTime\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"categoryPath\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"commentNum\":{\n" +
|
|
|
+ " \"type\":\"long\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"goodsId\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"goodsName\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"analyzer\":\"ik_max_word\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"grade\":{\n" +
|
|
|
+ " \"type\":\"float\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"highPraiseNum\":{\n" +
|
|
|
+ " \"type\":\"long\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"id\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"intro\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"isAuth\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"marketEnable\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"mobileIntro\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"point\":{\n" +
|
|
|
+ " \"type\":\"long\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"price\":{\n" +
|
|
|
+ " \"type\":\"float\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"salesModel\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"recommend\":{\n" +
|
|
|
+ " \"type\":\"boolean\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"selfOperated\":{\n" +
|
|
|
+ " \"type\":\"boolean\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"sellerId\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"sellerName\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"shopCategoryPath\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fielddata\":true,\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"sn\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"productEnterprise\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"productBitchNo\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"expireDate\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"thumbnail\":{\n" +
|
|
|
+ " \"type\":\"text\",\n" +
|
|
|
+ " \"fields\":{\n" +
|
|
|
+ " \"keyword\":{\n" +
|
|
|
+ " \"type\":\"keyword\",\n" +
|
|
|
+ " \"ignore_above\":256\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}";
|
|
|
+
|
|
|
+ PutMappingRequest request = new PutMappingRequest(index)
|
|
|
+ .source(source, XContentType.JSON);
|
|
|
+// AcknowledgedResponse putMappingResponse = client.indices().putMapping(request,
|
|
|
+// RequestOptions.DEFAULT);
|
|
|
+//
|
|
|
+// boolean acknowledged = putMappingResponse.isAcknowledged();
|
|
|
+// if (acknowledged) {
|
|
|
+// log.error("Succeed to put mapping");
|
|
|
+// }
|
|
|
+ CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ AtomicReference response = new AtomicReference<AcknowledgedResponse>();
|
|
|
+ client.indices().putMappingAsync(
|
|
|
+ request,
|
|
|
+ RequestOptions.DEFAULT,
|
|
|
+ new ActionListener<AcknowledgedResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(AcknowledgedResponse r) {
|
|
|
+ response.set(r);
|
|
|
+ latch.countDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ latch.countDown();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ latch.await(10, TimeUnit.SECONDS);
|
|
|
+ Assertions.assertThat(((AcknowledgedResponse) response.get()).isAcknowledged()).isTrue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Description: 判断某个index是否存在
|
|
|
+ *
|
|
|
+ * @param index index名
|
|
|
+ * @return boolean
|
|
|
+ * @author fanxb
|
|
|
+ * @date 2019/7/24 14:57
|
|
|
+ */
|
|
|
+ public boolean indexExist(String index) {
|
|
|
+ try {
|
|
|
+ GetIndexRequest request = new GetIndexRequest(index);
|
|
|
+ request.local(false);
|
|
|
+ request.humanReadable(true);
|
|
|
+ request.includeDefaults(false);
|
|
|
+ return client.indices().exists(request, RequestOptions.DEFAULT);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ElasticsearchException("获取索引 {" + index + "} 是否存在失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * delete elasticsearch index
|
|
|
+ *
|
|
|
+ * @param index elasticsearch index name
|
|
|
+ * @author fxbin
|
|
|
+ */
|
|
|
+ protected void deleteIndexRequest(String index) {
|
|
|
+ DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index);
|
|
|
+ try {
|
|
|
+ client.indices().delete(deleteIndexRequest, COMMON_OPTIONS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ElasticsearchException("删除索引 {" + index + "} 失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * exec updateRequest
|
|
|
+ *
|
|
|
+ * @param index elasticsearch index name
|
|
|
+ * @param id Document id
|
|
|
+ * @param object request object
|
|
|
+ * @author fxbin
|
|
|
+ */
|
|
|
+ protected void updateRequest(String index, String id, Object object) {
|
|
|
+ try {
|
|
|
+ UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtil.beanToMap(object), XContentType.JSON);
|
|
|
+ client.update(updateRequest, COMMON_OPTIONS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * exec deleteRequest
|
|
|
+ *
|
|
|
+ * @param index elasticsearch index name
|
|
|
+ * @param id Document id
|
|
|
+ * @author fxbin
|
|
|
+ */
|
|
|
+ protected void deleteRequest(String index, String id) {
|
|
|
+ try {
|
|
|
+ DeleteRequest deleteRequest = new DeleteRequest(index, id);
|
|
|
+ client.delete(deleteRequest, COMMON_OPTIONS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ElasticsearchException("删除索引 {" + index + "} 数据id {" + id + "} 失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * search all
|
|
|
+ *
|
|
|
+ * @param index elasticsearch index name
|
|
|
+ * @return {@link SearchResponse}
|
|
|
+ * @author fxbin
|
|
|
+ */
|
|
|
+ protected SearchResponse search(String index) {
|
|
|
+ SearchRequest searchRequest = new SearchRequest(index);
|
|
|
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
+ searchSourceBuilder.query(QueryBuilders.matchAllQuery());
|
|
|
+ searchRequest.source(searchSourceBuilder);
|
|
|
+ SearchResponse searchResponse = null;
|
|
|
+ try {
|
|
|
+ searchResponse = client.search(searchRequest, COMMON_OPTIONS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("es 搜索错误",e);
|
|
|
+ }
|
|
|
+ return searchResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(
|
|
|
" {\n" +
|
|
|
" \"properties\": {\n" +
|
|
|
" \"_class\": {\n" +
|
|
|
@@ -307,126 +659,6 @@ public abstract class BaseElasticsearchService {
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
|
- " }\n";
|
|
|
-
|
|
|
- PutMappingRequest request = new PutMappingRequest(index)
|
|
|
- .source(source, XContentType.JSON);
|
|
|
-// AcknowledgedResponse putMappingResponse = client.indices().putMapping(request,
|
|
|
-// RequestOptions.DEFAULT);
|
|
|
-//
|
|
|
-// boolean acknowledged = putMappingResponse.isAcknowledged();
|
|
|
-// if (acknowledged) {
|
|
|
-// log.error("Succeed to put mapping");
|
|
|
-// }
|
|
|
- CountDownLatch latch = new CountDownLatch(1);
|
|
|
- AtomicReference response = new AtomicReference<AcknowledgedResponse>();
|
|
|
- client.indices().putMappingAsync(
|
|
|
- request,
|
|
|
- RequestOptions.DEFAULT,
|
|
|
- new ActionListener<AcknowledgedResponse>() {
|
|
|
- @Override
|
|
|
- public void onResponse(AcknowledgedResponse r) {
|
|
|
- response.set(r);
|
|
|
- latch.countDown();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- latch.countDown();
|
|
|
- }
|
|
|
- });
|
|
|
- latch.await(10, TimeUnit.SECONDS);
|
|
|
- Assertions.assertThat(((AcknowledgedResponse) response.get()).isAcknowledged()).isTrue();
|
|
|
+ " }\n");
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * Description: 判断某个index是否存在
|
|
|
- *
|
|
|
- * @param index index名
|
|
|
- * @return boolean
|
|
|
- * @author fanxb
|
|
|
- * @date 2019/7/24 14:57
|
|
|
- */
|
|
|
- public boolean indexExist(String index) {
|
|
|
- try {
|
|
|
- GetIndexRequest request = new GetIndexRequest(index);
|
|
|
- request.local(false);
|
|
|
- request.humanReadable(true);
|
|
|
- request.includeDefaults(false);
|
|
|
- return client.indices().exists(request, RequestOptions.DEFAULT);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new ElasticsearchException("获取索引 {" + index + "} 是否存在失败:" + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * delete elasticsearch index
|
|
|
- *
|
|
|
- * @param index elasticsearch index name
|
|
|
- * @author fxbin
|
|
|
- */
|
|
|
- protected void deleteIndexRequest(String index) {
|
|
|
- DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index);
|
|
|
- try {
|
|
|
- client.indices().delete(deleteIndexRequest, COMMON_OPTIONS);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ElasticsearchException("删除索引 {" + index + "} 失败:" + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * exec updateRequest
|
|
|
- *
|
|
|
- * @param index elasticsearch index name
|
|
|
- * @param id Document id
|
|
|
- * @param object request object
|
|
|
- * @author fxbin
|
|
|
- */
|
|
|
- protected void updateRequest(String index, String id, Object object) {
|
|
|
- try {
|
|
|
- UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtil.beanToMap(object), XContentType.JSON);
|
|
|
- client.update(updateRequest, COMMON_OPTIONS);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * exec deleteRequest
|
|
|
- *
|
|
|
- * @param index elasticsearch index name
|
|
|
- * @param id Document id
|
|
|
- * @author fxbin
|
|
|
- */
|
|
|
- protected void deleteRequest(String index, String id) {
|
|
|
- try {
|
|
|
- DeleteRequest deleteRequest = new DeleteRequest(index, id);
|
|
|
- client.delete(deleteRequest, COMMON_OPTIONS);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ElasticsearchException("删除索引 {" + index + "} 数据id {" + id + "} 失败: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * search all
|
|
|
- *
|
|
|
- * @param index elasticsearch index name
|
|
|
- * @return {@link SearchResponse}
|
|
|
- * @author fxbin
|
|
|
- */
|
|
|
- protected SearchResponse search(String index) {
|
|
|
- SearchRequest searchRequest = new SearchRequest(index);
|
|
|
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
- searchSourceBuilder.query(QueryBuilders.matchAllQuery());
|
|
|
- searchRequest.source(searchSourceBuilder);
|
|
|
- SearchResponse searchResponse = null;
|
|
|
- try {
|
|
|
- searchResponse = client.search(searchRequest, COMMON_OPTIONS);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es 搜索错误",e);
|
|
|
- }
|
|
|
- return searchResponse;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|