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);
相关推荐
重生之绝世牛码7 分钟前
Java设计模式 —— 【结构型模式】外观模式详解
java·大数据·开发语言·设计模式·设计原则·外观模式
喝醉酒的小白26 分钟前
Elasticsearch相关知识@1
大数据·elasticsearch·搜索引擎
边缘计算社区26 分钟前
首个!艾灵参编的工业边缘计算国家标准正式发布
大数据·人工智能·边缘计算
MZWeiei26 分钟前
Zookeeper的选举机制
大数据·分布式·zookeeper
MZWeiei27 分钟前
Zookeeper基本命令解析
大数据·linux·运维·服务器·zookeeper
学计算机的睿智大学生28 分钟前
Hadoop集群搭建
大数据·hadoop·分布式
车轮滚滚__1 小时前
uniapp对接unipush 1.0 ios/android
笔记
小小工匠2 小时前
ElasticSearch - 深入解析 Elasticsearch Composite Aggregation 的分页与去重机制
elasticsearch·composite·after_key·桶聚合分页
风_流沙2 小时前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch