1. 常见问题

1.1. 批量同步数据?

通过读取数据再迭代调用Service进行保存操作

@Autowired
private IESContentService esContentService;

/**
 * 同步es文章
 * @return
 */
@PostMapping("/sync")
@RequiresPermissions("es:sync")
@ResponseBody
public ResultData sync() {
    // 没有索引需要先去创建索引
    if (!esService.existDoc(esService.getBeanClass())){
        return ResultData.build().error("当前不存在es索引,请先创建es索引");
    }
    // 获取栏目列表
    LambdaQueryWrapper<CategoryEntity> categoryWrapper = new LambdaQueryWrapper<>();
    categoryWrapper.eq(CategoryEntity::getCategoryType, CategoryTypeEnum.LIST.toString());
    List<CategoryEntity> categoryList = categoryBiz.list(categoryWrapper);

    for (CategoryEntity category : categoryList) {
        // 获取文章列表
        LambdaQueryWrapper<ContentEntity> contentWrapper = new LambdaQueryWrapper<>();
        contentWrapper.eq(ContentEntity::getCategoryId, category.getId());
        List<ContentEntity> contentList = contentBiz.list(contentWrapper);
        boolean hasModel = false;
        ModelEntity model = null;
        if (category.getMdiyModelId() != null) {
            hasModel = true;
            // 获取模型实体
            model = modelBiz.getById(category.getMdiyModelId());
        }
        for (ContentEntity content : contentList) {
            ESContentBean esContentBean = new ESContentBean();
            ESContentBeanUtil.fixESContentBean(esContentBean, content, category);

            esContentBean.setId(content.getId());
            //将需要同步到es库的字段逐个赋值
            esContentBean.setTitle(content.getContentTitle());
            esContentBean.setContent(content.getContentDetails());
            esContentBean.setAuthor(content.getContentAuthor());
            esContentBean.setDate(content.getContentDatetime());
            esContentBean.setTypeId(content.getCategoryId());
            esContentBean.setDescrip(content.getContentDescription());
            esContentBean.setSort(content.getContentSort());
            esContentBean.setLitPic(content.getContentImg());
            esContentBean.setFlag(content.getContentType());
            esContentBean.setUrl(category.getCategoryPath()


            try {
                esContentService.save(esContentBean);
            }catch (DataAccessResourceFailureException e) {
                return ResultData.build().error("未找到当前ES信息,请检查当前ES链接是否正常");
            }

        }
    }
    return ResultData.build().success("全部同步完成!");
}

[!tip]
service保存时,如果已经存在的数据es则会进行更新操作

1.2. 怎么快速查看es?

通过http://ip:5601/app/dev_tools#/console,常用指令

#索引名:cms
#查询索引内所有数据
GET cms/_search?pretty

#查询索引内数据数量
GET cms/_count?pretty

#查询es索引mapping
GET cms/_mapping

#删除索引
DELETE cms

#查看分词内容
GET cms/_analyze
{
  "field": "title",
  "text": "快速查看es"
}

1.3. 提示createTime错误?

直接同步数据没有设定字段的数据结构从而给定了默认的text字段类型及默认结构,需先创建es索引再进行同步。 若已存在索引需删除重新创建

[!tip] 有问题可以通过评论方式提交,如果没有看到评论列表请尝试刷新页面

Copyright © mingsoft.net 2012-2022 all right reserved,powered by Gitbook该文件修订时间: 2023-02-25 10:14:41

results matching ""

    No results matching ""

    results matching ""

      No results matching ""