首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

ES8的Java API client 8.0 简单示例操作 Elasticsearch

  • 25-03-07 22:21
  • 2357
  • 13168
blog.csdn.net

1.加入依赖

  1. co.elastic.clients
  2. elasticsearch-java
  3. 8.12.2

2.配置类

  1. @Slf4j
  2. @Configuration
  3. public class ElasticSearchConfig {
  4. @Value("${elasticsearch.hosts}")
  5. private String hosts;
  6. @Value("${elasticsearch.port}")
  7. private int port;
  8. @Value("${elasticsearch.username}")
  9. private String username;
  10. @Value("${elasticsearch.password}")
  11. private String password;
  12. @Value("${elasticsearch.apikey:''}")
  13. private String apikey;
  14. /**
  15. * 单节点没密码连接
  16. *
  17. * @return
  18. */
  19. @Bean
  20. public ElasticsearchClient elasticsearchClient() {
  21. String[] servers = hosts.split(",");
  22. int len = servers.length;
  23. if (0 == len) {
  24. log.error("ElasticsearchClient 配置错误!");
  25. }
  26. ElasticsearchTransport transport = null;
  27. // 不是集群时
  28. if (1 == len) {
  29. // 无账号、密码
  30. if (StringUtils.isEmpty(username) && StringUtils.isEmpty(password)) {
  31. RestClient client = RestClient.builder(new HttpHost(servers[0], port, "http"))
  32. .setHttpClientConfigCallback(httpClientBuilder
  33. ->httpClientBuilder.setDefaultHeaders(
  34. Collections.singletonList(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
  35. .addInterceptorLast((HttpResponseInterceptor) (response, context)
  36. -> response.addHeader("X-Elastic-Product", "Elasticsearch")))
  37. .build();
  38. transport = new RestClientTransport(client, new JacksonJsonpMapper());
  39. } else {
  40. // 账号密码的配置
  41. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  42. credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
  43. // 自签证书的设置,并且还包含了账号密码
  44. RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
  45. .setDefaultCredentialsProvider(credentialsProvider)
  46. .addInterceptorLast(
  47. (HttpResponseInterceptor)
  48. (response, context) ->
  49. response.addHeader("X-Elastic-Product", "Elasticsearch"));
  50. RestClient client = RestClient.builder(new HttpHost(servers[0], port, "http"))
  51. .setHttpClientConfigCallback(callback)
  52. .build();
  53. transport = new RestClientTransport(client, new JacksonJsonpMapper());
  54. }
  55. } else {
  56. // 集群时无账号、密码
  57. if (StringUtils.isEmpty(username) && StringUtils.isEmpty(password)) {
  58. transport = getElasticsearchTransport(toHttpHost());
  59. } else {
  60. transport = getElasticsearchTransport(username, password, toHttpHost());
  61. }
  62. }
  63. return new ElasticsearchClient(transport);
  64. }
  65. private HttpHost[] toHttpHost() {
  66. if (hosts.split(",").length == 0) {
  67. throw new RuntimeException("invalid elasticsearch configuration");
  68. }
  69. String[] hostArray = hosts.split(",");
  70. HttpHost[] httpHosts = new HttpHost[hostArray.length];
  71. HttpHost httpHost;
  72. for (int i = 0; i < hostArray.length; i++) {
  73. String[] strings = hostArray[i].split(":");
  74. httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "http");
  75. httpHosts[i] = httpHost;
  76. }
  77. return httpHosts;
  78. }
  79. private static ElasticsearchTransport getElasticsearchTransport(String username, String password, HttpHost... hosts) {
  80. // 账号密码的配置
  81. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  82. credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
  83. // 自签证书的设置,并且还包含了账号密码
  84. RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
  85. .setSSLContext(buildSSLContext())
  86. .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
  87. .setDefaultCredentialsProvider(credentialsProvider)
  88. .setDefaultHeaders(
  89. Stream.of(new BasicHeader(
  90. HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())).collect(toList())
  91. ).addInterceptorLast(
  92. (HttpResponseInterceptor)
  93. (response, context) ->
  94. response.addHeader("X-Elastic-Product", "Elasticsearch"))
  95. .addInterceptorLast((HttpResponseInterceptor) (response, context)
  96. -> response.addHeader("X-Elastic-Product", "Elasticsearch"));
  97. // 用builder创建RestClient对象
  98. RestClient client = RestClient
  99. .builder(hosts)
  100. .setHttpClientConfigCallback(callback)
  101. .build();
  102. return new RestClientTransport(client, new JacksonJsonpMapper());
  103. }
  104. private static ElasticsearchTransport getElasticsearchTransport(HttpHost... hosts) {
  105. // 用builder创建RestClient对象
  106. RestClient client = RestClient
  107. .builder(hosts)
  108. .build();
  109. return new RestClientTransport(client, new JacksonJsonpMapper());
  110. }
  111. private static SSLContext buildSSLContext() {
  112. ClassPathResource resource = new ClassPathResource("es01.crt");
  113. SSLContext sslContext = null;
  114. try {
  115. CertificateFactory factory = CertificateFactory.getInstance("X.509");
  116. Certificate trustedCa;
  117. try (InputStream is = resource.getInputStream()) {
  118. trustedCa = factory.generateCertificate(is);
  119. }
  120. KeyStore trustStore = KeyStore.getInstance("pkcs12");
  121. trustStore.load(null, null);
  122. trustStore.setCertificateEntry("ca", trustedCa);
  123. SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustStore, null);
  124. sslContext = sslContextBuilder.build();
  125. } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
  126. KeyManagementException e) {
  127. log.error("ES连接认证失败", e);
  128. }
  129. return sslContext;
  130. }
  131. }

3.相关api

前提:

注入ElasticsearchClient

  1. @Autowired
  2. private ElasticsearchClient client;

3.1 创建索引

  1. public void createIndex() throws IOException {
  2. ElasticsearchIndicesClient indices = client.indices();
  3. //是否存在
  4. //1.lambda
  5. boolean isExit = indices.exists(req -> req.index("ttt")).value();
  6. //2.builder,of
  7. ExistsRequest existsRequestOf = ExistsRequest.of(req -> req.index("ttt"));
  8. ExistsRequest existsRequest = new ExistsRequest.Builder().index("ttt").build();
  9. boolean builderExist = indices.exists(existsRequest).value();
  10. if (isExit){
  11. log.info("已经存在ttt");
  12. }else {
  13. //1.lambda
  14. boolean isSuccess = indices.create(req -> req.index("ttt")).acknowledged();
  15. //2.builder,of
  16. CreateIndexRequest createIndexRequestOf = CreateIndexRequest.of(req -> req.index("ttt"));
  17. CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index("ttt").build();
  18. boolean buildIsSuccess = indices.create(createIndexRequest).acknowledged();
  19. if (isSuccess){
  20. log.info("创建成功");
  21. }else {
  22. log.info("创建失败");
  23. }
  24. }
  25. }

3.2 删除索引

  1. public void deleteIndex() throws IOException {
  2. //1.lambda
  3. boolean isSuccess = client.indices().delete(req -> req.index("ttt")).acknowledged();
  4. //2.builder,of
  5. DeleteIndexRequest deleteRequestOf = DeleteIndexRequest.of(req -> req.index("ttt"));
  6. DeleteIndexRequest deleteRequest = new DeleteIndexRequest.Builder().index("ttt").build();
  7. boolean buildDeleteRequest = client.indices().delete(deleteRequest).acknowledged();
  8. }

3.3查询索引

  1. public void queryIndex() throws IOException {
  2. //1.lambda
  3. //查询单个
  4. Map tttIndex = client.indices().get(req -> req.index("ttt")).result();
  5. //查询索引
  6. Set all = client.indices().get(req -> req.index("*")).result().keySet();
  7. //2.builder
  8. GetIndexRequest getIndexRequestOf = GetIndexRequest.of(req -> req.index("ttt"));
  9. GetIndexRequest getIndexRequest = new GetIndexRequest.Builder().index("ttt").build();
  10. Map result = client.indices().get(getIndexRequest).result();
  11. }

3.4 插入文档

  1. public void addDoc() throws IOException {
  2. Goods goods = new Goods("3212334","华为mate60", 9999.0);
  3. //1.lambda
  4. client.index(i->i
  5. .index("goods")
  6. .id(goods.getSku())
  7. .document(goods));
  8. //2.builder-of
  9. IndexRequest addIndexRequestOf = IndexRequest.of(t -> t.id(goods.getSku()).document(goods).index("goods"));
  10. IndexRequest addIndexRequest = new IndexRequest<>.Builder()
  11. .index("goods")
  12. .id(goods.getSku())
  13. .document(goods).build();
  14. IndexResponse index = client.index(addIndexRequest);
  15. }

3.5 批量插入文档

  1. public void addBatchDoc() throws IOException {
  2. List goodsList = List.of(new Goods("1","手机",999.0));
  3. BulkRequest.Builder br = new BulkRequest.Builder();
  4. for (Goods goods : goodsList) {
  5. br.operations(op->op.
  6. index(idx->idx.index("goods")
  7. .id(goods.getSku())
  8. .document(goods)));
  9. }
  10. BulkResponse result = client.bulk(br.build());
  11. // Log errors, if any
  12. if (result.errors()) {
  13. log.error("Bulk had errors");
  14. for (BulkResponseItem item: result.items()) {
  15. if (item.error() != null) {
  16. log.error(item.error().reason());
  17. }
  18. }
  19. }
  20. }

3.6查询文档

  1. public void queryDoc() throws IOException {
  2. //1.lambda
  3. GetResponse response = client.get(g -> g
  4. .index("goods")
  5. .id("00000")
  6. , Goods.class);
  7. //2.builder,of
  8. GetRequest request = GetRequest.of(g -> g.index("goods").id("00000"));
  9. GetRequest ofRequest = new GetRequest.Builder().index("goods").id("00000").build();
  10. GetResponse response1 = client.get(request, Goods.class);
  11. if (response.found()){
  12. Goods source = response.source();
  13. System.out.println(source);
  14. }
  15. }

3.7修改文档

  1. public void updateDoc() throws IOException {
  2. //全量
  3. Goods goods = new Goods("1","2",3.0);
  4. UpdateResponse update = client.update(u -> u
  5. .doc(goods)
  6. .id(goods.getSku())
  7. , Goods.class);
  8. //of
  9. UpdateRequest of = UpdateRequest.of(u -> u.id(goods.getSku()).doc(goods));
  10. UpdateResponse update1 = client.update(of, Goods.class);
  11. }
  12. 3.8删除文档

    1. */
    2. public void deleteDoc() throws IOException {
    3. DeleteResponse goods = client.delete(d -> d
    4. .index("goods")
    5. .id("0000"));
    6. //of
    7. DeleteRequest deleteRequest = DeleteRequest.of(d -> d.index("goods").id("0000"));
    8. client.delete(deleteRequest);
    9. }

    3.9 DSL 匹配查询

    1. public void query() throws IOException {
    2. String text = "华为mate60";
    3. SearchResponse response = client.search(s -> s
    4. .index("goods")
    5. .query(q -> q
    6. .match(m -> m
    7. .field("name")
    8. .query(text))
    9. )
    10. , Goods.class);
    11. //of
    12. SearchRequest request = SearchRequest.of(s -> s.index("goods")
    13. .query(q -> q
    14. .match(m ->
    15. m.field("").field(""))
    16. ));
    17. SearchResponse search = client.search(request, Goods.class);
    18. //结果
    19. //总数
    20. TotalHits total = response.hits().total();
    21. assert total != null;
    22. boolean isExactResult = total.relation() == TotalHitsRelation.Eq;
    23. if (isExactResult) {
    24. log.info("找到 " + total.value() + " 个结果");
    25. } else {
    26. log.info("找到超过 " + total.value() + " 个结果");
    27. }
    28. List> hits = response.hits().hits();
    29. for (Hit hit : hits) {
    30. Goods goods = hit.source();
    31. System.out.println(goods);
    32. }
    33. }

    3.10 多精确terms

    1. public void terms(){
    2. List v = new ArrayList<>();
    3. FieldValue tag2 = FieldValue.of("tag2");
    4. FieldValue tag1 = FieldValue.of("tag1");
    5. v.add(tag1);v.add(tag2);
    6. TermsQuery tags = TermsQuery.of(t -> t.field("tags").terms(tm -> tm.value(v)));
    7. SearchRequest of = SearchRequest.of(s -> s.index("goods")
    8. .query(q -> q
    9. .bool(b -> b
    10. .must(m -> m
    11. .terms(tags)
    12. )
    13. .should(sh -> sh
    14. .match(ma -> ma
    15. .field("")
    16. .query("")
    17. )
    18. )
    19. )
    20. )
    21. .from(0)
    22. .size(10)
    23. );
    24. }

    3.11 布尔查询

    1. @Slf4j
    2. @Service
    3. public class EsTest {
    4. @Autowired
    5. private ElasticsearchClient client;
    6. /**
    7. * bool
    8. */
    9. public void boolQuery() throws IOException {
    10. Map map = new HashMap<>();
    11. map.put("title", HighlightField.of(hf -> hf.preTags("").postTags("")));
    12. map.put("description", HighlightField.of(hf -> hf.preTags("").postTags("")
    13. .numberOfFragments(4).fragmentSize(50)));
    14. //numberOfFragments(4),表示将字段分割为最多4个片段,并设置 fragmentSize(50),表示每个片段的大小为50个字符。
    15. Highlight highlight = Highlight.of(
    16. h -> h.type(HighlighterType.Unified)
    17. .fields(map)
    18. .fragmentSize(50)//设置默认的高亮片段大小为50个字符,如果字段没有单独设置,则使用此默认值。
    19. .numberOfFragments(5)//设置每个字段的最大高亮片段数为5个。
    20. );
    21. String text = "华为mate60";
    22. SearchResponse search = client.search(s -> s
    23. .index("goods")
    24. .query(q -> q
    25. .bool(b -> b
    26. .must(m -> m
    27. .term(t -> t
    28. .field("品牌")
    29. .value("华为")
    30. )
    31. )
    32. .should(sd -> sd
    33. .match(mh -> mh
    34. .field("name")
    35. .query("mate60")))
    36. )
    37. )
    38. .from(0)
    39. .size(10)
    40. .highlight(highlight)
    41. , Goods.class);
    42. //of
    43. SearchRequest of = SearchRequest.of(s -> s.index("goods")
    44. .query(q -> q
    45. .bool(b -> b
    46. .must(m -> m
    47. .term(t -> t.field("品牌")
    48. .value("华为")
    49. )
    50. )
    51. .should(sh -> sh
    52. .match(ma -> ma
    53. .field("")
    54. .query("")
    55. )
    56. )
    57. )
    58. )
    59. .from(0)
    60. .size(10)
    61. .highlight(highlight)
    62. );
    63. SearchResponse response = client.search(of, Goods.class);
    64. List> hits = response.hits().hits();
    65. for (Hit hit : hits) {
    66. Goods goods = hit.source();
    67. if (goods == null){
    68. continue;
    69. }
    70. Map> highlightList = hit.highlight();
    71. highlightList.forEach((key, Value)->{
    72. if (Objects.equals(key,goods.getName())){
    73. //存入
    74. }else {
    75. //无高亮
    76. }
    77. }
    78. );
    79. System.out.println(goods);
    80. }
    81. }
    82. }

    3.12构建排序

    1. /**
    2. * 构建排序
    3. */
    4. private List buildSort(SearchDTO dto) {
    5. if (dto.getTimeSort() != null){
    6. SortOptions sortOptions ;
    7. if (dto.getTimeSort() == 0){
    8. sortOptions = SortOptions.of(s -> s.field(FieldSort.of(f -> f.field(SearchConstants.TIMESTAMP).order(SortOrder.Asc))));
    9. }else {
    10. sortOptions = SortOptions.of(s -> s.field(FieldSort.of(f -> f.field(SearchConstants.TIMESTAMP).order(SortOrder.Desc))));
    11. }
    12. return List.of(sortOptions);
    13. }else {
    14. return Collections.emptyList();
    15. }
    16. }

    ...........

    注:本文转载自blog.csdn.net的it-shiyadi的文章"https://blog.csdn.net/m0_73363097/article/details/142617941"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
    复制链接
    复制链接
    相关推荐
    发表评论
    登录后才能发表评论和回复 注册

    / 登录

    评论记录:

    未查询到任何数据!
    回复评论:

    分类栏目

    后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

    热门文章

    132
    搜索
    关于我们 隐私政策 免责声明 联系我们
    Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
    Scroll to Top