1. 创建ES索引和映射
```
// 创建索引
PUT /order_index
// 创建映射
PUT /order_index/_mapping
{
"properties": {
"order_id": {
"type": "long"
},
"order_name": {
"type": "text"
},
"order_status": {
"type": "text"
},
"create_time": {
"type": "date"
}
}
}
```
2. 从Mysql分表查询数据,并将数据插入到ES中
```
// 从Mysql分表查询数据
for (int i = 0; i < 10; i++) {
String sql = "select order_id, order_name, order_status, create_time from order_table_" + i;
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
// 遍历结果集,将数据插入到ES中
Long orderId = rs.getLong("order_id");
String orderName = rs.getString("order_name");
String orderStatus = rs.getString("order_status");
String createTime = rs.getString("create_time");
// 插入ES
IndexRequest request = new IndexRequest("order_index")
.id(orderId.toString())
.source("order_id", orderId,
"order_name", orderName,
"order_status", orderStatus,
"create_time", createTime);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
}
}
```
3. 使用ES查询订单列表数据
```
SearchRequest searchRequest = new SearchRequest("order_index");
// 构建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("order_status", "CREATED"));
// 搜索
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 获取搜索结果
SearchHits searchHits = response.getHits();
for (SearchHit hit : searchHits.getHits()) {
Map<String, Object> source = hit.getSourceAsMap();
System.out.println(source);
}
```