|
@@ -39,21 +39,16 @@ public class DocumentServiceImpl implements DocumentService {
|
|
|
* 增加文档信息
|
|
|
*
|
|
|
* @param indexName 索引名
|
|
|
- * @param type 文档类型
|
|
|
* @param keyId 主键
|
|
|
* @param data json数据
|
|
|
* @return 增加文档信息状态
|
|
|
* @throws IOException 异常信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public RestStatus addDocument(String indexName, String type,String keyId, String data) throws IOException {
|
|
|
- /*1.默认类型为_doc*/
|
|
|
- if (StringUtils.isBlank(type)) {
|
|
|
- type = "_doc";
|
|
|
- }
|
|
|
+ public RestStatus addDocument(String indexName, String keyId, String data) throws IOException {
|
|
|
/*2.将对象转为json*/
|
|
|
/*3.创建索引请求对象*/
|
|
|
- IndexRequest indexRequest = new IndexRequest(indexName, type).id(keyId).source(data, XContentType.JSON);
|
|
|
+ IndexRequest indexRequest = new IndexRequest(indexName).id(keyId).source(data, XContentType.JSON);
|
|
|
/*4.执行增加文档*/
|
|
|
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
|
|
|
|
@@ -64,19 +59,14 @@ public class DocumentServiceImpl implements DocumentService {
|
|
|
* 获取文档信息
|
|
|
*
|
|
|
* @param indexName 索引名
|
|
|
- * @param type 文档类型
|
|
|
* @param id 文档ID
|
|
|
* @return 商品数据
|
|
|
* @throws Exception 异常信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public String getDocument(String indexName, String type, String id) throws Exception {
|
|
|
- /*1.默认类型为_doc*/
|
|
|
- if (StringUtils.isBlank(type)) {
|
|
|
- type = "_doc";
|
|
|
- }
|
|
|
+ public String getDocument(String indexName, String id) throws Exception {
|
|
|
/*2.创建获取请求对象*/
|
|
|
- GetRequest getRequest = new GetRequest(indexName, type, id);
|
|
|
+ GetRequest getRequest = new GetRequest(indexName, id);
|
|
|
GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
|
|
|
|
|
|
return response.getSourceAsString();
|
|
@@ -86,20 +76,15 @@ public class DocumentServiceImpl implements DocumentService {
|
|
|
* 更新文档信息
|
|
|
*
|
|
|
* @param indexName 索引名
|
|
|
- * @param type 文档类型
|
|
|
* @param data 数据
|
|
|
* @return 更新文档信息状态
|
|
|
* @throws IOException 异常信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public RestStatus updateDocument(String indexName, String type, String id ,String data) throws IOException {
|
|
|
- /*1.默认类型为_doc*/
|
|
|
- if (StringUtils.isBlank(type)) {
|
|
|
- type = "_doc";
|
|
|
- }
|
|
|
+ public RestStatus updateDocument(String indexName, String id ,String data) throws IOException {
|
|
|
|
|
|
/*2.创建索引请求对象*/
|
|
|
- UpdateRequest updateRequest = new UpdateRequest(indexName, type, id);
|
|
|
+ UpdateRequest updateRequest = new UpdateRequest(indexName, id);
|
|
|
/*3.设置更新文档内容*/
|
|
|
updateRequest.doc(data, XContentType.JSON);
|
|
|
/*4.执行更新文档*/
|
|
@@ -118,14 +103,9 @@ public class DocumentServiceImpl implements DocumentService {
|
|
|
* @throws IOException 异常信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public RestStatus deleteDocument(String indexName, String type, String id) throws IOException {
|
|
|
- /*1.默认类型为_doc*/
|
|
|
- if (StringUtils.isBlank(type)) {
|
|
|
- type = "_doc";
|
|
|
- }
|
|
|
-
|
|
|
+ public RestStatus deleteDocument(String indexName, String id) throws IOException {
|
|
|
/*2.创建删除请求对象*/
|
|
|
- DeleteRequest deleteRequest = new DeleteRequest(indexName, type, id);
|
|
|
+ DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
|
|
|
/*3.执行删除文档*/
|
|
|
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
|
|
|
|
@@ -149,8 +129,8 @@ public class DocumentServiceImpl implements DocumentService {
|
|
|
BulkRequest bulkRequest = new BulkRequest();
|
|
|
for (JSONObject data : dataList) {
|
|
|
//将goods对象转换为json字符串
|
|
|
- IndexRequest indexRequest = new IndexRequest(index, "_doc");
|
|
|
- indexRequest.id(data.getString("id")).source(data, XContentType.JSON);
|
|
|
+ IndexRequest indexRequest = new IndexRequest(index);
|
|
|
+ indexRequest.id(data.getString("id")).source(JSON.toJSONString(data), XContentType.JSON);
|
|
|
bulkRequest.add(indexRequest);
|
|
|
}
|
|
|
|