package top.fjy8018.elasticsearch.Controller; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.action.get.GetResponse; 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.GetMapping; 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 16:31 */ @RestController @RequestMapping("/get/book") @Slf4j public class GetBookController { @Autowired private TransportClient client; /** * 根据ID查询结果 * @param id * @return */ @GetMapping("/novel") public ResponseEntity get(@RequestParam(name = "id") String id){ if (id==null){ return new ResponseEntity(HttpStatus.NOT_FOUND); } GetResponse response = this.client.prepareGet("book","novel",id).get(); if (!response.isExists()){ log.error("【查询为空】ID="+id); return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(response.getSource(), HttpStatus.OK); } }