1. Easy-ESの概要
Easy-Es(略してEE)は、国内の開発者によって作成された完全にオープンソースのElasticSearch-ORMフレームワークです。ネイティブの RestHighLevelClient をベースに、変更を加えずに機能強化のみを行い、開発を簡素化し、効率を向上させるために作成されています。Easy-Es は MP と同じ構文設計を採用しているため、ElasticSearch 検索エンジンを使用するためのしきい値と追加の学習コストが削減され、開発者の作業負荷が大幅に軽減され、企業のコスト削減と効率の向上に役立ちます。いくつかの側面では、MP よりもさらにシンプルであり、さまざまなシナリオの開発を迅速に実現するのに役立つ ElasticSearch のよりユニークな機能も組み込まれています。(esを操作するmpとも解釈できます)
公式サイトアドレス:https://www.easy-es.cn/
プロジェクトアドレス: https://gitee.com/dromara/easy-es/
オープンソース コミュニティ: https://gitee.com/dromara/
EE特性:
-
非侵入型: 機能強化のみで変更は行われません。導入は既存のプロジェクトに影響を与えず、シルクのようにスムーズです。
-
低損失: 基本的な CURD は起動時に自動的に挿入され、基本的にパフォーマンスの損失はなく、直接オブジェクト指向の操作が行われます。
-
強力な CRUD 操作: ユニバーサル マッパーが組み込まれており、ほとんどの CRUD 操作はわずかな構成で実装できます。また、さまざまな使用ニーズを満たす強力な条件付きコンストラクターもあります。
-
Lambda フォーム呼び出しのサポート: Lambda 式を通じて、間違ったフィールドを記述することを心配することなく、さまざまなクエリ条件を簡単に記述することができます。
-
主キーの自動生成をサポート: 2 つの主キー戦略をサポートしており、主キーの問題を完全に解決するために自由に構成できます。
-
ActiveRecord モードのサポート: ActiveRecord フォーム呼び出しをサポートします。エンティティ クラスは Model クラスを継承するだけで強力な CRUD 操作を実行できます。
-
カスタムのグローバル ユニバーサル オペレーションをサポート: グローバル ユニバーサル メソッド インジェクションをサポートします (一度書いたらどこでも使用できます)。
-
組み込みのページング プラグイン: RestHighLevelClient 物理ページングに基づいており、開発者は特定の操作を気にする必要がなく、追加のプラグインを構成する必要もありません。ページングの記述は通常のリスト クエリと同等であり、同じページングを維持します。効果の名前付けを気にせずに、PageHelper プラグインとしてフィールドを返します。
-
MySQL 関数を完全にカバー: MySQL でサポートされているすべての関数は、EE を通じて簡単に実装できます。
-
ES 高レベル構文のサポート: ハイライト検索、単語分割クエリ、重み付けクエリ、地理的位置クエリ、IP クエリ、集計クエリ、その他の高レベル構文をサポートします。
-
優れたスケーラビリティ: 最下層ではスケーラビリティを維持できる RestHighLevelClient を使用しており、開発者は EE を使用しながらも RestHighLevelClient の機能を使用できます。
EE フレームワーク構造:
EE テクノロジーと Spring Data ES テクノロジーの比較:
2. Easy-ESによるクイックスタート
1.Mavenプロジェクトを作成する
2. 依存関係を導入します。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- springboot工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.aopmin</groupId>
<artifactId>EE-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--es相对稳定版本,esay-es会依赖这个版本的jar包-->
<elasticsearch.version>7.14.0</elasticsearch.version>
</properties>
<dependencies>
<!-- web,排除springboot内置的es依赖,以防和easy-es中的依赖冲突 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- easy-es -->
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>2.0.0-beta3</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. スタートアップ クラスを作成します。
package cn.aopmin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EEApplication {
public static void main(String[] args) {
SpringApplication.run(EEApplication.class, args);
}
}
4. application.yml で easy-es を設定します。
easy-es:
address: 192.168.150.123:9200
# username: XXXX #如果es无账号密码则可不配置此行
# password: XXXX #如果es无账号密码则可不配置此行
5. エンティティ クラスを作成します。
package cn.aopmin.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexId;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;
/**
* ES实体类
*
* @author 白豆五
* @version 2023/07/26
* @since JDK8
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@IndexName("document") // 指定索引名称
public class Document {
/**
* es中的唯一id
*/
@IndexId(type = IdType.CUSTOMIZE) //id生成策略为自定义
private String id;
/**
* 文档标题
*/
@IndexField( fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD, searchAnalyzer = Analyzer.IK_SMART)
private String title;
/**
* 文档内容
*/
@IndexField( fieldType = FieldType.KEYWORD)
private String content;
}
6. マッパーインターフェイスを作成します。
package cn.aopmin.mapper;
import cn.aopmin.pojo.Document;
import org.dromara.easyes.core.core.BaseEsMapper;
/**
* Mapper接口
*
* @author 白豆五
* @version 2023/07/26
* @since JDK8
*/
public interface DocumentMapper extends BaseEsMapper<Document> {
}
7. スタートアップ クラスでパッケージ スキャンを構成します: (@EsMapperScan)
package cn.aopmin;
import org.dromara.easyes.starter.register.EsMapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EsMapperScan("cn.aopmin.mapper") // 指定Mapper接口所在的包名
@SpringBootApplication
public class EEApplication {
public static void main(String[] args) {
SpringApplication.run(EEApplication.class, args);
}
}
8. テスト
例 1: インデックス ライブラリを作成する
package cn.aopmin.test;
import cn.aopmin.mapper.DocumentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* EasyEs快速入门测试类
*
* @author 白豆五
* @version 2023/07/26
* @since JDK8
*/
@SpringBootTest
public class EasyEsTest {
@Resource
private DocumentMapper documentMapper;
/**
* 测试创建索引库
*/
@Test
public void testCreateIndex() {
//创建索引,根据当前mapper对应实体类信息及其注解配置生成索引信息
Boolean success = documentMapper.createIndex();
System.out.println(success ? "创建索引成功" : "创建索引失败");
}
}
例 2: ドキュメントの追加
/**
* 测试添加文档
*/
@Test
public void testAddDocument() {
// 持久层添加数据跟mp类似也是insert方法
documentMapper.insert(new Document("1", "程序员的幽默", "为了让你开心,我特意编写了一段关于程序员的小笑话,希望你能喜欢。"));
}
例 3: テスト クエリのタイトルにはプログラマのドキュメント リストが含まれています (結果の解析も自動的に行われます)
/**
* 测试查询title中包含程序员的文档列表
*/
@Test
public void testSearch() {
// 条件构造器
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle, "程序"); // 类似match查询
// 查询文档列表
List<Document> list = documentMapper.selectList(wrapper);
list.forEach(System.out::println);
}