elasticsearch7.x整合springBoot

Posted 文nKkCJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch7.x整合springBoot相关的知识,希望对你有一定的参考价值。

1、导入依赖

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.6.2</version>
</dependency>

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

2、配置application.xml

elasticsearch:
  ip: 你的ip:9200

3、创建配置类


/**
 * 描述: 连接客户端
 *
 * @author wenye
 * @create 2021-03-25 13:22
 */
@Configuration
public class ElasticSearchClientConfig
{
    /**
     * 超时时间设为5分钟
     */
    private static final int TIME_OUT = 5 * 60 * 1000;
    private static final int ADDRESS_LENGTH = 2;
    private static final String HTTP_SCHEME = "http";

    @Value("${elasticsearch.ip}")
    String[] ipAddress;

    @Bean
    public RestClientBuilder restClientBuilder() {

        HttpHost[] hosts = Arrays.stream(ipAddress)
                .map(this::makeHttpHost)
                .filter(Objects::nonNull)
                .toArray(HttpHost[]::new);
        return RestClient.builder(hosts);
    }

    @Bean(name = "highLevelClient")
    public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
        restClientBuilder.setRequestConfigCallback(
                new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(
                            RequestConfig.Builder requestConfigBuilder) {
                        return requestConfigBuilder.setSocketTimeout(TIME_OUT);
                    }
                });
        //TODO 此处可以进行其它操作
        return new RestHighLevelClient(restClientBuilder);
    }


    private HttpHost makeHttpHost(String s) {
        assert StringUtils.isNotEmpty(s);
        String[] address = s.split(":");
        if (address.length == ADDRESS_LENGTH) {
            String ip = address[0];
            int port = Integer.parseInt(address[1]);
            System.err.println(ip+"+"+port);
            return new HttpHost(ip, port, HTTP_SCHEME);
        } else {
            return null;
        }
    }
}

4、实现es中的一些操作、如索引的创建、文档的crud、批量操作、各种查询


/**
 * @author : wenye
 * @describe 1、jest 以及elasticSearchTemplate(这个不清楚)都被不能调用7.0的版本了年轻人耗子尾汁 7.X使用的是rest来进行通信
 * @date : 2021-02-26 19:16
 **/
@RestController
@RequestMapping("/elasticsearch")
@Api(tags = "es文档")
public class ElasticSearch {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 创建索引
     * @throws IOException
     */
    @RequestMapping(value = "createindex",method = RequestMethod.POST)
    @ApiOperation(value = "创建索引")
    public void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("test2021030411");
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

    /**
     * 测试索引是否存在
     */
    @RequestMapping(value = "hashindex",method = RequestMethod.POST)
    @ApiOperation(value = "测试索引")
    public void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("test2021030411"); //测试test_index1索引是否存在
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println("索引是否存在:"+exists);
    }

    /**
     * 删除索引
     */
    @RequestMapping(value = "deleteindex",method = RequestMethod.POST)
    @ApiOperation(value = "删除索引")
    public void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("test2021030411");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    /**
     * 添加文档
     */
    @RequestMapping(value = "adddoc",method = RequestMethod.POST)
    @ApiOperation(value = "添加文档")
    public void testAddDocument() throws IOException {
        //创建对象
        ElasticsearchDto elasticSearch = new ElasticsearchDto();
        elasticSearch.setAge(11);
        elasticSearch.setName("wenye");
        //json数据
        String value = JSONUtils.toJsonString(elasticSearch);
        //创建请求
        IndexRequest request = new IndexRequest("test2021030411");
        // put /test2021030411/_doc/1
        request.id("1");
        //设置超时时间 等待主片的响应时间
        request.timeout(TimeValue.timeValueSeconds(5));
        //刷新策略
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        request.setRefreshPolicy("wait_for");
        //将数据放入请求
        request.source(value, XContentType.JSON);

        //执行请求
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }

    /**
     *
     * 判断文档是否存在
     */
    @RequestMapping(value = "testexistDoc",method = RequestMethod.POST)
    @ApiOperation(value = "判断文档")
    public void testExistDoc() throws IOException {
        GetRequest request = new GetRequest("test2021030411", "1");
        //不获取返回的_source的上下文了
        //request.fetchSourceContext(new FetchSourceContext(false));
        //request.storedFields("_none_");
        boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
        System.out.println("文档是否存在:"+exists);
    }

    /**
     * 获取文档
     * @throws IOException
     */
    @RequestMapping(value = "getdoc",method = RequestMethod.POST)
    @ApiOperation(value = "获取文档")
    public RespMessage getDoc() throws IOException {
        GetRequest request = new GetRequest("test2021030411", "1");
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsString());
        System.out.println(response);
        return RespHandler.success(response.getSourceAsString());

    }

    /**
     * 更新文档
     */
    @RequestMapping(value = "updatedoc",method = RequestMethod.POST)
    @ApiOperation(value = "更新文档")
    void testUpdateDoc() throws IOException {
        ElasticsearchDto elasticSearch = new ElasticsearchDto();
        elasticSearch.setAge(11);
        elasticSearch.setName("wenye");
        UpdateRequest request = new UpdateRequest("test2021030411", "1");
        //超时时间
        //request.timeout("5s");
        String value = JSONUtils.toJsonString(elasticSearch);
        request.doc(value,XContentType.JSON);
        UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);

    }

    /**
     * 删除文档
     */
    @RequestMapping(value = "deletedoc",method = RequestMethod.POST)
    @ApiOperation(value = "删除文档")
    void deleteDoc() throws IOException {
        DeleteRequest request = new DeleteRequest("test2021030411","1");
        restHighLevelClient.delete(request,RequestOptions.DEFAULT);
    }

    /**
     * 批量操作
     * @return
     */
    @RequestMapping(value = "bullkdoc",method = RequestMethod.POST)
    @ApiOperation(value = "文档批量操作")
    public RespMessage bullkdoc() {
        BulkRequest request = new BulkRequest();
        request.add(new DeleteRequest("test2021030411", "3"));
        request.add(new UpdateRequest("test2021030411", "2")
                .doc(XContentType.JSON,"other", "test"));
        request.add(new IndexRequest("test2021030411").id("4")
                .source(XContentType.JSON,"field", "baz"));
        try {
            restHighLevelClient.bulk(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return RespHandler.success();
    }
    void simpleSearch() throws IOException {

    }

    /**
     * matchAll搜索
     */
    @RequestMapping(value = "matchallquery",method = RequestMethod.POST)
    @ApiOperation(value = "匹配所有")
    public RespMessage matchAllquery() throws IOException {
        SearchRequest request = new SearchRequest("test2021030411");
        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查询条件 可以使用QueryBuilders工具来实现
        //QueryBuilders.termQuery精确匹配
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQueryBuilder);
        sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
        sourceBuilder.from(0);  //查询结果从第几条数据开始返回
        sourceBuilder.size(5);//一次返回几条数据
        request.source(sourceBuilder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        //遍历输出
        for (SearchHit fields : response.getHits().getHits()) {
            System.out.println(fields.getSourceAsString());
        }
        return RespHandler.success(response.getHits());
    }

    /**
     * match搜索
     */
    @RequestMapping(value = "scrollquery",method = RequestMethod.POST)
    @ApiOperation(value = "匹配搜索")
    public RespMessage scrollquery() throws IOException {
        SearchRequest request = new SearchRequest("test2021030411");
        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查询条件 可以使用QueryBuilders工具来实现
        //QueryBuilders.termQuery精确匹配
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "文烨");
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
        sourceBuilder.from(0);  //查询结果从第几条数据开始返回
        sourceBuilder.size(5);//一次返回几条数据
        //高亮搜索
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.requireFieldMatch(false);
        highlightBuilder.preTags("<span style=\'color:red\'");
        highlightBuilder.postTags("</span>");
        sourceBuilder.highlighter(highlightBuilder);
        //执行查询
        request.source(sourceBuilder);
        //滚动设置
        request.scroll(TimeValue.timeValueMinutes(1L));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        String scrollId = response.getScrollId();
        SearchHits hits = response.getHits();

        //解析结果
        for (SearchHit hit : response.getHits().getHits()) {
            //执行搜索后,取出高亮字段
            Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields();
            HighlightField title = highlightFieldMap.get("title");
            Map<String, Object> stringObjectMap = hit.getSourceAsMap();//原来的结果
            //解析高亮字段
            if (title != null) {
                //取出高亮查询中具体的高亮字段
                Text[] fra = title.fragments();
                String new_title = "";
                for (Text text:fra) {
                    new_title += text;
                }
                stringObjectMap.put("title",new_title);
            }
        }
        return RespHandler.success(hits);
//        return RespHandler.success(scrollId);
    }

    /**
     * match搜索, scroll配置
     */
    @RequestMapping(value = "msearch",method = RequestMethod.POST)
    @ApiOperation(value = "滚动搜索")
    public RespMessage msearch() throws IOException {
        MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
        SearchRequest request = new SearchRequest("test2021030411");
        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //QueryBuilders.termQuery精确匹配
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "文烨");
        ///设置高亮
        sourceBuilder.highlighter();
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));

        request.source(sourceBuilder);
        multiSearchRequest.add(request);

        SearchRequest request2 = new SearchRequest("test2021030411");
        //构建搜索条件
        SearchSourceBuilder sourceBuilder2 = new SearchSourceBuilder();

        //QueryBuilders.termQuery精确匹配
        MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("name", "文烨");
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));

        request2.source(sourceBuilder2);
        multiSearchRequest.add(request2);
        //滚动设置

        MultiSearchResponse msearch = restHighLevelClient.msearch(multiSearchRequest, RequestOptions.DEFAULT);

        MultiSearchResponse.Item[] responses = msearch.getResponses();

        return RespHandler.success(responses);
//        return RespHandler.success(scrollId);
    }
    /**
     * term搜索
     */
    @RequestMapping(value = "termquery",method = RequestMethod.POST)
    @ApiOperation(value = "精准查询")
    void termSearch() throws IOException {
        SearchRequest request = new SearchRequest("test2021030411");
        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查询条件 可以使用QueryBuilders工具来实现
        //QueryBuilders.termQuery精确匹配
        TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name", "user1");
        sourceBuilder.query(queryBuilder);
        sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
        sourceBuilder.from(0);  //查询结果从第几条数据开始返回
        sourceBuilder.size(5);//一次返回几条数据
        request.source(sourceBuilder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        //遍历输出
        for (SearchHit fields : response.getHits().getHits()) {
            System.out.println(fields.getSourceAsString());
        }
    }

}

以上是关于elasticsearch7.x整合springBoot的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch7.x

使用kibana操作elasticsearch7.x 教程

SpringBoot+ElasticSearch7.x+JPA配置多数据源

「日记」ElasticSearch7.x新功能介绍

重磅 | Elasticsearch7.X学习路线图

ElasticSearch7.X和ElasticSearch8.X学习记录