从ElasticSearch中查询课程信息。
添加课程索引
接口信息如下
课程文档参数
CourseIndex@Data public class CourseIndex implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull(message = "课程id为空") private Long id;
private Long companyId;
private String companyName;
private String name;
private String users;
private String tags;
private String mt;
private String mtName;
private String st;
private String stName;
private String grade;
private String teachmode;
private String pic;
private String description;
@JSONField(format="yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createDate;
private String status;
private String remark;
private String charge;
private Float price;
private Float originalPrice;
private Integer validDays;
}
|
定义Service
新建CourseSearchService
接口,定义课程搜索接口
IndexService
public interface IndexService {
public Boolean addCourseIndex(String indexName, String id, Object object);
public Boolean updateCourseIndex(String indexName, String id, Object object);
public Boolean deleteCourseIndex(String indexName, String id);
}
|
实现该方法,创建其实现类IndexServiceImpl
IndexServiceImpl@Slf4j @Service public class IndexServiceImpl implements IndexService {
private final RestHighLevelClient restHighLevelClient;
public IndexServiceImpl(RestHighLevelClient restHighLevelClient) { this.restHighLevelClient = restHighLevelClient; }
@Override public Boolean addCourseIndex(String indexName, String id, Object object) { String jsonString = JSON.toJSONString(object); IndexRequest indexRequest = new IndexRequest(indexName).id(id); indexRequest.source(jsonString, XContentType.JSON); IndexResponse indexResponse = null; try { indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); } catch (IOException e) { log.error("添加索引出错: ", e); throw new BizException("添加索引出错"); } String name = indexResponse.getResult().name(); return name.equalsIgnoreCase("created") || name.equalsIgnoreCase("updated"); }
@Override public Boolean updateCourseIndex(String indexName, String id, Object object) { String jsonString = JSON.toJSONString(object); UpdateRequest updateRequest = new UpdateRequest(indexName, id); updateRequest.doc(jsonString, XContentType.JSON); UpdateResponse updateResponse = null; try { updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); } catch (IOException e) { log.error("更新索引出错: ",e); throw new BizException("更新索引出错"); } DocWriteResponse.Result result = updateResponse.getResult(); return result.name().equalsIgnoreCase("updated"); }
@Override public Boolean deleteCourseIndex(String indexName, String id) { DeleteRequest deleteRequest = new DeleteRequest(indexName,id); DeleteResponse deleteResponse = null; try { deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); } catch (IOException e) { log.error("删除索引出错: ",e); throw new BizException("删除索引出错"); } DocWriteResponse.Result result = deleteResponse.getResult(); return result.name().equalsIgnoreCase("deleted"); } }
|
定义Controller
CourseIndexController@Api(value = "课程信息索引接口", tags = "课程信息索引接口") @RestController @RequestMapping("/index") @RefreshScope public class CourseIndexController {
@Value("${elasticsearch.course.index}") private String courseIndexStore; private final IndexService indexService;
public CourseIndexController(IndexService indexService) { this.indexService = indexService; }
@ApiOperation("添加课程索引") @PostMapping("course") public Boolean add(@RequestBody @Validated CourseIndex courseIndex) { Long id = courseIndex.getId(); Boolean result = indexService.addCourseIndex(courseIndexStore, String.valueOf(id), courseIndex); if (!result) { throw new BizException("添加课程索引失败"); } return true; } }
|