Spring Boot(2.2.X)-Spring Elastic Search(6.8.X)-不同的JSONProperty和字段名称 [英] Spring Boot(2.2.X) - Spring Elastic Search(6.8.X) - Different JSONProperty and Field Name

查看:130
本文介绍了Spring Boot(2.2.X)-Spring Elastic Search(6.8.X)-不同的JSONProperty和字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Spring Boot从 2.1.X 更新到 2.2.X ,并将弹性搜索从 6.3.X更新到 6.8.X /strong>.

Updated Spring boot to 2.2.X from 2.1.X and elastic search to 6.8.X from 6.3.X.

知道了映射异常,以解决映射异常,将文档变量重命名为myDocument.

elasticSearchRepo.SaveAll(objectTosave)上的值现在不保留在文档中. 文档中还存在其他属性,例如ID,类别.

Now on elasticSearchRepo.SaveAll(objectTosave) value is not persisted in document. Other properties like id, category are present in the document.

是否可以使用不同的fieldName和jsonProperty?

Is there any way to have different fieldName and jsonProperty?

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@Data
@EqualsAndHashCode
@ToString
@Document(indexName="my_document_index", type="information", createIndex=false)
@JsonIgnoreProperties(ignoreUnKnown = true)
@JsonInclude(Include.NON_NULL)
public class MyInstance
{
  @Field
  private String id;

  @Field
  private String category;

  @Field
  @JsonProperty("document")
  private MyObject mydocument;
  
  /** JSON Creator **/
  @JsonCreator
  public MyInstance(@JsonProperty("id") id, @JsonProperty("category") category,
         @JsonProperty("document") mydocument) 
  {
     this.id = id;
     this.category = category;
     this.mydocument = mydocument;
  }
}

推荐答案

无需使用@Field注释id属性,您应该将@Id放在此处.尽管不需要此属性,但由于属性的名称已足够,因此可以更清楚地了解其含义.

No need to annotate the id property with @Field, you should rather put @Id there. Although this is not needed, as the name of the property is enough, it makes it clearer what it is.

关于mydocument属性未保留的情况:它在Elasticsearch中,但名称为 mydocument . @JsonProperty("document")例如,当您通过REST端点获取时,@JsonProperty("document")在Jackson映射时定义了JSON中此属性的名称.重命名为mydocument可以避免将该属性解释为id属性的错误.

As for the mydocument property not being persisted: It is but in Elasticsearch with the name mydocument. The @JsonProperty("document") defines the name of this property in JSON when mapped by Jackson, when you get this in over a REST endpoint for example. Renaming to mydocument inhibits the error that the property is interpreted as id property.

但是我想您也希望在Elasticsearch中具有 document 的功能.您可以通过在@Field批注中设置属性来在Elasticsearch中定义属性的名称:

But I think you want to have as document in Elasticsearch as well. You can define the name of a property in Elasticsearch by setting it in the @Field annotation:

@Document(indexName="my_document_index", createIndex=false)
public class MyInstance
{
    @Id
    private String id;

    @Field
    private String category;

    @Field(name = "document")
    @JsonProperty("document")
    private MyObject mydocument;

}

这篇关于Spring Boot(2.2.X)-Spring Elastic Search(6.8.X)-不同的JSONProperty和字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆