使用JestClient操作ElasticSearch

本文详细介绍了如何在Spring Boot应用中使用JestClient与ElasticSearch进行交互,包括配置Maven依赖,设置配置文件,以及索引管理、文档管理、批量操作和异步操作等核心功能的实现。
摘要由CSDN通过智能技术生成

1. 前言

ElasticSearch是一个在全文搜索引擎库Apache Lucene基础之上建立的开源服务,它提供了一个分布式、高扩展、高实时的搜索与数据分析引擎。

在Spring Boot中集成ElasticSearch有Spring Data Elasticsearch、REST Client和Jest等方式。其中Jest作为一个用于ElasticSearch的HTTP Java 客户端,提供了更流畅的API和更容易使用的接口。

本文将介绍Jest的基本用法。

2. Maven依赖

Maven依赖

<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</version>
</dependency>
 
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.2.4</version>
</dependency>

3. 配置文件

配置比较简单,只需要在application.yml中添加相应配置:

spring:
    elasticsearch:
      jest:
        uris: https://ceshi.elastic.feibo.com
        username: elastic
        password: 123456
        read-timeout: 1000s
        connection-timeout: 1000s
        multi-threaded: true

4. 操作

由于添加了Jest配置,只需要注入JestClient就可以直接使用,该客户端连接到本地运行的Elasticsearch。

JestClient类是通用类,只有少数公共方法。我们将使用的一个主要方法是execute,它接受Action接口的一个实例。Jest客户端提供了几个构建器类来帮助创建与ElasticSearch交互的不同操作:

@Resource
private JestClient jestClient;

所有Jest调用的结果都是JestResult的一个实例。我们可以定义一个checkResult方法来检查是否成功。对于失败的操作,我们可以调用getJsonString方法来获取更多详细信息或进行其他操作:

private boolean checkRes(JestResult jestResult) {
   
        if (!jestResult.isSucceeded()) {
   
            logger.error(jestResult.getResponseCode() + jestResult.getJsonString());
            throw new RuntimeException(jestResult.getJsonString());
        }
        return true;
    }
4.1 索引管理

创建索引使用CreateIndex操作,可以选择是否手动设置settings值:

public boolean createIndex(String indexName, String settings) {
   
        try {
   
            JestResult jestResult;
            if (null == settings) {
   
                jestResult = jestClient.execute(new CreateIndex.Builder(indexName).build());
            } else {
   
                jestResult = jestClient.execute(new CreateIndex.Builder(indexName).settings(Settings.builder().loadFromSource(settings, XContentType.JSON).build()).build());
            }
            return checkRes(jestResult);
        } catch (Exception e) {
   
            throw new RuntimeException(e);
        }
    }

设置mapping使用PutMapping操作:

public boolean createIndexMapping(String indexName, String typeName, String mappingString) {
   
        try {
   
            JestResult jestResult = jestClient.execute(new PutMapping.Builder(indexName, typeName, mappingString).build());
            return checkRes(jestResult);
        } catch (Exception e) {
   
            throw new RuntimeException(e);
        }
    }
4.2 文档管理

Jest可以使用最基本的Json字符串格式进行储存,也可以接受表示要索引的文档的任何POJO。为了方便文档操作,我们可以先创建一个DTO类来定义文档:

package com.xx.xx.bean.dto.common;
 
import io.searchbox.annotations.JestId;
import io.searchbox.annotations.JestVersion;
 
/**
 * ES文档对应字段
 *
 * @author LKET
 * @date 2019/6/21 下午2:50
 */
public class StatisticsDTO {
   
 
    /**
     * 文档id
     */
    @JestId
    private String documentId;
 
    /**
     * 文档版本
     */
    @JestVersion
    private Long documentVersion;
 
    /**
     * 页面名称
     */
    private String pageName;
 
    /**
     * 按钮位置名称
     */
    private String locationName;
 
    /**
     * 用户id
     */
    private Integer userId;
 
    /**
     * 数量
     */
    private Integer quantity;
 
    /**
     * 创建时间
     */
    private Long createdAt;
 
    public String getDocumentId() {
   
        return documentId;
    }
 
    public void setDocumentId(String documentId) {
   
        this.documentId = documentId;
    }
 
    public Long getDocumentVersion() {
   
        return documentVersion;
    }
 
    public void setDocumentVersion(Long documentVersion) {
   
        this.documentVersion = documentVersion;
    }
 
    public String getPageName() {
   
        return pageName;
    }
 
    public void setPageName(String pageName) {
   
        this.pageName = pageName;
    }
 
    public String getLocationName() {
   
        return locationName;
    }
 
    public void setLocationName(String locationName) {
   
        this.locationName = locationName;
    }
 
    public Integer getUserId() {
   
        return userId;
    }
 
    public void setUserId(Integer userId) {
   
        this.userId = userId;
    }
 
    public Integer getQuantity() {
   
        return quantity;
    }
 
    public void setQuantity(Integer quantity) {
   
        this.quantity = quantity;
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值