package com.example.es.controller; import cn.hutool.core.bean.BeanUtil; import com.example.es.document.ESGoodsDocument; import com.example.es.service.EsSearchService; import com.example.sys.mapper.ESGoodsMapper; import com.example.sys.entity.ESGoodsEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * elasticsearch 搜索 * * @author zhoudong * @version 0.1 * @date 2018/12/13 15:09 */ @RestController public class EsSearchController { private Logger log = LoggerFactory.getLogger(getClass()); @Resource private EsSearchService esSearchService; @Resource private ESGoodsMapper ESGoodsMapper; /** * 初始化数据 * * @return */ @RequestMapping("init") public String init() { List rollCallCurriculumEntities = ESGoodsMapper.selectList(null); for (ESGoodsEntity ESGoodsEntity : rollCallCurriculumEntities) { ESGoodsDocument ESGoodsDocument = new ESGoodsDocument(); BeanUtil.copyProperties(ESGoodsEntity, ESGoodsDocument); esSearchService.save(ESGoodsDocument); } return "success"; } /** * 新增 / 修改索引 * * @return */ @RequestMapping("save") public String add(@RequestBody ESGoodsDocument ESGoodsDocument) { esSearchService.save(ESGoodsDocument); return "success"; } /** * 删除索引 * * @return */ @RequestMapping("delete/{id}") public String delete(@PathVariable String id) { esSearchService.delete(id); return "success"; } /** * 清空索引 * * @return */ @RequestMapping("delete_all") public String deleteAll() { esSearchService.deleteAll(); return "success"; } /** * 根据ID获取 * * @return */ @RequestMapping("get/{id}") public ESGoodsDocument getById(@PathVariable String id) { return esSearchService.getById(id); } /** * 根据获取全部 * * @return */ @RequestMapping("get_all") public List getAll() { return esSearchService.getAll(); } /** * 搜索 * * @param keyword * @return */ @RequestMapping("query/{keyword}") public List query(@PathVariable String keyword) { return esSearchService.query(keyword, ESGoodsDocument.class); } }