首页 > 编程语言 >(十一):ES的编程操作

(十一):ES的编程操作

时间:2024-01-18 17:48:40浏览次数:30  
标签:检索 十一 searchRequest 编程 sourceBuilder private client new ES

1、ES API的两种方式

  Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClientAPI两种。相比来说transportClient API效率更高,transportClient是通过Elasticsearch内部RPC的形式进行请求的,连接可以是一个长连接,相当于是把客户端的请求当成Elasticsearch 集群的一个节点, REST Client API 也支持httpd的keepAlive形式的长连接,只是非内部RPC形式。

  Elasticsearch 7 后就会移除transportClient,主要原因是transportClient 难以向下兼容版本。

1.1 9300[TCP]

  利用9300端口的是spring-data-elasticsearch:transport-api.jar,但是这种方式因为对应的SpringBoot版本不一致,造成对应的transport-api.jar也不同,不能适配es的版本,而且ElasticSearch7.x中已经不推荐使用了,ElasticSearch 8之后更是废弃。

1.2 9200[HTTP]

  基于9200端口的方式也有多种,详情如下:

JsetClient

非官方,更新缓慢

RestTemplate

模拟发送Http请求,ES很多的操作需要我们自己来封装,效率低

HttpClient

与RestTemplate的情况一样

ElasticSearch-Rest-Client

官方的RestClient,封装了ES的操作,API层次分明,易于上手

JavaAPIClient

7.15版本后推荐

  下面重点来看ElasticSearch-Rest-Client的构建。

2、ElasticSearch-Rest-Client的应用

2.1、添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.4.12</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2.2、添加ES配置

2.2.1、ES客户端配置

  文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html

   客户端配置如下:

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: ElasticSearch的配置类
 * @author: snails
 */
@Configuration
public class ElasticSearchConfiguration {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 构建ES客户端
        RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}

  测试从Spring容器中获取client对象:

import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Description:
 * @author: snails
 */
@SpringBootTest
public class ESApplicationTests {

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void contextLoads() {
        System.out.println("====> " + client);
    }
}

  输出结果不为空,表示连接ES服务成功。

0

2.2.2、设置RequestOptions

  文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/java-rest-low-usage-requests.html#java-rest-low-usage-request-options

0

  在 ElasticSearchConfiguration 中添加如下内容:

private static final RequestOptions COMMON_OPTIONS;
static {
    RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    COMMON_OPTIONS = builder.build();
}

2.3、API使用

2.3.1、保存文档

@Test
public void addDocument() throws IOException {
    // 创建索引对象
    IndexRequest indexRequest = new IndexRequest("java_api");
    // 设置id
    indexRequest.id("1");
    // 创建文档对象
    Student stu = new Student();
    stu.setStuno("20130911");
    stu.setAge(15);
    stu.setName("张三");
    // 用Jackson中的对象转json数据
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(stu);
    indexRequest.source(json, XContentType.JSON);
    // 执行操作
    IndexResponse index = client.index(indexRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 提取返回信息
    System.out.println(index);
}

@Data
class Student{
    private String stuno;
    private String name;
    private Integer age;
}

  执行结果如下:

0

  kibana查询结果如下:

0

2.3.2、检索文档

2.1、检索出items索引的所有文件

@Test
public void testSearchIndexAllDoc() throws IOException {
    // 1.创建一个 SearchRequest 对象
    SearchRequest searchRequest = new SearchRequest();
    // 设置检索数据对应的索引库
    searchRequest.indices("items");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    searchRequest.source(sourceBuilder);
    // 2.执行检索操作
    SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 3.获取检索后的响应对象,解析出需要的数据
    System.out.println("ElasticSearch检索的信息:"+response);
}

  执行结果如下:

0

2.2、根据address全文检索

@Test
void searchIndexByAddress() throws IOException {
    // 1.创建一个 SearchRequest 对象
    SearchRequest searchRequest = new SearchRequest();
    // 设置检索数据对应的索引库
    searchRequest.indices("items");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 查询出items下 address 中包含 586 的记录
    sourceBuilder.query(QueryBuilders.matchQuery("address","586"));
    searchRequest.source(sourceBuilder);

    // 2.执行检索操作
    SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 3.获取检索后的响应对象解析出需要的数据
    System.out.println("ElasticSearch检索的信息:"+response);
}

  执行结果如下:

0

2.3、嵌套的聚合操作:检索出 items 下的年龄分布和每个年龄段的平均薪资

/**
 * 聚合:嵌套聚合
 * @throws IOException
 */
@Test
void searchIndexAggregation() throws IOException {
    // 1.创建一个 SearchRequest 对象
    SearchRequest searchRequest = new SearchRequest();
    // 设置检索数据对应的索引库
    searchRequest.indices("items");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 查询出items下 所有的文档
    sourceBuilder.query(QueryBuilders.matchAllQuery());
    // 聚合 aggregation
    // 聚合item下年龄的分布和每个年龄段的平均薪资
    AggregationBuilder aggregationBuiler = AggregationBuilders.terms("ageAgg")
            .field("age")
            .size(2);
    // 嵌套聚合
    aggregationBuiler.subAggregation(AggregationBuilders.avg("balanceAvg").field("balance"));

    sourceBuilder.aggregation(aggregationBuiler);
    // 聚合时,不显示满足条件的文档内容
    sourceBuilder.size(0);
    searchRequest.source(sourceBuilder);
    System.out.println(sourceBuilder);

    // 2.执行检索操作
    SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 3.获取检索后的响应对象解析出需要的数据
    System.out.println(response);
}

  执行结果如下:

0

2.4、并行的聚合操作:查询出items下年龄段的分布和总的平均薪资

/**
 * 聚合
 * @throws IOException
 */
@Test
void searchIndexAggregation02() throws IOException {
    // 1.创建一个 SearchRequest 对象
    SearchRequest searchRequest = new SearchRequest();
    // 设置检索数据对应的索引库
    searchRequest.indices("items");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 查询出items下 所有的文档
    sourceBuilder.query(QueryBuilders.matchAllQuery());
    // 聚合 aggregation
    // 聚合items下年龄的分布和平均薪资
    AggregationBuilder aggregationBuiler = AggregationBuilders.terms("ageAgg")
            .field("age")
            .size(2);

    sourceBuilder.aggregation(aggregationBuiler);
    // 聚合平均年龄
    AvgAggregationBuilder balanceAggBuilder = AggregationBuilders.avg("balanceAgg").field("age");
    sourceBuilder.aggregation(balanceAggBuilder);
    // 聚合时,不显示满足条件的文档内容
    sourceBuilder.size(0);
    searchRequest.source(sourceBuilder);
    System.out.println(sourceBuilder);

    // 2.执行检索操作
    SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 3.获取检索后的响应对象解析出需要的数据
    System.out.println(response);
}

  执行结果如下:

0

2.5、处理检索后的结果

@Test
void searchIndexResponse() throws IOException {
    // 1.创建一个 SearchRequest 对象
    SearchRequest searchRequest = new SearchRequest();
    // 设置检索数据对应的索引库
    searchRequest.indices("items");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 查询出bank下 address 中包含 586 的记录
    sourceBuilder.query(QueryBuilders.matchQuery("address","586"));
    searchRequest.source(sourceBuilder);

    // 2.执行检索操作
    SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
    // 3.解析数据
    RestStatus status = response.status();
    TimeValue took = response.getTook();
    SearchHits hits = response.getHits();
    TotalHits totalHits = hits.getTotalHits();
    TotalHits.Relation relation = totalHits.relation;
    long value = totalHits.value;
    // 相关性的最高分
    float maxScore = hits.getMaxScore();
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit documentFields : hits1) {
        String json = documentFields.getSourceAsString();
        // JSON字符串转换为 Object对象
        ObjectMapper mapper = new ObjectMapper();
        Item item = mapper.readValue(json, Item.class);
        System.out.println("item = " + item);
    }
}

@ToString
@Data
static class Item {

    private int account_number;
    private int balance;
    private String firstname;
    private String lastname;
    private int age;
    private String gender;
    private String address;
    private String employer;
    private String email;
    private String city;
    private String state;

}

  执行结果如下:

0    

标签:检索,十一,searchRequest,编程,sourceBuilder,private,client,new,ES
From: https://www.cnblogs.com/RunningSnails/p/17973004

相关文章

  • KubeSphere 开源社区 2023 年度回顾与致谢
    2023年结束了,让我们再一次一起回顾一下KubeSphere开源社区在过去一年的变化。更重要的是,本篇文章将会对2023年所有参与过KubeSphere社区贡献的成员致以最诚挚的感谢,快来看看有没有你!开源项目发展情况2023年,在国内外社区贡献者的参与下,KubeSphere及KubeSphere社区衍生......
  • PostgreSQL安装部署
    1.使用yum安装1)安装数据库服务器yuminstall-yhttps://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpmyuminstall-ypostgresql15-server2)初始化并启动数据库/usr/pgsql-15/bin/postgresql-15-setupinitdb#默认......
  • python编程中break pass continue这三个有什么区别?
    在Python编程中,break、pass和continue是三种不同的控制流语句,它们各自有不同的用途和行为:(以下内容由百度文心一言生成)   break:       break语句用于终止循环的执行。当程序执行到break语句时,会立即跳出当前循环,不再执行循环内的剩余代码,而是继续执行循环之后的代......
  • 题解 CF741E Arpa’s abnormal DNA and Mehrdad’s deep interest
    CF741EArpa’sabnormalDNAandMehrdad’sdeepinterest记\(R_{i}\)表示把\(T\)插入在\(S\)的第\(i\)位后组成的字符串。有\(q\)组询问,给定\((x,y,l,r)\),求\(\min_{i}R_{i},({i\in[l,r],i\%k\in[x,y]})\)。一个暴力的想法是先把\(R_{i}\)的排名求出来,这显......
  • Flink DataStream API 编程模型
    Flink系列文章第01讲:Flink的应用场景和架构模型第02讲:Flink入门程序WordCount和SQL实现第03讲:Flink的编程模型与其他框架比较第04讲:Flink常用的DataSet和DataStreamAPI第05讲:FlinkSQL&Table编程和案例第06讲:Flink集群安装部署和HA配置第07讲:Flink常见......
  • 解决vue用axiso两次访问后台api,发现后台的sessionID不一致
    我用的是ASP.NETCore7.0做的后台api,在解决了跨域问题(这个问题在官网上就有答案https://learn.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-7.0)为了方便阅读,我再讲一下在里progam里面增加代码(黄色代码),代码格式我就把不变的放到一起了解决完这个之后,因为要......
  • 基于ESP32的CAN总线通讯
    CAN总线的概述:CAN是ControllerAreaNetwork的缩写(以下称为CAN),是ISO国际标准化的串行通信协议。在当今汽车总线中CAN总线是应用最广泛的通信方式。此外CAN由于其高可靠性和高性能使其在工业自动化,船舶,医疗等领域也得到了广泛应用。CAN总线的工作原理:1、CAN总线使用串行数......
  • Wireshark网络工具是什么?
    Wireshark是网络包分析工具。网络包分析工具的主要作用是尝试捕获网络包,并尝试显示包的尽可能详细的情况。Wireshark是一个免费开源软件,不需要付费,免费使用,可以直接登陆到Wireshark的官网下载安装。在windows环境中,Wireshark使用WinPCAP作为接口,直接与网卡进行数据报文交换,这个工具......
  • WhisperForConditionalGeneration requires the PyTorch library but it was not
    实现WhisperForConditionalGeneration所需的PyTorch库概述在这篇文章中,我将向你解释如何实现"WhisperForConditionalGenerationrequiresthePyTorchlibrarybutitwasnotfound"这个错误。作为一名经验丰富的开发者,我将帮助你了解整个过程,并提供详细的代码解释。表格展示步......
  • hey_left 6 Codeforces Round 886 (Div. 4)
    题目链接A.判总和-最小值的差是否大等于10即可#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+10;voidsolve(){inta,b,c;cin>>a>>b>>c;intans=a+b+c;ans-=min({a,b,c});if(ans>=10){cout<<"YES&qu......