简体   繁体   English

不是为Spring Data Elasticsearch中的Long类型而是字符串类型字段自动生成id

[英]Not Auto-generating id for Long type but for String type field in Spring Data Elasticsearch

I am using spring boot application of version 1.5.6.RELEASE and spring-boot-starter-data-elasticsearch of the same version. 我正在使用1.5.6.RELEASE版本的spring boot应用程序和同一版本的spring-boot-starter-data-elasticsearch 1.5.6.RELEASE

I have a model Greeting as like : 我有一个Greeting模型,例如:

@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
    @Id
    private Long id;
    private String username;
    // Getter, Setter and constructor added here
}

My controller and service classes are same for the below cases without the type of id . 对于以下情况,没有id的类型,我的控制器和服务类相同。

When I have sent the below post request: 当我发送以下帖子请求时:

curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings

It replies: 它回答:

{"id":null,"username":"sunkuet02","message":"this is test"}

Then I have changed the Greeting class changing the type of id to String. 然后,我更改了Greeting类,将id的类型更改为String。

@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{    
    @Id
    private String id;    
    private String username;
    // Getter, Setter and constructor added here
}

Clean, build and send the same post request: 清理,构建并发送相同的帖子请求:

curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings

And got the below response : 并得到以下回应:

{"id":"AV2cq2OXcuirs1TrVgG6","username":"sunkuet02","message":"this is test"}

The scenario is : When the type of id field is Long then it doesn't generate id automatically but if the type is String then it generates id automatically. 方案是 :当id字段的类型为Long它不会自动生成id,但是如果类型为String则它会自动生成id。

My questions are: 我的问题是:

  • What is the actual reason ? 实际原因是什么?
  • Does spring-data-elasticsarch always uses id field of type String ? spring-data-elasticsarch是否始终使用String类型的id字段?
  • Is there any way in configuration to use id of type Long without adding any annotations. 在配置中是否有任何方法可以使用Long类型的id而不添加任何注释。

Spring Data Elasticsearch uses _id internally as Id and _id type is String. Spring Data Elasticsearch在内部使用_id作为Id并且_id类型为String。 When you used @Id on your document field, and your data type is String, spring data ES mapped its internal _id to your fields. 在文档字段上使用@Id且数据类型为String时,spring数据ES会将其内部_id映射到您的字段。 But when you use numeric (Long, Integer, etc.) data type, spring data ES can't map its auto-generated _id to your @Id field. 但是,当您使用数字(Long,Integer等)数据类型时,spring数据ES无法将其自动生成的_id映射到您的@Id字段。 If you see your document on ES, you will see that your document id field is null, and _id get's auto-generated value. 如果在ES上看到文档,则将看到您的文档ID字段为空,并且_id get的自动生成值。

What you can do is, generate your own id and set it in your document, then spring data ES will set the String value of that field in its internal _id field. 您可以做的是,生成自己的id并将其设置在文档中,然后spring数据ES将在其内部_id字段中设置该字段的String值。 And you will see your document id field contains the value you set. 您将看到您的文档ID字段包含您设置的值。

I'll explain this part: 我将解释这一部分:

What is the actual reason ? 实际原因是什么? Does spring-data-elasticsarch always uses id field of type String ? spring-data-elasticsarch是否始终使用String类型的id字段?

Ids generated by default by Elasticsearch are 20 character long, URL-safe, Base64-encoded GUID strings. 默认情况下,Elasticsearch生成的ID为20个字符,URL安全,Base64编码的GUID字符串。

The reason is, of course, performance - using modified Flake IDs allows to increase the number of lookups per second while indexing data. 原因当然是性能-使用修改后的Flake ID可以在索引数据时增加每秒的查找次数。 Generally speaking - the segment-base nature of Lucene promotes IDs that have some pattern in the way they are assigned to segments or have some predictability. 一般来说,Lucene基于段的本质促进了ID的分配,这些ID在分配到段的方式上具有某种模式或具有一定的可预测性。

So if your application does not really care how the id should look like, it's better to stick to the default ids provided by Elsticsearch. 因此,如果您的应用程序并不真正在乎ID的外观,最好坚持使用Elsticsearch提供的默认ID。

More information in this article , section Use auto id or pick a good id . 本文“ 使用自动ID或选择一个好的ID”部分中的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动生成一个唯一的varchar字段 - mySQL,Java,Hibernate - Auto-generating a unique varchar field - mySQL, Java, Hibernate 自动生成默认和字段构造函数的 eclipse 快捷方式是什么? - What is the eclipse shortcut for auto-generating a default and field constructor? 生成具有自动增量的String类型的长数字ID - Generate long numeric id of type String with auto increment 如何在 Spring Data Elasticsearch 中为时间访问器的 HashMap 提供字段类型 - How to provide Field type for HashMap of temporal accessors in Spring Data Elasticsearch Spring Boot 不会增加 long 类型的自动生成的 id - Spring Boot doesn't increment auto-generated id with type long 自动生成EJB3实体Bean - Auto-Generating EJB3 Entity Beans Javadoc 不会在 IntelliJ IDEA 中自动生成 - Javadoc not auto-generating in IntelliJ IDEA Eclipse Android自动生成动作处理程序 - Eclipse Android auto-generating action handlers 在单元测试中自动生成数据存储区索引 - Auto-generating datastore indexes in unit tests Spring 数据 Elasticsearch - 找不到能够从类型 [java.lang.Long] 转换为类型 [java.time.Instant] 的转换器 - Spring Data Elasticsearch - No converter found capable of converting from type [java.lang.Long] to type [java.time.Instant]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM