028-从零搭建微服务-搜索服务(二)

写在最前

如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。

源码地址(后端):https://gitee.com/csps/mingyue

源码地址(前端):https://gitee.com/csps/mingyue-ui

文档地址:https://gitee.com/csps/mingyue/wikis

搜服服务

更多用法可以参考 Easy-Es 官方文档:https://www.easy-es.cn/pages/04414d/

精确查询

@GetMapping("/accurate")
@Operation(summary = "精确查询", parameters = { @Parameter(name = "key", description = "标题", required = true) })
public Document accurate(String title) {
  LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  wrapper.eq(Document::getTitle, title);
  return documentMapper.selectOne(wrapper);
}

测试接口

curl -X 'GET' \
  'http://mingyue-gateway:7100/search/crudDemo/accurate?title=%E7%99%BB%E9%AB%98' \
  -H 'accept: */*'

接口返回

{
  "code": 200,
  "msg": "操作成功",
  "data": {
    "id": "1",
    "title": "登高",
    "content": "风急天高猿啸哀,渚清沙白鸟飞回。"
  }
}

模糊查询

@GetMapping("/fuzzy")
@Operation(summary = "模糊查询", parameters = { @Parameter(name = "key", description = "搜索关键字", required = true) })
public List<Document> fuzzy(String key) {
  LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  wrapper.like(Document::getTitle, key);
  return documentMapper.selectList(wrapper);
}

测试接口

curl -X 'GET' \
  'http://mingyue-gateway:7100/search/crudDemo/fuzzy?key=%E9%AB%98' \
  -H 'accept: */*' \
  -H 'Authorization: 6H1mlA91zFRa5yEpIl2b2mnCjbG5B44f'

接口返回

{
  "code": 200,
  "msg": "操作成功",
  "data": [
    {
      "id": "1",
      "title": "登高",
      "content": "风急天高猿啸哀,渚清沙白鸟飞回。"
    }
  ]
}

添加

@PostMapping("/insert")
public Integer insert(@RequestBody Document document) {
    return documentMapper.insert(document);
}

测试接口

curl -X 'POST' \
  'http://mingyue-gateway:7100/search/crudDemo/add' \
  -H 'accept: */*' \
  -H 'Authorization: 6H1mlA91zFRa5yEpIl2b2mnCjbG5B44f' \
  -H 'Content-Type: application/json' \
  -d '{
  "id": "2",
  "title": "草",
  "content": "离离原上草,一岁一枯荣。野火烧不尽,春风吹又生。"
}'

接口返回

{
  "code": 200,
  "msg": "操作成功",
  "data": true
}

更新

@PutMapping("/update")
public R<Boolean> update(@RequestBody Document document) {
  return R.ok(documentMapper.updateById(document) > 0);
}

测试接口

curl -X 'PUT' \
  'http://mingyue-gateway:7100/search/crudDemo/update' \
  -H 'accept: */*' \
  -H 'Authorization: 6H1mlA91zFRa5yEpIl2b2mnCjbG5B44f' \
  -H 'Content-Type: application/json' \
  -d '{
  "id": "1",
  "title": "登高",
  "content": "风急天高猿啸哀,渚清沙白鸟飞回。无边落木萧萧下,不尽长江滚滚来。"
}'

接口返回

{
  "code": 200,
  "msg": "操作成功",
  "data": true
}

删除

@DeleteMapping("/delete/{id}")
@Operation(summary = "删除文档",
    parameters = { @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true) })
public R<Integer> delete(@PathVariable String id) {
  return R.ok(documentMapper.deleteById(id));
}

测试接口

curl -X 'DELETE' \
  'http://mingyue-gateway:7100/search/crudDemo/delete/2' \
  -H 'accept: */*' \
  -H 'Authorization: 6H1mlA91zFRa5yEpIl2b2mnCjbG5B44f'

接口返回

{
  "code": 200,
  "msg": "操作成功",
  "data": true
}

小结

搜素服务基础功能已经完成,列一下未来搜索服务的功能吧:

  1. 数据同步,同步业务数据库中数据;
  2. 分页查询示例;
  3. 聚合、排序查询示例;
  4. 分词&模糊匹配示例;
  5. 。。。

搜素服务暂时先停一哈,准备接下来给 mingyue 集成一个消息队列,敬请期待吧~~~

你可能感兴趣的:(MingYue微服务,微服务,架构,云原生)