Elasticsearch 修改字段类型,索引映射关系,模板属性 Java实现

Java Api实现:

/**
 * 对索引/模板的查询/更新操作
 */
@Slf4j
@Service
public class ESMappingService {

    @Resource
    protected RestHighLevelClient client;

    /**
     * 根据索引名(可模糊)-查询相关索引名
     * @return String[]
     */
    public String[] getIndices(String index){
        GetIndexRequest request = new GetIndexRequest(index);
        //允许不存在的索引
        IndicesOptions indicesOptions = IndicesOptions.fromOptions(true, true, true, false);
        request.indicesOptions(indicesOptions);
        GetIndexResponse response = null;
        String[] indices = new String[]{};
        try {
            response = client.indices().get(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error(" ESMappingService getIndices Cause:{},Message:{}", e.getCause(),e.getMessage());
        }
        if (response != null) {
            indices = response.getIndices();
        }
        return indices;
    }

    /**
     * 查询所有的索引名
     * @return String[]
     */
    public String[] getAllIndices(){
        GetIndexRequest request = new GetIndexRequest();
        GetIndexResponse response = null;
        String[] indices = new String[]{};
        try {
            response = client.indices().get(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error(" ESMappingService getAllIndices Cause:{},Message:{}", e.getCause(),e.getMessage());
        }
        if (response != null) {
            indices = response.getIndices();
        }
        return indices;
    }

    /**
     * 查询现有的所有模板信息
     * @return List
     */
    public List<IndexTemplateMetaData> getAllTemplates(){
        GetIndexTemplatesRequest templatesRequest = new GetIndexTemplatesRequest();
        GetIndexTemplatesResponse templatesResponse = null;
        List<IndexTemplateMetaData> list = new ArrayList<>();
        try {
            templatesResponse = client.indices().getIndexTemplate(templatesRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error(" ESCheckMappingService getAllTemplates query error ", e);
        }
        if(templatesResponse == null){
            return list;
        }
        return templatesResponse.getIndexTemplates();
    }

    /**
     * 判断模板是否已经存在
     * @param template 模板名-需明确
     * @return boolean
     */
    public boolean isTemplateExist(String template){
        List<IndexTemplateMetaData> list = getAllTemplates();
        if(list.isEmpty()){
            return false;
        }
        List<String> names = list.stream().map(IndexTemplateMetaData::name).collect(Collecto
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值