SpringBoot 통합 ElasticSearch 삭제 확인 완료(6)
8213 단어 ElasticSearch
org.springframework.boot
spring-boot-starter-data-elasticsearch
2.0.2.RELEASE
org.springframework.boot
spring-boot-starter-web
2.0.2.RELEASE
org.springframework.boot
spring-boot-starter-test
2.0.2.RELEASE
test
2.연결 구성
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.net.InetAddress;
/**
* Created by ${ligh} on 2018/12/27 10:08
*/
@Configuration // ,
public class MyConfig {
@Bean
public TransportClient client() throws Exception {
// ip
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("localhost"),
9300
);
Settings settings = Settings.builder()
.put("cluster.name","wali")
.build();
//
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
//
// client.addTransportAddress();
return client;
}
}
셋.삭제 수정 작업 인터페이스
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
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.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
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.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
@RestController
public class HelloController {
@Autowired
private TransportClient client;
/**
*
*
* @param id
* @return
*/
@GetMapping("/get/book/novel")
@ResponseBody
public ResponseEntity get(@RequestParam(name = "id",defaultValue = "") String id){
if(id.isEmpty()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
GetResponse result = this.client.prepareGet("book", "novel", id)
.get();
if(!result.isExists()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(result.getSource(),HttpStatus.OK);
}
/**
*
*
* @param title
* @param author
* @param wordCount
* @param publishDate
* @return
*/
@PostMapping("add/book/novel")
@ResponseBody
public ResponseEntity add(
@RequestParam(name = "title") String title,
@RequestParam(name = "author") String author,
@RequestParam(name = "word_count") int wordCount,
@RequestParam(name = "publish_date")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Date publishDate
){
try{
XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("title",title)
.field("author",author)
.field("word_count",wordCount)
.field("publish_date",publishDate.getTime())
.endObject();
IndexResponse result = this.client.prepareIndex("book","novel")
.setSource(contentBuilder)
.get();
return new ResponseEntity(result.getId(),HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
*
*
* @param id
* @return
*/
@DeleteMapping("delete/book/novel")
@ResponseBody
public ResponseEntity delete(@RequestParam(name = "id") String id){
DeleteResponse result = this.client.prepareDelete("book", "novel", id).get();
return new ResponseEntity(result.getResult().toString(),HttpStatus.OK);
}
/**
*
*
* @param id
* @param title
* @param author
* @return
*/
@PutMapping("update/book/novel")
@ResponseBody
public ResponseEntity update(
@RequestParam(name = "id") String id,
@RequestParam(name = "title",required = false) String title,
@RequestParam(name = "author",required = false) String author
){
UpdateRequest update = new UpdateRequest("book", "novel", id);
try{
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject();
if(title != null){
builder.field("title",title);
}
if(author != null){
builder.field("author",author);
}
builder.endObject();
update.doc(builder);
}catch (Exception e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
try{
UpdateResponse result = this.client.update(update).get();
return new ResponseEntity(result.getResult().toString(),HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
*
* ( )
* @param author
* @param title
* @param gtWordCount
* @param ltWordCount
* @return
*/
@PostMapping("query/book/novel")
@ResponseBody
public ResponseEntity query(
@RequestParam(name = "author",required = false) String author,
@RequestParam(name = "title",required = false) String title,
@RequestParam(name = "gt_word_count",defaultValue = "0") int gtWordCount,
@RequestParam(name = "lt_word_count",required = false) Integer ltWordCount
){
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(author != null){
boolQuery.must(QueryBuilders.matchQuery("author",author));
}
if(title != null){
boolQuery.must(QueryBuilders.matchQuery("title",title));
}
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("word_count")
.from(gtWordCount);
if(ltWordCount != null && ltWordCount > 0){
rangeQuery.to(ltWordCount);
}
boolQuery.filter(rangeQuery);
SearchRequestBuilder builder = this.client.prepareSearch("book")
.setTypes("novel")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(boolQuery)
.setFrom(0)
.setSize(10);
System.out.println(builder);
SearchResponse response = builder.get();
ArrayList
Springboot 프로젝트에 ElasticSearch 검색 엔진을 통합했습니다.후기에는 인터페이스에 대한 봉인을 업데이트하여 일반적인 인터페이스처럼 할 것이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
spring-data-elasticsearch 페이지 조회부록: 1. 이름에서 알 수 있듯이QueryBuilder는 검색 조건, 필터 조건을 구축하는 데 사용되고 SortBuilder는 정렬을 구축하는 데 사용된다. 예를 들어 우리는 어느 위치에서 100미터 범위 내의 모...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.