ES中的api调用测试

2023-10-15 09:33:25 浏览数 (2)

Java高级REST客户端需要Java 1.8,并依赖于Elasticsearch核心项目。客户端版本与开发客户端的Elasticsearch版本相同。它接受与TransportClient相同的请求参数,并返回相同的响应对象。如果需要将应用程序从TransportClient迁移到新的REST客户端,请参阅迁移指南。

高级客户端保证能够与运行在相同主版本和更高或相同次版本上的任何Elasticsearch节点通信。它不需要与它通信的Elasticsearch节点处于相同的次要版本,因为它是向前兼容的,这意味着它支持与比其开发时所用版本更高版本的Elasticsearch通信。 

代码语言:javascript复制
  <!--es客户端-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.4.2</version>
        </dependency>

高级Java REST客户端依赖于以下工件及其 传递依赖关系:

  • org.elasticsearch.client:elasticsearch-rest-client 客户端:弹性搜索-其余-客户端
  • org.elasticsearch:elasticsearch 组织弹性搜索:弹性搜索

配置 RestHighLevelClient 进行注入

代码语言:javascript复制
@Configuration
public class GulimallElasticSearchConfig {
    @Bean
    RestHighLevelClient client() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.107.132", 9200, "http"));
        return new RestHighLevelClient(builder);
    }

    public  static  final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder=RequestOptions.DEFAULT.toBuilder();

        COMMON_OPTIONS=builder.build();
    }
}

REST高级客户端实例需要REST低级客户端构建器

存储数据
代码语言:javascript复制
    @Test
    public void indexData() throws IOException {
        System.out.println(client);
        IndexRequest indexRequest = new IndexRequest("users1");
        indexRequest.id("1");
//        indexRequest.source("userName","张三","age",18,"gender","男");
        User user = new User();
        user.setAge(18);
        user.setUserName("tom");
        user.setGender("男");
        String s = JSON.toJSONString(user);
        indexRequest.source(s, XContentType.JSON);//要保存的内容
        //执行保存操作
        IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
        System.out.println(index);
    }
    @Data
    class  User{
        private String userName;
        private  String gender;
        private  Integer age;

    }
SearchRequest

SearchRequest可用于与搜索文档、聚合、建议有关的任何操作,还提供请求突出显示结果文档的方法。 

 在最基本的形式中,我们可以向请求添加查询:

代码语言:javascript复制
SearchRequest searchRequest = new SearchRequest(); 
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
searchSourceBuilder.query(QueryBuilders.matchAllQuery()); 
searchRequest.source(searchSourceBuilder); 

 创建搜索请求。如果没有参数,这将针对所有索引运行。

大多数搜索参数都添加到SearchSourceBuilder中。它为进入搜索请求主体的所有内容提供了设置器 

代码语言:javascript复制
    @Test
    public void searchTest() throws IOException {
       //1.创建一个检索请求
        SearchRequest searchRequest = new SearchRequest();
        //2.指定索引
        searchRequest.indices("bank");
        //指定DSL,检索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query();
//        searchSourceBuilder.from();
//        searchSourceBuilder.size();
        searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
        System.out.println(searchSourceBuilder.toString());
        //1) 按照年龄聚合
        TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
        searchSourceBuilder.aggregation(ageAgg);


        //2)计算平均工资
        AvgAggregationBuilder banlanceAgg = AggregationBuilders.avg("banlanceAgg").field("balance");
        searchSourceBuilder.aggregation(banlanceAgg);
        System.out.println(searchSourceBuilder.toString());
//        ageAgg.subAggregation(banlanceAgg);
//        System.out.println(searchSourceBuilder.toString());
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
        //分析结果
        System.out.println(searchResponse.toString());
        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
        Aggregations aggregations = searchResponse.getAggregations();
        Terms ageAgg1 = aggregations.get("ageAgg");
        for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
            Number keyAsNumber = bucket.getKeyAsNumber();

            String keyAsString = bucket.getKeyAsString();
            System.out.println("年龄:" keyAsString "---->" bucket.getDocCount());

        }
        Avg banlanceAgg1 = aggregations.get("banlanceAgg");
        double value = banlanceAgg1.getValue();
        System.out.println("balance" value);

    }

执行搜索返回的SearchResponse提供了有关搜索执行本身的详细信息以及对返回文档的访问。首先,有一些关于请求执行本身的有用信息,如HTTP状态代码、执行时间或请求是否提前终止或超时

0 人点赞