Skip to content

After upgrade to 4.x can't read property id from _source named (different value from _id) #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matus753 opened this issue Feb 3, 2021 · 1 comment · Fixed by #1681
Closed
Assignees
Labels
type: enhancement A general enhancement

Comments

@matus753
Copy link

matus753 commented Feb 3, 2021

We have this record stored in elastic search:
{ "_index": "INDEX_NAME", "_type": "_doc", "_id": "ID_VALUE", "_version": 44, "_score": 0, "_source": { "someAttribute": "someValue", "id": "someValue" } }

until now using spring data elasticsearch 3.x we were using class as this:
@Data @Document(indexName = INDEX_PREFIX + BLACKLIST) public class ClassName { @Id @JsonIgnore private String id; @JsonProperty("id") private String productId;

after upgrading to spring data elasticsearch 4.x which started using MappingElasticsearchConverter jackson anotations are useless and variable productId is null after reading from elastich search

when i try another trick like this:
@Data @Document(indexName = INDEX_PREFIX + BLACKLIST) public class ClassName { @Id private String elasticId; @Field("id") private String productId;

i get this error:
Exception while fetching data (INDEX_PATH) : Attempt to add id property private java.lang.String package.path.ClassName.elasticId but already have property private java.lang.String package.path.ClassName.Product.productId registered as id. Check your mapping configuration!

that's probably due to this part of code in org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty
when any field named as "id" or "document" is set as id property, even there is @id anotation on some other field
this.isId = super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());

is there any way how to read id property from elastic search by using spring data elastic serach 4.x ??

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 3, 2021
@sothawo sothawo added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 3, 2021
@sothawo sothawo self-assigned this Feb 3, 2021
@sothawo sothawo added type: enhancement A general enhancement and removed type: bug A general bug labels Feb 3, 2021
@sothawo
Copy link
Collaborator

sothawo commented Feb 3, 2021

This comes as you noticed from the fact that an id-property cannot only be defined by using the @Id annotation, but also by just using one of the "supported" names which are id and document.

In your case this produces a clash as there are now 2 id properties. I'll fix this so that Spring Data Elasticsearch behaves like other spring data modules so that a property that has one of the supported names - either by the property itself or by a @Field annotation - only is considered to be an id property if it is annotated with @Id or does not have a custom name set with @Field. So your second approach will work then. I will implement this within the next days.

When this fix is in place you should use

@Document(indexName = "custom-id")
public class CustomId {
    @Id
    @ReadOnlyProperty
    private String elasticId;
    @Field("id")
    private String documentId;

     // other properties, getter and setter
}

By using @ReadOnlyProperty on the id property, it will not be written to the _source of the document but only to the Elasticsearch _id field.

sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Feb 5, 2021
sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Feb 5, 2021
sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Feb 5, 2021
sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Feb 5, 2021
sothawo added a commit that referenced this issue Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants