ElasticSearch之RestClient笔记

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。ElasticSearch之RestClient笔记,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1. ElasticSearch

ElasticSearch之RestClient笔记

1.1 倒排索引 

ElasticSearch之RestClient笔记

 ElasticSearch之RestClient笔记

1.2 ElasticSearch和Mysql对比 

ElasticSearch之RestClient笔记

 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配置

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

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

  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);

添加文档

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

查找文档

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

更新文档

//全量更新,再次写入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);

删除文档

  DeleteRequest deleteRequest=new DeleteRequest("test","1");
        restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);

批量导入文档

  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);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/196934.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!