实现删除和更新数据接口
This commit is contained in:
@@ -32,6 +32,7 @@ public class AddBookController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入数据
|
* 插入数据
|
||||||
|
*
|
||||||
* @param title
|
* @param title
|
||||||
* @param author
|
* @param author
|
||||||
* @param wordCount
|
* @param wordCount
|
||||||
@@ -43,7 +44,7 @@ public class AddBookController {
|
|||||||
@RequestParam(name = "author") String author,
|
@RequestParam(name = "author") String author,
|
||||||
@RequestParam(name = "word_count") int wordCount,
|
@RequestParam(name = "word_count") int wordCount,
|
||||||
@RequestParam(name = "publish_date")
|
@RequestParam(name = "publish_date")
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDate) {
|
@DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDate) {
|
||||||
String type = "novel";
|
String type = "novel";
|
||||||
|
|
||||||
// 格式化日期
|
// 格式化日期
|
||||||
@@ -53,20 +54,20 @@ public class AddBookController {
|
|||||||
try {
|
try {
|
||||||
// 用自带的构造文档
|
// 用自带的构造文档
|
||||||
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
|
XContentBuilder content = XContentFactory.jsonBuilder().startObject()
|
||||||
.field("title",title)
|
.field("title", title)
|
||||||
.field("author",author)
|
.field("author", author)
|
||||||
.field("word_count",wordCount)
|
.field("word_count", wordCount)
|
||||||
.field("publish_date",dateStr)
|
.field("publish_date", dateStr)
|
||||||
.field("type",type)
|
.field("type", type)
|
||||||
.endObject();
|
.endObject();
|
||||||
|
|
||||||
IndexResponse response = this.client.prepareIndex("book",type).setSource(content).get();
|
IndexResponse response = this.client.prepareIndex("book", type).setSource(content).get();
|
||||||
|
|
||||||
// 返回结果带上新增的ID
|
// 返回结果带上新增的ID
|
||||||
return new ResponseEntity(response.getId(), HttpStatus.OK);
|
return new ResponseEntity(response.getId(), HttpStatus.OK);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
log.error("【title】"+title+"【author】"+author+"【wordCount】"+wordCount+"【publishDate】"+publishDate);
|
log.error("【title】" + title + "【author】" + author + "【wordCount】" + wordCount + "【publishDate】" + publishDate);
|
||||||
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package top.fjy8018.elasticsearch.Controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-05-19 22:03
|
||||||
|
*/
|
||||||
|
@RequestMapping("/delete/book")
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class DeleteBookController {
|
||||||
|
|
||||||
|
private static String INDEX = "book";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TransportClient client;
|
||||||
|
|
||||||
|
@DeleteMapping("/novel")
|
||||||
|
public ResponseEntity delete(@RequestParam(name = "id") String id) {
|
||||||
|
if (id == null) {
|
||||||
|
log.error("【删除失败】:传入ID为空");
|
||||||
|
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeleteResponse response = this.client.prepareDelete(INDEX, "novel", id).get();
|
||||||
|
|
||||||
|
log.info("【response】" + response.toString());
|
||||||
|
|
||||||
|
if (response.getId().equals(id)) {
|
||||||
|
return new ResponseEntity(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
log.error("【删除失败】ID:" + id + response.toString());
|
||||||
|
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -25,18 +25,19 @@ public class GetBookController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID查询结果
|
* 根据ID查询结果
|
||||||
|
*
|
||||||
* @param id
|
* @param id
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GetMapping("/novel")
|
@GetMapping("/novel")
|
||||||
public ResponseEntity get(@RequestParam(name = "id") String id){
|
public ResponseEntity get(@RequestParam(name = "id") String id) {
|
||||||
if (id==null){
|
if (id == null) {
|
||||||
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
GetResponse response = this.client.prepareGet("book","novel",id).get();
|
GetResponse response = this.client.prepareGet("book", "novel", id).get();
|
||||||
|
|
||||||
if (!response.isExists()){
|
if (!response.isExists()) {
|
||||||
log.error("【查询为空】ID="+id);
|
log.error("【查询为空】ID=" + id);
|
||||||
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
return new ResponseEntity(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package top.fjy8018.elasticsearch.Controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.elasticsearch.action.update.UpdateRequest;
|
||||||
|
import org.elasticsearch.action.update.UpdateResponse;
|
||||||
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-05-19 22:22
|
||||||
|
*/
|
||||||
|
@RequestMapping("/update/book")
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class UpdateBookController {
|
||||||
|
|
||||||
|
private static String INDEX = "book";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TransportClient client;
|
||||||
|
|
||||||
|
@PutMapping("/novel")
|
||||||
|
public ResponseEntity update(@RequestParam(name = "id") String id,
|
||||||
|
@RequestParam(name = "title", required = false) String title,
|
||||||
|
@RequestParam(name = "author", required = false) String author,
|
||||||
|
@RequestParam(name = "word_count", required = false) int wordCount,
|
||||||
|
@RequestParam(name = "publish_date", required = false)
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDate) {
|
||||||
|
String type = "novel";
|
||||||
|
|
||||||
|
// 构造更新请求
|
||||||
|
UpdateRequest request = new UpdateRequest(INDEX, "novel", id);
|
||||||
|
|
||||||
|
UpdateResponse response = new UpdateResponse();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 构造更新请求体
|
||||||
|
XContentBuilder content = XContentFactory.jsonBuilder().startObject().field("type", type);
|
||||||
|
|
||||||
|
if (title != null) {
|
||||||
|
content.field("title", title);
|
||||||
|
}
|
||||||
|
if (author != null) {
|
||||||
|
content.field("author", author);
|
||||||
|
}
|
||||||
|
if (wordCount != 0) {
|
||||||
|
content.field("word_count", wordCount);
|
||||||
|
}
|
||||||
|
if (publishDate != null) {
|
||||||
|
// 格式化日期
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String dateStr = sdf.format(publishDate);
|
||||||
|
content.field("publish_date", dateStr);
|
||||||
|
}
|
||||||
|
content.endObject();
|
||||||
|
|
||||||
|
request.doc(content);
|
||||||
|
|
||||||
|
response = this.client.update(request).get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("【title】" + title + "【author】" + author + "【wordCount】" + wordCount + "【publishDate】" + publishDate);
|
||||||
|
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.getId().equals(id)) {
|
||||||
|
return new ResponseEntity(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.error("【response】" + response.toString() + "【title】" + title + "【author】" + author + "【wordCount】" + wordCount + "【publishDate】" + publishDate);
|
||||||
|
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user