当前位置:   article > 正文

JAVA集成ElasticSearch、Java集成es

java集成elasticsearch

注:ES和Maven引用的版本尽量一致

使用Maven的情况下

  1. <dependency>
  2. <groupId> org.elasticsearch.client </groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.12.0</version>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
  7. <dependency>
  8. <groupId>org.elasticsearch</groupId>
  9. <artifactId>elasticsearch</artifactId>
  10. <version>7.12.0</version>
  11. </dependency>

这两个就够用了

如果没使用Maven项目管理集成Es的话 

https://download.csdn.net/download/gaolengroujiamo/16245088

在上面这个路径去下载集成Jar包去,已经给大家准备好了。

分隔线================================================================================================================================

ElasticSearch准备的基础数据

 

代码示例

  1. package com.example.demoes.controller;
  2. import org.apache.http.HttpHost;
  3. import org.elasticsearch.client.RestClient;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import java.io.IOException;
  6. public class EsApiController {
  7. public static org.elasticsearch.client.RestHighLevelClient RestHighLevelClient= new RestHighLevelClient(
  8. RestClient.builder(
  9. new HttpHost("localhost", 9200, "http"),
  10. new HttpHost("localhost", 9201, "http")));//初始化
  11. private static IndexApi indexApi = new IndexApi();
  12. private static SelectApi searchApi = new SelectApi();
  13. public static void main(String[] args) throws IOException {
  14. //indexApi.CreateIndexApi();//创建索引
  15. //indexApi.QueryIndexApi();//查询索引是否存在
  16. //indexApi.GetIndexDocumet("blog","2");
  17. //searchApi.SearchAll();
  18. //searchApi.SearchSoleParam("么稳");
  19. searchApi.SearchMuchParam("这是一段话");
  20. //searchApi.SearchParam("A350-A-71-00-51-00001-720A-A");
  21. //searchApi.SearchParam("JCNUMBER");
  22. EsApiController.close();
  23. }
  24. public static void close() throws IOException {
  25. RestHighLevelClient.close();
  26. }
  27. }

索引API

  1. package com.example.demoes.controller;
  2. import org.elasticsearch.action.get.GetRequest;
  3. import org.elasticsearch.action.get.GetResponse;
  4. import org.elasticsearch.action.index.IndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestHighLevelClient;
  7. import org.elasticsearch.client.indices.CreateIndexRequest;
  8. import org.elasticsearch.client.indices.CreateIndexResponse;
  9. import org.elasticsearch.client.indices.GetIndexRequest;
  10. import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. public class IndexApi {
  16. private static Logger logger = LoggerFactory.getLogger(IndexApi.class);
  17. private static RestHighLevelClient restHighLevelClient =EsApiController.RestHighLevelClient;
  18. /**
  19. * 创建索引
  20. */
  21. public void CreateIndexApi() throws IOException {
  22. CreateIndexRequest indexRequest = new CreateIndexRequest("addindex");
  23. CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(indexRequest, RequestOptions.DEFAULT);
  24. logger.info(createIndexResponse.toString());
  25. System.out.println(createIndexResponse);
  26. }
  27. /**
  28. * 查找索引
  29. */
  30. public void QueryIndexApi() throws IOException {
  31. GetIndexRequest indexRequest = new GetIndexRequest("addindex");
  32. boolean createIndexResponse = restHighLevelClient.indices().exists(indexRequest,RequestOptions.DEFAULT);
  33. logger.info(createIndexResponse+"");
  34. System.out.println(createIndexResponse);
  35. }
  36. /**
  37. * 查找索引
  38. */
  39. public void GetIndexDocumet(String indexName,String id ) throws IOException {
  40. GetRequest indexRequest = new GetRequest(indexName,id);
  41. indexRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
  42. GetResponse documentFields = restHighLevelClient.get(indexRequest, RequestOptions.DEFAULT);
  43. System.out.println(documentFields.getSource().toString());
  44. }
  45. }

简单查询API

  1. package com.example.demoes.controller;
  2. import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
  3. import org.elasticsearch.action.get.GetRequest;
  4. import org.elasticsearch.action.get.GetResponse;
  5. import org.elasticsearch.action.search.SearchRequest;
  6. import org.elasticsearch.action.search.SearchResponse;
  7. import org.elasticsearch.client.RequestOptions;
  8. import org.elasticsearch.client.RestHighLevelClient;
  9. import org.elasticsearch.client.indices.CreateIndexRequest;
  10. import org.elasticsearch.client.indices.CreateIndexResponse;
  11. import org.elasticsearch.client.indices.GetIndexRequest;
  12. import org.elasticsearch.common.unit.Fuzziness;
  13. import org.elasticsearch.index.query.BoolQueryBuilder;
  14. import org.elasticsearch.index.query.QueryBuilders;
  15. import org.elasticsearch.search.SearchHit;
  16. import org.elasticsearch.search.SearchHits;
  17. import org.elasticsearch.search.aggregations.Aggregation;
  18. import org.elasticsearch.search.builder.SearchSourceBuilder;
  19. import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import java.io.IOException;
  23. import java.util.List;
  24. public class SelectApi {
  25. private static Logger logger = LoggerFactory.getLogger(SelectApi.class);
  26. private static RestHighLevelClient restHighLevelClient =EsApiController.RestHighLevelClient;
  27. /**
  28. * 全部查询
  29. */
  30. public void SearchAll() throws IOException {
  31. SearchRequest searchRequest = new SearchRequest();
  32. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  33. searchSourceBuilder.query(QueryBuilders.matchAllQuery());
  34. searchSourceBuilder.size(50);
  35. searchRequest.source(searchSourceBuilder);
  36. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  37. SearchHits hits = search.getHits();
  38. for(SearchHit hit : hits){
  39. String sourceAsString = hit.getSourceAsString();
  40. if (sourceAsString != null) {
  41. System.out.println(sourceAsString);
  42. }
  43. }
  44. }
  45. /**
  46. * 模糊匹配单独字段查询
  47. * @param param
  48. * @throws IOException
  49. */
  50. public void SearchSoleParam(String param) throws IOException {
  51. SearchRequest searchRequest = new SearchRequest();
  52. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  53. searchSourceBuilder.query(QueryBuilders.matchQuery("title",param).fuzziness(Fuzziness.AUTO));//无效
  54. searchSourceBuilder.query(QueryBuilders.matchQuery("content",param).fuzziness(Fuzziness.AUTO));//无效
  55. searchSourceBuilder.query(QueryBuilders.matchQuery("jcXmlContent",param).fuzziness(Fuzziness.AUTO));//无效
  56. searchSourceBuilder.query(QueryBuilders.matchQuery("jcNumber",param).fuzziness(Fuzziness.AUTO));//无效
  57. searchSourceBuilder.query(QueryBuilders.matchQuery("author",param).fuzziness(Fuzziness.AUTO));//有效
  58. searchSourceBuilder.size(50);
  59. searchRequest.source(searchSourceBuilder);
  60. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  61. SearchHits hits = search.getHits();
  62. for(SearchHit hit : hits){
  63. String sourceAsString = hit.getSourceAsString();
  64. if (sourceAsString != null) {
  65. System.out.println("参数:"+sourceAsString);
  66. }
  67. }
  68. }
  69. /**
  70. * 模糊匹配多字段查询
  71. * @param param
  72. * @throws IOException
  73. */
  74. public void SearchMuchParam(String param) throws IOException {
  75. SearchRequest searchRequest = new SearchRequest();
  76. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  77. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  78. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("title",param));
  79. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("content",param));
  80. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("jcXmlContent",param));
  81. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("jcNumber",param));
  82. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("author",param));
  83. searchSourceBuilder.size(50) ;
  84. searchSourceBuilder.query(boolQueryBuilder);
  85. searchRequest.source(searchSourceBuilder);
  86. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  87. SearchHits hits = search.getHits();
  88. for(SearchHit hit : hits){
  89. String sourceAsString = hit.getSourceAsString();
  90. if (sourceAsString != null) {
  91. System.out.println("参数:"+sourceAsString);
  92. }
  93. }
  94. }
  95. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  96. }

查询出来的数据

一些复杂的查询可以去ES官网查看一下具体的API

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

如果对您有帮助的话劳烦您的小手点点赞

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/68500
推荐阅读
相关标签
  

闽ICP备14008679号