ElasticSearch之RestClient笔记

1. ElasticSearch

1.1 倒排索引

1.2 ElasticSearch和Mysql对比

1.3 RestClient操作

导入依赖

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.15.0</version>
        </dependency>

并设置

<elasticsearch.version>7.15.0</elasticsearch.version>

添加ElasticSearch配置

java 复制代码
@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
        );
        return client;
    }
}

索引的创建,判断是否存在,删除

java 复制代码
  CreateIndexRequest createIndexRequest=new CreateIndexRequest("test");
        restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        //判断某个索引库是否存在
        GetIndexRequest getIndexRequest=new GetIndexRequest("test");
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
        //删除某个索引库
        DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("test");
        restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);

添加文档

java 复制代码
  IndexRequest in = new IndexRequest("test").id("1").source("{\n" +
                "   \"username\":\"admin\",\n" +
                "   \"password\":\"admin\"\n" +
                "}", XContentType.JSON);
        restHighLevelClient.index(in,RequestOptions.DEFAULT);

查找文档

java 复制代码
 GetRequest getRequest=new GetRequest("test","1");
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());

更新文档

java 复制代码
//全量更新,再次写入id一样的文档,就会删除旧文档,添加新文档
        IndexRequest in = new IndexRequest("test").id("1").source("{\n" +
                "   \"username\":\"admin\",\n" +
                "   \"password\":\"helloworld\"\n" +
                "}", XContentType.JSON);
        restHighLevelClient.index(in,RequestOptions.DEFAULT);


UpdateRequest updateRequest=new UpdateRequest("test","1");
        updateRequest.doc(
                "password","hello"
        );
        restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);

删除文档

java 复制代码
  DeleteRequest deleteRequest=new DeleteRequest("test","1");
        restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);

批量导入文档

java 复制代码
  BulkRequest bulkRequest=new BulkRequest();
        bulkRequest.add(new IndexRequest("test").id("2").source("{\n" +
                "   \"username\":\"hello\",\n" +
                "   \"password\":\"hello\"\n" +
                "}",XContentType.JSON));
        bulkRequest.add(new IndexRequest("test").id("3").source("{\n" +
                "   \"username\":\"world\",\n" +
                "   \"password\":\"world\"\n" +
                "}",XContentType.JSON));
        restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
相关推荐
doubt。36 分钟前
【BUUCTF】[RCTF2015]EasySQL1
网络·数据库·笔记·mysql·安全·web安全
喝醉酒的小白36 分钟前
Elasticsearch 中,分片(Shards)数量上限?副本的数量?
大数据·elasticsearch·jenkins
Zelotz1 小时前
线段树与矩阵
笔记
汇能感知2 小时前
光谱相机在智能冰箱的应用原理与优势
经验分享·笔记·科技
yuanbenshidiaos2 小时前
【大数据】机器学习----------计算机学习理论
大数据·学习·机器学习
熟透的蜗牛3 小时前
Elasticsearch 8.17.1 JAVA工具类
elasticsearch
Pandaconda3 小时前
【Golang 面试题】每日 3 题(四十一)
开发语言·经验分享·笔记·后端·面试·golang·go
杰克逊的日记4 小时前
HBased的原理
大数据·hbase
红色的山茶花4 小时前
YOLOv10-1.1部分代码阅读笔记-predictor.py
笔记·深度学习·yolo
执念斩长河5 小时前
Go反射学习笔记
笔记·学习·golang