设为首页 加入收藏

TOP

ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程(二)
2019-10-10 11:17:24 】 浏览:95
Tags:ElasticSearch 实战 系列 JAVA API 使用 教程
创建之后就不能修改,除非reindex,所以这里我们还是指定数据模板进行创建。
使用JAVA API 创建索引库的方法和上述中新增数据的一样,有三种方式,不过这里就只介绍一种。

新增索引库的代码示例:

private static void createIndex() throws IOException {
    String type = "_doc";
    String index = "test1";
    // setting 的值
    Map<String, Object> setmapping = new HashMap<>();
    // 分区数、副本数、缓存刷新时间
    setmapping.put("number_of_shards", 10);
    setmapping.put("number_of_replicas", 1);
    setmapping.put("refresh_interval", "5s");
    Map<String, Object> keyword = new HashMap<>();
    //设置类型
    keyword.put("type", "keyword");
    Map<String, Object> lon = new HashMap<>();
    //设置类型
    lon.put("type", "long");
    Map<String, Object> date = new HashMap<>();
    //设置类型
    date.put("type", "date");
    date.put("format", "yyyy-MM-dd HH:mm:ss");

    Map<String, Object> jsonMap2 = new HashMap<>();
    Map<String, Object> properties = new HashMap<>();
    //设置字段message信息
    properties.put("uid", lon);
    properties.put("phone", lon);
    properties.put("msgcode", lon);
    properties.put("message", keyword);
    properties.put("sendtime", date);
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    jsonMap2.put(type, mapping);

    GetIndexRequest getRequest = new GetIndexRequest();
    getRequest.indices(index);
    getRequest.local(false);
    getRequest.humanReadable(true);
    boolean exists2 = client.indices().exists(getRequest, RequestOptions.DEFAULT);
    //如果存在就不创建了
    if(exists2) {
        System.out.println(index+"索引库已经存在!");
        return;
    }
    // 开始创建库
    CreateIndexRequest request = new CreateIndexRequest(index);
    try {
        // 加载数据类型
        request.settings(setmapping);
        //设置mapping参数
        request.mapping(type, jsonMap2);
        //设置别名
        request.alias(new Alias("pancm_alias"));
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        boolean falg = createIndexResponse.isAcknowledged();
        if(falg){
            System.out.println("创建索引库:"+index+"成功!" );
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

注:创建索引库的时候,一定要先判断索引库是否存在!!!
这里创建索引库的时候顺便也指定了别名(alias),这个别名是一个好东西,使用恰当可以提升查询性能,这里我们留着下次在讲。

三、修改数据

ES提供修改API的时候,有两种方式,一种是直接修改,但是若数据不存在会抛出异常,另一种则是存在更新,不存着就插入。相比第一种,第二种会更加好用一些,不过在写入速度上是不如第一种的。

ES修改的代码示例:

private static void update() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 唯一编号
    String id = "1";
    UpdateRequest upateRequest = new UpdateRequest();
    upateRequest.id(id);
    upateRequest.index(index);
    upateRequest.type(type);

    // 依旧可以使用Map这种集合作为更新条件
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("uid", 12345);
    jsonMap.put("phone", 123456789019L);
    jsonMap.put("msgcode", 2);
    jsonMap.put("sendtime", "2019-03-14 01:57:04");
    jsonMap.put("message", "xuwujing study Elasticsearch");
    upateRequest.doc(jsonMap);
    // upsert 方法表示如果数据不存在,那么就新增一条
    upateRequest.docAsUpsert(true);
    client.update(upateRequest, RequestOptions.DEFAULT);
    System.out
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java自学-日期 Date 下一篇SpringBoot健康检查实现原理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目