sun*_*t02 4 java spring spring-data-elasticsearch
我正在使用spring boot版本1.5.6.RELEASE和spring-boot-starter-data-elasticsearch相同版本的应用程序。
我有一个Greeting像这样的模型:
@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
@Id
private Long id;
private String username;
// Getter, Setter and constructor added here
}
Run Code Online (Sandbox Code Playgroud)
对于以下情况,我的控制器和服务类是相同的,没有id.
当我发送以下帖子请求时:
curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings
Run Code Online (Sandbox Code Playgroud)
它回复:
{"id":null,"username":"sunkuet02","message":"this is test"}
Run Code Online (Sandbox Code Playgroud)
然后我更改了Greeting将类型更改id为字符串的类。
@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
@Id
private String id;
private String username;
// Getter, Setter and constructor added here
}
Run Code Online (Sandbox Code Playgroud)
清理、构建和发送相同的 post 请求:
curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings
Run Code Online (Sandbox Code Playgroud)
并得到以下回复:
{"id":"AV2cq2OXcuirs1TrVgG6","username":"sunkuet02","message":"this is test"}
Run Code Online (Sandbox Code Playgroud)
场景是:当id字段的类型为时,Long它不会自动生成 id,但如果类型是,String则它会自动生成 id。
我的问题是:
spring-data-elasticsarch总是使用idtype 字段String?id类型Long而不添加任何注释。Spring Data Elasticsearch 在_id内部使用as Id,_id类型为 String。当您在文档字段上使用 @Id 并且您的数据类型是 String 时,spring data ES 将其内部映射_id到您的字段。但是当您使用数字(Long、Integer 等)数据类型时,spring data ES 无法将其自动生成的映射_id到您的 @Id 字段。如果您在 ES 上看到您的文档,您会看到您的文档 ID 字段为空,并且是_idget 自动生成的值。
您可以做的是,生成自己的id并将其设置在您的文档中,然后spring data ES将在其内部_id字段中设置该字段的String值。您将看到您的文档 ID 字段包含您设置的值。
| 归档时间: |
|
| 查看次数: |
6085 次 |
| 最近记录: |