Easy-Es Tutorial: Simplify Elasticsearch Operations with a Java ORM Framework
This article introduces Easy-Es, a Java ORM framework built on Elasticsearch's RestHighLevelClient that simplifies query construction and CRUD operations, provides step‑by‑step setup including Maven dependencies, configuration, entity and mapper definitions, controller examples, and advanced features such as condition builders, index management, logging, and aggregation queries.
0. Introduction
Elasticsearch's Java client has a cumbersome syntax; Easy-Es provides an ORM similar to MyBatis‑Plus, allowing developers to write MySQL‑like queries.
1. Easy-Es Overview
Easy-Es builds on the official RestHighLevelClient and offers annotations such as @IndexName and @IndexField so that users can define indexes and fields without mastering Elasticsearch DSL.
2. Using Easy-Es
1) Add Maven dependencies (example shown).
2) Configure basic properties (address, username, password).
easy-es:
address: 192.168.244.11:9200
username: elastic
password: elastic3) Scan mapper packages with @EsMapperScan.
@EsMapperScan("com.example.easyesdemo.mapper")4) Define entity classes using @IndexName, @IndexId and @IndexField annotations.
@IndexName(value = "user_easy_es")
@Data
public class UserEasyEs {
@IndexId(type = IdType.CUSTOMIZE)
private Long id;
private String name;
private Integer age;
private Integer sex;
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_SMART)
private String address;
@IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private Date createTime;
private String createUser;
}5) Create a mapper interface extending BaseEsMapper.
public interface UserEsMapper extends BaseEsMapper
{}6) Implement a REST controller with endpoints for creating indexes, inserting/updating records, and searching.
@RestController
@RequestMapping("es")
@AllArgsConstructor
public class UserEsController {
private final UserEsMapper userEsMapper;
@GetMapping("create")
public Boolean createIndex() { return userEsMapper.createIndex(); }
@GetMapping("save")
public Integer save(Long id) {
// build UserEasyEs and insert or update
}
@GetMapping("search")
public List
search(String name, String address) {
return userEsMapper.selectList(
EsWrappers.lambdaQuery(UserEasyEs.class)
.eq(UserEasyEs::getName, name)
.match(UserEasyEs::getAddress, address));
}
}3. Advanced Features
Condition builder: use EsWrappers similar to MyBatis‑Plus.
Index management modes: smoothly, not_smoothly, manual; configure via process_index_mode .
easy-es:
global-config:
process_index_mode: not_smoothlyData synchronization: recommend Canal, DataX, Logstash, or manual CRUD.
Logging: enable trace level to print DSL statements.
logging:
level:
tracer: traceAggregation queries: Easy‑Es supports Terms aggregation and other metrics; example controller shows grouping by status.
@GetMapping("search")
public String search() {
SearchResponse search = orderEsMapper.search(
EsWrappers.lambdaQuery(OrderTest.class).groupBy(OrderTest::getStatus));
// process Aggregations...
return statusRes.toString();
}4. Conclusion
Easy‑Es greatly simplifies Elasticsearch CRUD operations with a MyBatis‑Plus‑like API, though complex aggregations may still require the underlying RestHighLevelClient.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.