七、Java 中 ElasticSearch使用:

1,添加ElasticSearch 客户端依赖项

在springboot项目中,需要添加ElasticSearch 客户端依赖项。 可以通过maven 工具添加。

xml 复制代码
<dependency> 
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId> 
   <version>新版本</version>
</dependency>

2,配置elasticSearch客户端

配置Elasticsearch客户端,以便能够连接到Elasticsearch服务器。可以在application.properties或application.yml文件中添加以下属性

ini 复制代码
spring.elasticsearch.rest.uris=http://localhost:9200

3,创建ElasticSearch客户端

创建ElasticSearch客户端,以便能够使用它来与ElasticSearch 服务器交互。 可以使用RestHighLevlClient 类创建ElasticSearch 客户端:

kotlin 复制代码
@Configurationpublic class ElasticsearchConfig { 
   @Value("${spring.elasticsearch.rest.uris}") 
   private String[] uris;

     @Bean(destroyMethod = "close")
    public RestHighLevelClient restHighLevelClient() { 
       RestClientBuilder restClientBuilder = RestClient.builder(  
          new HttpHost(uris[0], 9200, "http")        );
         return new RestHighLevelClient(restClientBuilder);
    }
}

springboot 整合的es 操作。 可以通过以下工具进行操作 RestHighLevelClient、BookRepository和ElasticsearchTemplate.

学习这部分的时候 发现这几种方式都可以用于与es 进行交互。那么我们用那种呢?

RestHighLevelClient的优点:

  1. 官方支持:RestHighLevelClient是Elasticsearch官方提供的Java高级客户端,它与Elasticsearch的版本保持同步,能够及时获得最新的功能和性能优化。
  2. 功能丰富:RestHighLevelClient提供了一系列的方法来执行各种操作,如索引、搜索、更新和删除文档等,可以满足大部分的需求。
  3. 灵活性高:RestHighLevelClient提供了灵活的配置选项,可以根据具体需求进行配置,如连接超时时间、请求重试策略等。
  4. 性能优化:RestHighLevelClient使用了异步请求和批量操作等技术,可以提高性能和吞吐量。

RestHighLevelClient的缺点:

  1. 学习曲线较陡峭:由于RestHighLevelClient是底层的原生客户端,使用时需要对Elasticsearch的API和查询语法有一定的了解,对于新手来说可能需要花费一些时间学习和理解。
  2. 编码复杂:使用RestHighLevelClient编写代码时,需要手动处理请求和响应的序列化和反序列化,编码复杂度较高。

BookRepository的优点:

  1. 简化开发:BookRepository使用Spring Data Elasticsearch提供的注解和方法,可以大大简化与Elasticsearch的交互,减少了繁琐的代码编写。
  2. 提供CRUD操作:BookRepository提供了常见的CRUD操作方法,如保存、查询、更新和删除等,可以快速实现对文档的操作。
  3. 支持自定义查询:BookRepository支持使用注解和方法来定义自定义查询,可以根据具体需求灵活地构建查询条件。

BookRepository的缺点:

  1. 依赖于Spring Data Elasticsearch:使用BookRepository需要引入Spring Data Elasticsearch的依赖,增加了项目的复杂性和依赖关系。
  2. 功能相对有限:相比于原生的RestHighLevelClient,BookRepository提供的功能相对有限,可能无法满足一些复杂的需求。

ElasticsearchTemplate的优点:

  1. 简化操作:ElasticsearchTemplate封装了底层的Elasticsearch客户端,提供了一些便捷的方法来执行索引、搜索、更新和删除等操作,简化了操作的过程。
  2. 支持自定义查询:ElasticsearchTemplate支持使用原生的查询语句或QueryBuilder来构建自定义查询,可以满足一些复杂的查询需求。

ElasticsearchTemplate的缺点:

  1. 不是官方推荐:ElasticsearchTemplate是Spring Data Elasticsearch提供的一个类,虽然功能较为丰富,但不是官方推荐的方式,可能存在一些兼容性和稳定性的问题。
  2. 缺乏一些新特性:由于ElasticsearchTemplate是基于旧版本的Elasticsearch客户端开发的,可能缺乏一些新版本的特性和优化。

目前来说,RestHighLevelClient是更好的选择。它是官方提供的原生客户端,具有更好的兼容性和稳定性,能够及时获得最新的功能和性能优化。虽然使用RestHighLevelClient需要一些学习成本和编码复杂度,但它提供了更底层的操作方式,可以满足更复杂的需求。而BookRepository和ElasticsearchTemplate虽然提供了一些便捷的方法和功能,但相对来说功能较为有限,且可能存在一些兼容性和稳定性的问题。因此,如果对性能和功能要求较高,建议使用RestHighLevelClient。

在了解以上信息后 。我们来看 RestHightLevelClient 使用方法:

创建 RestHightLevelClient 上面已经给出,不再赘述。 下面来看怎么使用。

由于我们已经通过 spring bean 管理了 RestHightLevelClient

使用的时候直接引入就可以了 如下

java 复制代码
@Resource
private RestHighLevelClient restHighLevelClient;

reast-api操作

1,索引

创建索引:

restHighLevelClient 用esClient 替代

scss 复制代码
publicstaticvoidcreateIndex()throws IOException {   CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");   CreateIndexResponse indexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);    boolean acknowledged = indexResponse.isAcknowledged();    System.out.println("索引创建状态:" + acknowledged);}

获取索引

scss 复制代码
publicstaticvoidgetIndex()throws IOException {        GetIndexRequest getIndexRequest = new GetIndexRequest("user");        GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);        System.out.println(getIndexResponse.getAliases());        System.out.println(getIndexResponse.getMappings());        System.out.println(getIndexResponse.getSettings());    }

删除索引

go 复制代码
publicstaticvoiddeleteIndex() throws IOException {        DeleteIndexRequest getIndexRequest = new DeleteIndexRequest("user");        AcknowledgedResponse delete = esClient.indices().delete(getIndexRequest, RequestOptions.DEFAULT);        System.out.println("索引删除状态:" + delete.isAcknowledged());    }

2,文档常用api 操作

1,索引添加文档

erlang 复制代码
publicstaticvoidadd()throws Exception{        IndexRequest indexRequest = new IndexRequest();        indexRequest.index("user").id("1111");  //如果同步的表中数据 直接使用自增主键即可        User user = new User();        user.setName("测试");        user.setAge(23);        user.setSex("-");        user.setSalary(10000);        String userData = objectMapper.writeValueAsString(user);        indexRequest.source(userData,XContentType.JSON);        //插入数据        IndexResponse response = esClient.index(indexRequest, RequestOptions.DEFAULT);        System.out.println(response.status());        System.out.println(response.getResult());    }

2,索引修改文档

vbscript 复制代码
publicstaticvoidupdate()throws Exception{        UpdateRequest request = new UpdateRequest();        request.index("user").id("1111");        request.doc(XContentType.JSON,"name","鸣人");        //插入数据        UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);        System.out.println(response.getResult());    }

3,删除文档

erlang 复制代码
publicstaticvoiddelete() throws Exception{        DeleteRequest request = new DeleteRequest();        request.index("user").id("1111");        //插入数据        DeleteResponse delete = esClient.delete(request, RequestOptions.DEFAULT);        System.out.println(delete.getResult());    }

4,批量添加文档

scss 复制代码
public staticvoid batchInsert() throws Exception{         BulkRequest bulkRequest = new BulkRequest();         User user1 = new User("佐助","男",33,5500);        String userData1 = objectMapper.writeValueAsString(user1);        IndexRequest indexRequest1 = new IndexRequest().index("user").id("1002").source(userData1, XContentType.JSON);         bulkRequest.add(indexRequest1);         User user2 = new User("小樱","女",20,8000);        String userData2 = objectMapper.writeValueAsString(user2);        IndexRequest indexRequest2 = new IndexRequest().index("user").id("1003").source(userData2, XContentType.JSON);        bulkRequest.add(indexRequest2);         User user3 = new User("凯","男",49,10000);        String userData3 = objectMapper.writeValueAsString(user3);        IndexRequest indexRequest3 = new IndexRequest().index("user").id("1004").source(userData3, XContentType.JSON);        bulkRequest.add(indexRequest3);         BulkResponse bulkResponse = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);        System.out.println(bulkResponse.status());        System.out.println(bulkResponse.getItems());    }

5,批量删除文档

erlang 复制代码
publicstaticvoidbatchDelete()throws Exception{        BulkRequest bulkRequest = new BulkRequest();        DeleteRequest indexRequest1 = new DeleteRequest().index("user").id("1002");        DeleteRequest indexRequest2 = new DeleteRequest().index("user").id("1003");        DeleteRequest indexRequest3 = new DeleteRequest().index("user").id("1004");            bulkRequest.add(indexRequest1);        bulkRequest.add(indexRequest2);        bulkRequest.add(indexRequest3);         BulkResponse bulkResponse = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);        System.out.println(bulkResponse.status());        System.out.println(bulkResponse.getItems());    }

搜索API 使用

1,查询某个索引下的所有数据

vbscript 复制代码
publicstaticvoidsearchIndexAll()throws Exception{        SearchRequest request = new SearchRequest();        request.indices("user");        // 索引中的全部数据查询        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());        request.source(query);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }    }

2,批量查询多条数据

vbnet 复制代码
      MultiGetRequest multiGetRequest = new MultiGetRequest();        multiGetRequest.add("user", "1002");        multiGetRequest.add("user", "1003");        MultiGetResponse responses = esClient                .mget(multiGetRequest, RequestOptions.DEFAULT);         Iterator<MultiGetItemResponse> iterator = responses.iterator();        while (iterator.hasNext()){            MultiGetItemResponse next = iterator.next();            System.out.println(next.getResponse().getSourceAsString());        }

3,根据条件精确查询

scss 复制代码
TermQueryBuilder ageQueryBuilder = QueryBuilders.termQuery("sex", "女");        SearchSourceBuilder query = new SearchSourceBuilder().query(ageQueryBuilder);        request.source(query);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

4,分页查询

scss 复制代码
 SearchSourceBuilder sourceBuilder = new                                 SearchSourceBuilder().query(QueryBuilders.matchAllQuery());        sourceBuilder.from(0).size(3);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

5,查询结果按照某个字段进行排序

scss 复制代码
 SearchSourceBuilder sourceBuilder = new             SearchSourceBuilder().query(QueryBuilders.matchAllQuery());        sourceBuilder.sort("age",SortOrder.ASC);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

6,查询结果过滤某些字段

scss 复制代码
 SearchSourceBuilder sourceBuilder = new                 SearchSourceBuilder().query(QueryBuilders.matchAllQuery());        request.source(sourceBuilder);        String[] includes = {"name","sex"};        String[] excludes = {"age"};        sourceBuilder.fetchSource(includes,excludes);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

7,多条件查询

scss 复制代码
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();        boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));        boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lt(45).gt(35));        sourceBuilder.query(boolQueryBuilder);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);         System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

8,范围查询

scss 复制代码
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();        RangeQueryBuilder rangeQueryBuilder =             QueryBuilders.rangeQuery("age").gte(35).lte(45);        sourceBuilder.query(rangeQueryBuilder);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

9,模糊查询

scss 复制代码
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();        FuzzyQueryBuilder fuzzyQueryBuilder =                QueryBuilders.fuzzyQuery("name", "鸣人")                        .fuzziness(Fuzziness.ONE);        sourceBuilder.query(fuzzyQueryBuilder);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

10.高亮查询

scss 复制代码
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();        TermQueryBuilder ageQueryBuilder = QueryBuilders.termQuery("age", 33);        HighlightBuilder highlightBuilder = new HighlightBuilder();        highlightBuilder.preTags("<font color='red'>");        highlightBuilder.postTags("</font>");        highlightBuilder.field("name");        sourceBuilder.highlighter(highlightBuilder);        sourceBuilder.query(ageQueryBuilder);        request.source(sourceBuilder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

11,多字段查询multi_match

这个用法表示从多个字段中匹配某个关键字

scss 复制代码
  SearchSourceBuilder builder = new SearchSourceBuilder();        MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("黄忠","name", "sex");        multiMatchQuery.operator(Operator.OR);        builder.query(multiMatchQuery);        request.source(builder);        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);        System.out.println(response.getHits().getHits());        System.out.println(response.getHits().getTotalHits());        SearchHits hits = response.getHits();        for (SearchHit searchHit : hits){            System.out.println(searchHit.getSourceAsString());        }

12 ,聚合查询

ini 复制代码
  public class ElasticsearchAggregationQuery {    private static final String INDEX_NAME = "your_index_name";

    public void aggregationQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 设置查询条件
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        // 添加聚合查询
        searchSourceBuilder.aggregation(
                AggregationBuilders.terms("group_by_field").field("your_field_name")
                        .subAggregation(AggregationBuilders.avg("avg_value").field("your_numeric_field"))
        );

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);

        // 处理聚合结果
        Terms termsAggregation = searchResponse.getAggregations().get("group_by_field");
        for (Terms.Bucket bucket : termsAggregation.getBuckets()) {
            String key = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            System.out.println("Key: " + key + ", Doc Count: " + docCount);

            Avg avgAggregation = bucket.getAggregations().get("avg_value");
            double avgValue = avgAggregation.getValue();
            System.out.println("Average Value: " + avgValue);
        }
    }
}

13, 分组查询

ini 复制代码
public class ElasticsearchGroupingQuery {
    private static final String INDEX_NAME = "your_index_name";

    public void groupingQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 设置查询条件
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        // 添加分组查询
        searchSourceBuilder.aggregation(
                AggregationBuilders.terms("group_by_field").field("your_field_name")
        );

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);

        // 处理分组查询结果
        Terms termsAggregation = searchResponse.getAggregations().get("group_by_field");
        for (Terms.Bucket bucket : termsAggregation.getBuckets()) {
            String key = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            
            System.out.println("Key: " + key + ", Doc Count: " + docCount);
        }
    }
}
相关推荐
竹影卿心几秒前
Java连接HANA数据库
java·数据库·windows
Abelard_9 分钟前
LeetCode--347.前k个高频元素(使用优先队列解决)
java·算法·leetcode
海海不掉头发26 分钟前
软件工程-【软件项目管理】--期末复习题汇总
java·学习·产品运营·软件工程·团队开发·需求分析·期末复习
缘友一世29 分钟前
java实现网络IO高并发编程java AIO
java·网络·python
CodeClimb33 分钟前
【华为OD-E卷 - 猜字谜100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
ADRU39 分钟前
设计模式-责任链模式
java·设计模式·责任链模式
heeheeai40 分钟前
kotlin 函数作为参数
java·算法·kotlin
吴冰_hogan1 小时前
Java虚拟机(JVM)的类加载器与双亲委派机制
java·开发语言·jvm
程序员shen1616111 小时前
注意⚠️:矩阵系统源码开发/SaaS矩阵系统开源/抖音矩阵开发优势和方向
java·大数据·数据库·python·php
青春男大1 小时前
java队列--数据结构
java·开发语言·数据结构·学习·eclipse