ElasticSearch7索引管理

本文介绍了ElasticSearch7中如何进行索引管理,包括索引映射的创建,数据清空,以及测试数据的添加。通过代码示例和yml配置文件展示了具体操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

  • 索引映射文件示例
  • 索引mapping创建
  • 索引数据清空
  • 测试数据添加

代码

import com.csg.dse.es.client.OkHttpUtil;
import com.csg.dse.es.config.EsComponUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.yaml.snakeyaml.Yaml;
import test.util.EsClientMe;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: tianya
 * @Date: 2019/8/06
 * @Description:创建es索引
 */
public class EsIndexCreate {

    String index = "";
    String urlEsBase = "http://192.168.10.124:9200/";
    String urlEsQuery = "http://192.168.10.124:9200/_xpack/sql?format=json";


    @Test
    public void createWarnlog() {
        createIndex("test20190821");
    }


    private void createIndex(String index) {
        final URL resource = EsIndexCreate.class.getResource("/config/esIndexMapping.yml");
        try (FileInputStream inputStream = new FileInputStream(new File(resource.getPath()))) {
            Map<String, Object> map = (Map<String, Object>) new Yaml().load(inputStream);
            //修改index名称
            Object json = map.get(index);
            System.out.println(json.toString());
            String str = OkHttpUtil.putResponseWithParamsInJson(urlEsBase + index, json.toString());
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Test
    public void addData() throws Exception {

        index = "test20190821";

        IndexRequest indexRequest = new IndexRequest(index);
        HashMap<String, Object> map = new HashMap<>();
        map.put("esid", "1001111111");
        map.put("srvcCode", "服务C");
        map.put("srvcName", "serviceM");

        map.put("srvcEngName", "100");
        map.put("monitorTarget", "100");

        map.put("monitorTime", EsComponUtil.sourceTime2Time8("2022-08-09 09:15:00"));
        //map.put("monitorTime", "2022-08-09 09:15:00");
        map.put("trigger", "连续三次");
        map.put("noticeMethod", "服务M ldskjflsdlfl");
        map.put("noticePerson", "服务M ldskjflsdlfl");
        map.put("alarmNotice", "服务M ldskjflsdlfl");
        map.put("noticeCount", "服务M ldskjflsdlfl");
        map.put("ruleid", "2");
        map.put("isNotice", "已通知");

        indexRequest.source(map, XContentType.JSON);
        RestHighLevelClient client = EsClientMe.getRhlClient();
        client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("OK");
    }


    @Test
    public void emptyInd() {
        String index = "test20190821";
        final String res = emptyIndex(index);
        System.out.println(res);

    }

    /**
     * 清空es方法
     */
    public String emptyIndex(String index) {
        String jb = "{" + "\"query\": {" + " \"match_all\": {}" + " }" + "}";
        String delUrl = urlEsBase + index + "/_doc/_delete_by_query";
        return OkHttpUtil.postResponseWithParamsInJson(delUrl, jb);
    }

/**
*获取yml文件信息
*/
    @Test
    public void getYmlInfo() {
        final URL resource = EsIndexCreate.class.getResource("/config/esIndexMapping.yml");
        System.out.println(resource.getPath());
        try (FileInputStream inputStream = new FileInputStream(new File(resource.getPath()))) {
            final Map<String, Object> map = (Map<String, Object>) new Yaml().load(inputStream);
            System.out.println(map.get("warnlog"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

yml文件示例

test20190821:
  '{
      "mappings" : {
        "properties" : {
          "esid" : {"type" : "keyword"},
          "srvcCode" : {"type" : "keyword"},
          "warnTime" : {"type" : "date"},
          "isNotice" : {"type" : "keyword"}
        }
      },
      "settings" : {
        "number_of_replicas" : 1,
        "number_of_shards" : 3
      }
  }'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值