关于elasticsearch:elasticsearch7x整合springBoot

37次阅读

共计 10168 个字符,预计需要花费 26 分钟才能阅读完成。

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

}

正文完
 0