栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 云计算 > 云平台

ElasticSearch学习笔记

云平台 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ElasticSearch学习笔记

- 

创建maven工程

  1. 导入pom依赖


    com.fasterxml.jackson.core
    jackson-databind
    2.9.9


    org.elasticsearch
    elasticsearch
    7.8.0



    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.8.0



    org.apache.logging.log4j
    log4j-api
    2.17.1



    org.apache.logging.log4j
    log4j-core
    2.17.1

     
         junit
         junit
         4.13.2
         test
     
    
    //创建rest高级客户端 RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost("192.168.215.137", 9200)) );
     //关闭客户端
     restHighLevelClient.close();
    
  2. 创建索引
    //创建索引请求
    CreateIndexRequest user = new CreateIndexRequest(“user”);
    //创建索引 使用默认规则
    CreateIndexResponse createIndexResponse = client.indices().create(user, RequestOptions.DEFAULT);
    boolean acknowledged = createIndexResponse.isAcknowledged();
    System.out.println("acknowledged = " + acknowledged);

  3. 查询索引
    GetIndexRequest getIndexRequest = new GetIndexRequest(“user”);
    GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
    System.out.println("getIndexResponse.getMappings() = " + getIndexResponse.getMappings());
    System.out.println("getIndexResponse.getSetting() = " + getIndexResponse.getSettings());
    System.out.println("getIndexResponse.getAliases() = " + getIndexResponse.getAliases());

  4. 删除索引
    AcknowledgedResponse user = restHighLevelClient.indices().delete(new DeleteIndexRequest(“user”), RequestOptions.DEFAULT);
    System.out.println("user.isAcknowledged() = " + user.isAcknowledged());

  5. 文档新增
    IndexRequest request = new IndexRequest(“user”);

     request.id("1001");
    
    
     User user1 = new User();
     user1.setAge("10");
     user1.setName("我是你爹");
     user1.setSex("男");
    
     JSONObject jsonObject = JSONUtil.parseObj(user1, true, true);
     request.source(jsonObject, XContentType.JSON);
     IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
     System.out.println("response.getResult() = " + response.getResult());
    
  6. 文档修改
    UpdateRequest request = new UpdateRequest();
    request.index(“user”).id(“1001”);
    request.doc(XContentType.JSON, “sex”, “女”);

     UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    
     System.out.println("response.getGetResult() = " + response.getResult());
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/895808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号