Springboot2.0 통합 elasticsearch CRUD 작업

6013 단어
springboot과elasticsearchd의 통합 네트워크에는 많은 예가 있습니다. 예를 들어elasticsearch가 너무 늙었거나springboot 버전과 일치하지 않는 오류가 발생했거나:
org.elasticsearch.transport.NodeDisconnectedException: [][127.0.0.1:9300][cluster:monitor/nodes/liveness] disconnected
그래서 자신의 마이크로서비스 형식은 스프링부트 2.0과elasticsearch5.6.8의 통합을 바탕으로 기본적인 CRUD를 정리했고 핵심 패키지와 클래스는 아래에 열거했다.
의존 패키지

  org.springframework.boot
  spring-boot-starter-parent
  
  2.0.1.RELEASE
   

    org.springframework.boot
    spring-boot-starter-data-elasticsearch


    org.springframework.boot
    spring-boot-starter-web

구성 파일:
server:
  port: 8085
spring:
  application:
    name: es-search
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
      cluster-name: elasticsearch
      repositories:
        enabled: true
public interface SearchServer {

    @RequestMapping(value = "/goodslist")
    Response getList(@RequestParam("fieldName") String fieldName,@RequestParam("query") String query);

    @RequestMapping(value = "/goodssave")
    Response save(@RequestBody GoodsDTO goodsDTO);

    @RequestMapping(value = "/goodspage")
    Response page(@RequestParam(value = "pageNum",required = false) Integer pageNum,@RequestParam(value = "pageSize",required = false) Integer pageSize,@RequestParam("fieldName") String fieldName,@RequestParam("query") String query);

    @RequestMapping(value = "/goodsdel")
    Response delete(@RequestBody GoodsDTO goodsDTO);

}
public interface BaseService {

    List getList(int type,String fieldName,String query);

    T save(T t);

    Page page(Integer pageNum, Integer pageSize,int type,String fieldName, String query);

    void delete(T t);
}
public abstract class BaseSearchService implements BaseService{

    protected abstract BaseRepository getBaseRepository();

    /**
     *
     * @param type   1: ,2: 
     * @param fieldName  
     * @param query  
     * @return
     */
    public List getList(int type,String fieldName,String query) {

        if(type != SearchType.ac_type && type != SearchType.ob_type){
            throw new IllegalArgumentException(" ");
        }
        // 
        if(type == SearchType.ob_type){
            query=matchString(query);
        }
        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(fieldName, query);
        Iterable search = getBaseRepository().search(queryBuilder);
        List list=new ArrayList<>();
        Iterator iterator = search.iterator();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        return list;
    }

    /**
     *  ( )
     * @param t
     * @return
     */
    public T save(T t){
        return (T) getBaseRepository().save(t);
    }

    /**
     *  
     * @param pageNum  
     * @param pageSize
     * @param type   1: ,2: 
     * @param fieldName  
     * @param query  
     * @return
     */
    public Page page(Integer pageNum, Integer pageSize,int type,String fieldName, String query) {

        if(type != SearchType.ac_type && type != SearchType.ob_type){
            throw new IllegalArgumentException(" ");
        }
        if(pageNum == null) pageNum = 0;
        if(pageSize == null) pageSize=20;
        // 
        if(type == SearchType.ob_type){
            query=matchString(query);
        }
        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(fieldName, query);
        return getBaseRepository().search(queryBuilder, PageRequest.of(pageNum,pageSize));
    }

    /**
     *  
     * @param t
     */
    public void delete(T t){
        getBaseRepository().delete(t);
    }

    protected String matchString(String str){
        return "*".concat(str).concat("*");
    }
}
@RestController
@RequestMapping("search/goods")
public class GoodsController implements SearchServer{

    @Autowired
    private GoodsService goodsService;

    @Override
    public Response getList(String fieldName,String query){
        List list = goodsService.getList(SearchType.ob_type, fieldName, query);
        return Response.buildSuccessResponse().setData(list);
    }

    @Override
    public Response save(GoodsDTO goodsDTO) {
        GoodsInfo goodsInfo=new GoodsInfo();
        BeanUtils.copyProperties(goodsDTO,goodsInfo);
        goodsService.save(goodsInfo);
        return Response.buildSuccessResponse();
    }

    @Override
    public Response page(Integer pageNum,Integer pageSize,String fieldName, String query){
        Page page = goodsService.page(pageNum, pageSize, SearchType.ob_type, fieldName, query);
        return Response.buildSuccessResponse().setData(new PageResult<>(page.getTotalElements(),page.getContent()));
    }

    @Override
    public Response delete(GoodsDTO goodsDTO) {
        GoodsInfo goodsInfo=new GoodsInfo();
        BeanUtils.copyProperties(goodsDTO,goodsInfo);
        goodsService.delete(goodsInfo);
        return Response.buildSuccessResponse();
    }

}
@Service
public class GoodsServiceImpl extends BaseSearchService implements GoodsService {

    @Autowired
    private GoodsRepository goodsRepository;

    @Override
    public BaseRepository getBaseRepository() {
        return goodsRepository;
    }

}
@Component
public interface GoodsRepository extends BaseRepository {
}

좋은 웹페이지 즐겨찾기