8.2 SpringBoot集成ElasticSearch之新增文档
Posted lwen.steven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8.2 SpringBoot集成ElasticSearch之新增文档相关的知识,希望对你有一定的参考价值。
1.entity开发
在项目目录“/src/main/java/com/example/es”下新建entity目录,并在entity目录下新建EmployeeEntity实体类,具体代码如下。
@Data
public class EmployeeEntity
@JsonProperty("employeeId")
private String employeeId;
@JsonProperty("name")
private String name;
@JsonProperty("age")
private Integer age;
@JsonProperty("birthday")
private String birthday;
@JsonProperty("job")
private String job;
@JsonProperty("salary")
private Float salary;
2.mapper开发
在项目目录“/src/main/java/com/example/es”下新建mapper目录,并在mapper目录下新建EmployeeMapper接口,具体代码如下。
@EasyMapper(indices = "employee", clusterRouter = "sampleCluster")
public interface EmployeeMapper
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回true
*/
@Index
Boolean indexEmployeeBoolean(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回文档id
*/
@Index
String indexEmployeeString(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
*/
@Index
void indexEmployeeVoid(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回文档IndexResponse
*/
@Index
IndexResponse indexEmployeeIndexResponse(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回RestStatus状态码
*/
@Index
RestStatus indexEmployeeRestStatus(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回异步RestStatus状态码
*/
@Index
CompletableFuture<RestStatus> indexEmployeeCompletableFuture(EmployeeEntity employeeEntity);
/**
* 新建一名员工
*
* @param employeeEntity 员工
* @return 新建成功,返回异步结果
*/
@Index
CompletableFuture<Void> indexEmployeeFutureVoid(EmployeeEntity employeeEntity);
3.controller开发
在项目目录“/src/main/java/com/example/es”下新建controller目录,并在controller目录下新建EmployeeController类,以indexEmployeeString接口为例演示开发步骤,具体代码如下。
@RestController
@RequestMapping("/employee")
public class EmployeeController
@Resource
private EmployeeMapper employeeMapper;
@ResponseBody
@RequestMapping(value = "/indexEmployeeString", method = RequestMethod.POST)
public String indexEmployeeString(@RequestBody EmployeeCondition employeeCondition)
return employeeMapper.indexEmployeeString(employeeCondition);
4.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/indexEmployeeString”,成功后返回文档id。
"employeeId":10000008,
"name": "Damian Lillard",
"job": "Vue engineer",
"age": 25,
"salary": 18000.00,
"birthday": "1996-10-01"
qfuPv3wBpyKimLS2Ar_q
5.@Index属性说明
序号 | 属性名 | 默认值 | 说明 |
---|---|---|---|
1 | timeout | 1m | 数值+时间单位(ms/s/m/h/M/y) |
2 | refreshPolicy | RefreshPolicy.NONE | 默认不刷新 |
3 | pipeline | 可选 | |
4 | versionType | VersionType.INTERNAL | |
5 | waitForActiveShards | -2 | 活动分片数量,-2时不指定分片数量,-1或all指定全部分片 |
6.指定文档id新增(一般将文档id的值设置为文档主键字段)
(1).入参实体需实现IdProvider接口
@Data
public class EmployeeEntity implements IdProvider
@JsonProperty("id")
private String id;
@JsonProperty("employeeId")
private String employeeId;
@JsonProperty("name")
private String name;
@JsonProperty("age")
private Integer age;
@JsonProperty("birthday")
private String birthday;
@JsonProperty("job")
private String job;
@JsonProperty("salary")
private Float salary;
@Override
public String getId()
return id;
(2).测试
启动项目,然后在postman中请求“http://localhost:8080/employee/indexEmployeeString”,成功后返回文档id。
"id":"10000009",
"employeeId":10000009,
"name": "Damian Lillard",
"job": "Vue engineer",
"age": 25,
"salary": 18000.00,
"birthday": "1996-10-01"
10000009
7.批量新增
(1).mapper开发
在项目目录“/src/main/java/com/example/es”的EmployeeMapper类中新增批量创建员工接口,具体代码如下。
@EasyMapper(indices = "employee", clusterRouter = "sampleCluster")
public interface EmployeeMapper
/**
* 批量创建员工
*
* @param employeeEntityList 员工
* @return 响应
*/
@Bulk(bulkType = BulkType.INDEX)
List<BulkItemResponse> bulkIndexEmployeeList(List<EmployeeEntity> employeeEntityList);
/**
* 批量创建员工
*
* @param employeeEntities 员工
* @return 响应
*/
@Bulk(bulkType = BulkType.INDEX)
List<BulkItemResponse> bulkIndexEmployeeList(EmployeeEntity... employeeEntities);
/**
* 批量创建员工
*
* @param employeeEntities 员工
* @return 响应
*/
@Bulk(bulkType = BulkType.INDEX)
BulkItemResponse[] bulkIndexEmployeeArray(EmployeeEntity... employeeEntities);
/**
* 批量创建员工
*
* @param employeeEntities 员工
* @return 响应
*/
@Bulk(bulkType = BulkType.INDEX)
BulkResponse bulkIndexEmployeeBulkResponse(EmployeeEntity... employeeEntities);
(2).controller开发
在项目目录“/src/main/java/com/example/es”下的EmployeeController类中新增批量创建员工接口,以接口bulkIndexEmployeeList为例演示开发步骤,具体代码如下。
@RestController
@RequestMapping("/employee")
public class EmployeeController
@Resource
private EmployeeMapper employeeMapper;
@ResponseBody
@RequestMapping(value = "/bulkIndexEmployeeList", method = RequestMethod.POST)
public List<BulkItemResponse> bulkIndexEmployeeList(@RequestBody List<EmployeeEntity> employeeConditionList)
return employeeMapper.bulkIndexEmployeeList(employeeConditionList);
(3).测试
启动项目,然后在postman中请求“http://localhost:8080/employee/bulkIndexEmployeeList”,成功后返回对应的信息。
[
"id": "10000011",
"employeeId": 10000011,
"name": "Stephen Curry",
"job": "Java engineer",
"age": 27,
"salary": 20000.00,
"birthday": "1995-08-06"
,
"id": "10000012",
"employeeId": 10000012,
"name": "Stephen Curry",
"job": "Java engineer",
"age": 27,
"salary": 20000.00,
"birthday": "1995-08-06"
]
[
"id": "10000011",
"opType": "INDEX",
"response":
"shardInfo":
"total": 2,
"successful": 1,
"failures": [],
"failed": 0,
"fragment": false
,
"shardId":
"index":
"name": "employee",
"uuid": "_na_",
"fragment": false
,
"id": -1,
"indexName": "employee",
"fragment": true
,
"id": "10000011",
"type": "_doc",
"version": 1,
"seqNo": 4,
"primaryTerm": 1,
"result": "CREATED",
"index": "employee",
"fragment": false
,
"failure": null,
"type": "_doc",
"index": "employee",
"version": 1,
"failureMessage": null,
"itemId": 0,
"failed": false,
"fragment": false
,
"id": "10000012",
"opType": "INDEX",
"response":
"shardInfo":
"total": 2,
"successful": 1,
"failures": [],
"failed": 0,
"fragment": false
,
"shardId":
"index":
"name": "employee",
"uuid": "_na_",
"fragment": false
,
"id": -1,
"indexName": "employee",
"fragment": true
,
"id": "10000012",
"type": "_doc",
"version": 1,
"seqNo": 4,
"primaryTerm": 1,
"result": "CREATED",
"index": "employee",
"fragment": false
,
"failure": null,
"type": "_doc",
"index": "employee",
"version": 1,
"failureMessage": null,
"itemId": 1,
"failed": false,
"fragment": false
]
(4).注解
@Bulk注解标明此方法是批量方法,同时必须要指定BulkType标明是何种类型的批量操作。做批量更新操作时,如果想文档不存在则做插入操作,设置@Update(docAsUpsert=true)。
(5).@Bulk属性说明
序号 | 属性名 | 默认值 | 说明 |
---|---|---|---|
1 | bulkType | 必填 | 可选类型:INDEX/DELETE/UPDATE |
2 | timeout | 1m | 数值+时间单位(ms/s/m/h/M/y) |
3 | waitForActiveShards | -2 | 活动分片数量,-2时不指定分片数量,-1或all指定全部分片 |
4 | index | 可选 | bulkType = INDEX有效 |
5 | delete | 可选 | bulkType = DELETE有效 |
6 | update | 可选 | bulkType = UPDATE有效 |
7 | refreshPolicy | RefreshPolicy.NONE |
以上是关于8.2 SpringBoot集成ElasticSearch之新增文档的主要内容,如果未能解决你的问题,请参考以下文章
8.2 sikuli 集成进eclipse 报错:Getting the VisionProxy.dll: Can not find dependent libraries...
springboot+elasticsearch + rabbitMQ实现全文检索(springboot+ES整合)
Linux??????ELK?????????????????????FIlebeat+Redis+Logstash+Elasticse