1.condition开发
在项目目录“/src/main/java/com/example/es”下新建condition目录,并在condition目录下新建EmployeeCondition查询条件类,具体代码如下。
@Data public class EmployeeCondition { @Field("employeeId") private String employeeId; @Field("name") private String name; @Field("age") private Integer age; @Field("birthday") private Date birthday; @Field("job") private String job; @Field("salary") private Float salary; }
2.mapper开发
在项目目录“/src/main/java/com/example/es”的EmployeeMapper类中新增查询员工信息接口,具体代码如下。
@EasyMapper(indices = "employee", clusterRouter = "sampleCluster") public interface EmployeeMapper { @Get EmployeeEntity getEmployee(EmployeeCondition employeeCondition); @Get EmployeeEntity getEmployee(String id); @Get OptionalgetEmployeeOptional(String id); @Get GetResponse getEmployeeGetResponse(String id); @Get CompletableFuture > getEmployeeCompletableFuture(String id); }
3.controller开发
在项目目录“/src/main/java/com/example/es”下的EmployeeController类中新增查询员工信息接口,以indexEmployeeString接口为例演示开发步骤,具体代码如下。
@RestController @RequestMapping("/employee") public class EmployeeController { @Resource private EmployeeMapper employeeMapper; @ResponseBody @RequestMapping(value = "/getEmployee", method = RequestMethod.POST) public EmployeeEntity getEmployee(@RequestParam String id) { return employeeMapper.getEmployee(id); } }
4.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/getEmployee?id=4SB70x0B68NPz8Jszbaa”,成功后返回文档id所对应的文档信息。
{ "id": null, "employeeId": "10000002", "name": "Stephen Curry", "age": 27, "birthday": "1995-08-06", "job": "Java engineer", "salary": 20000.0 }
5.@Get属性说明
6.Multi Get API
(1).mapper开发
在项目目录“/src/main/java/com/example/es”的EmployeeMapper类中新增批量查询员工信息接口,具体代码如下。
@EasyMapper(indices = "employee", clusterRouter = "sampleCluster") public interface EmployeeMapper { @MultiGet MultiGetResponse getEmployeesResponse(String... ids); @MultiGet ListgetEmployees(String... ids); @MultiGet MultiGetItemResponse[] getEmployeesItemResponse(String... ids); @MultiGet EmployeeEntity[] getEmployees(List employeeConditionList); @MultiGet List getEmployeesItemResponse(List employeeConditionList); @MultiGet List > getEmployeesOptional(String... ids); @MultiGet Optional [] getEmployeesOptional(List employeeConditionList); @MultiGet CompletableFuture []> getEmployeesOptionalFuture(List employeeConditionList); }
(2).controller开发
在项目目录“/src/main/java/com/example/es”下的EmployeeController类中新增批量查询员工信息接口,以接口getEmployeesResponse为例演示开发步骤,具体代码如下。
@RestController @RequestMapping("/employee") public class EmployeeController { @Resource private EmployeeMapper employeeMapper; @ResponseBody @RequestMapping(value = "/getEmployeesResponse", method = RequestMethod.POST) public MultiGetResponse getEmployeesResponse(@RequestBody ListintegerList) { return employeeMapper.getEmployeesResponse(integerList.toArray(new String[integerList.size()])); } }
(3).测试
启动项目,然后在postman中请求“http://localhost:8080/employee/getEmployeesResponse”,成功后返回对应的信息。
["4iB70x0B68NPz8Js6rb3","5iB80x0B68NPz8JsY7aI"]
{ "responses": [ { "response": { "fields": {}, "id": "4iB70x0B68NPz8Js6rb3", "type": "_doc", "index": "employee", "source": { "birthday": "1987-12-25", "name": "LeBron James", "employeeId": 10000003, "job": "Technical director", "salary": 50000.0, "age": 35 }, "version": 2, "primaryTerm": 1, "seqNo": 18, "sourceAsBytes": "eyJlbXBsb3llZUlkIjoxMDAwMDAwMywibmFtZSI6IkxlQnJvbiBKYW1lcyIsImpvYiI6IlRlY2huaWNhbCBkaXJlY3RvciIsImFnZSI6MzUsInNhbGFyeSI6NTAwMDAuMCwiYmlydGhkYXkiOiIxOTg3LTEyLTI1In0=", "exists": true, "sourceEmpty": false, "sourceAsMap": { "birthday": "1987-12-25", "name": "LeBron James", "employeeId": 10000003, "job": "Technical director", "salary": 50000.0, "age": 35 }, "sourceInternal": { "fragment": true }, "sourceAsString": "{"employeeId":10000003,"name":"LeBron James","job":"Technical director","age":35,"salary":50000.0,"birthday":"1987-12-25"}", "sourceAsBytesRef": { "fragment": true }, "fragment": false }, "failure": null, "id": "4iB70x0B68NPz8Js6rb3", "type": "_doc", "index": "employee", "failed": false }, { "response": { "fields": {}, "id": "5iB80x0B68NPz8JsY7aI", "type": "_doc", "index": "employee", "source": { "birthday": "1997-08-02", "name": "Jason Tatum", "employeeId": 10000007, "job": "Java engineer", "salary": 15000.0, "age": 24 }, "version": 3, "primaryTerm": 1, "seqNo": 4, "sourceAsBytes": "eyJlbXBsb3llZUlkIjoxMDAwMDAwNywibmFtZSI6Ikphc29uIFRhdHVtIiwiam9iIjoiSmF2YSBlbmdpbmVlciIsImFnZSI6MjQsInNhbGFyeSI6MTUwMDAuMCwiYmlydGhkYXkiOiIxOTk3LTA4LTAyIn0=", "exists": true, "sourceEmpty": false, "sourceAsMap": { "birthday": "1997-08-02", "name": "Jason Tatum", "employeeId": 10000007, "job": "Java engineer", "salary": 15000.0, "age": 24 }, "sourceInternal": { "fragment": true }, "sourceAsString": "{"employeeId":10000007,"name":"Jason Tatum","job":"Java engineer","age":24,"salary":15000.0,"birthday":"1997-08-02"}", "sourceAsBytesRef": { "fragment": true }, "fragment": false }, "failure": null, "id": "5iB80x0B68NPz8JsY7aI", "type": "_doc", "index": "employee", "failed": false } ], "fragment": false }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)