ElasticSearch 7.6.x 版本 2020最新版 JavaRest api

ElasticSearch 7.6.x 版本 2020最新版 JavaRest api

 

 

前言

      周末闲来无事,到官网学习(fanyi)了下最新版本的ESJavaRest API。
ES版本:7.6.2
Springboot版本:2.2.6

准备工作及注意事项

1. Maven导包

      Springboot 2.2.6默认帮我们导入的是ES 6.8.7的包,所以我们需要更改下版本

<elasticsearch.version>7.6.1</elasticsearch.version>
  • 1

2. 初始化Client

@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient client(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost",9200,"http")
        ));
        return client;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

索引(index)

增加索引

    //创建索引
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("index1");
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

删除索引

    //删除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index1");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        boolean isSuccessful = delete.isAcknowledged();

        System.out.println(isSuccessful);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

查询索引是否存在

    //返回索引是否存在
    @Test
    void existIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("index1");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

文档(document)

添加文档

    //添加文档
    @Test
    void addDocument() throws IOException {
        User user = new User("dai", 22);
        IndexRequest request = new IndexRequest("index1")
                .id("1")
                .timeout(TimeValue.timeValueSeconds(1));

        IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);

        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

删除文档

    //删除文档
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("index1", "2");
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(deleteResponse.status());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

查询文档是否存在

    //判断文档是否存在
    @Test
    void isExistDocument() throws IOException {
        GetRequest request = new GetRequest("index1", "1");
        //不获取_source内容,提升效率
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");

        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

更新文档

    //更新文档
    @Test
    void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("index1", "1");
        request.timeout("1s");

        User user = new User("dai2", 22);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

        System.out.println(updateResponse.status());
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

数据(datas)

批量插入数据

    //批量插入数据
    @Test
    void BulkRequest() throws IOException {

        BulkRequest bulkRequest = new BulkRequest()
                .timeout("5s");
        List<User> users = Arrays.asList(new User("dai1", 1), new User("dai2", 2), new User("dai3", 3));

        for (User user : users) {
            bulkRequest.add(new IndexRequest("index1")
                    //.id("xxx")
                    .source(JSON.toJSONString(user), XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //是否失败,false表示成功
        System.out.println(bulkResponse.hasFailures());
        System.out.println(bulkResponse.status());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

查询

    //查询
    @Test
    void search() throws IOException {
        SearchRequest request = new SearchRequest("index1");

        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // SearchRequest 搜索请求
        // SearchSourceBuilder 条件构造
        // HighlightBuilder 构建高亮
        // TermQueryBuilder 精确查询
        // MatchAllQueryBuilder .....
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQueryBuilder)
                .timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
        System.out.println("===================================");
        for (SearchHit documentFields : searchResponse.getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

点个赞再走,球球啦!

原创不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

本博客仅发布于CSDN—一个帅到不能再帅的人 Mr_kidBK。转载请标明出处。
https://blog.csdn.net/Mr_kidBK

点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!

======================================================================================

springboot集成elasticsearch-rest-high-level-client的坑

原创
2019/09/23 17:01
阅读数 3.9W

我使用的是Elasticsearch7.2.0,由于官网上推荐使用elasticsearch-rest-high-level-client端集成在springboot所以尝试爬坑。

首先直接引入官网的配置文件

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

然后在运行时报错:

...
...
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.xcontent.DeprecationHandler
...
...

百度了一下说jar里的Elasticsearch版本有错,要移除了再重新引入,所以把配置文件改成下面的样子

        <dependency>
		    <groupId>org.elasticsearch.client</groupId>
		    <artifactId>elasticsearch-rest-high-level-client</artifactId>
		    <version>7.2.0</version>
		    <exclusions>
	       <exclusion>
	         <groupId>org.elasticsearch</groupId>
	         <artifactId>elasticsearch</artifactId>
	       </exclusion>
	     </exclusions>
		</dependency>
		
		<!-- 正确版本 -->
		<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.2.0</version>
        </dependency>

然后再次启动,Elasticsearch正常运行,但是发现spring的rest服务运行不正常,配置的Filter没有正常运行,且不报错。网上找了很久也没有找到有类似错误的问题,后来反复查看官网,他说elasticsearch-rest-high-level-client是基于elasticsearch-rest-client的,我就猜这个基于会不会是依赖的意思呢?

于是把配置改成了这样:

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

		<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.2.0</version>
        </dependency>
		
		<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.2.0</version>
        </dependency>

然后再次启动,Elasticsearch正常运行,REST服务也正常,我的老天鹅,终于可以了。

后来我找到了这篇文章Elasticsearch High Level REST Client,具体配置可以参考下

https://blog.csdn.net/mr_kidbk/article/details/105477095

https://my.oschina.net/u/3362856/blog/3109764

高亮查询

https://blog.csdn.net/qq_43750656/article/details/106752154

原文地址:https://www.cnblogs.com/javalanguage/p/15261822.html