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 getAllTemplates(){
        GetIndexTemplatesRequest templatesRequest = new GetIndexTemplatesRequest();
        GetIndexTemplatesResponse templatesResponse = null;
        List 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 list = getAllTemplates();
        if(list.isEmpty()){
            return false;
        }
        List names = list.stream().map(IndexTemplateMetaData::name).collect(Collecto

你可能感兴趣的:(elasticsearch,大数据,big,data)