Spring Data Elasticsearch文档未被反序列化

huangapple go评论90阅读模式
英文:

Spring Data Elasticsearch documents not being deserialized

问题

Spring Data Elasticsearch 版本:3.2.6.RELEASE
Elasticsearch 版本:7.6.2

我正在尝试使用以下代码对 MerchantCategory 列表进行反序列化:

SearchQuery getAllQuery = new NativeSearchQueryBuilder()
        .withQuery(matchAllQuery())
        .build();
return elasticsearchTemplate.queryForList(getAllQuery, MerchantCategory.class);

MerchsntCategory 列表上的字符串 id 字段被正确设置,但其他字段仍然保持为 null

我已经确认文档字段在 Elasticsearch 中通过 Kibana 进行了持久化。

当启动 Spring Boot 应用程序时,字段映射会被提交到 Elasticsearch:

request [PUT http://127.0.0.1:9200/merchantcategory/_mapping/merchantcategory?master_timeout=30s&include_type_name=true&timeout=30s] 返回了 1 个警告: [299 Elasticsearch-7.6.2-ef48eb35cf30adf4db14086e8aabd07ef6fb113f "[types removal] Using include_type_name in put mapping requests is deprecated. The parameter will be removed in the next major version."]

以下是 MerchantCategory 类的定义:

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Document(indexName = "merchantcategory", type = "merchantcategory")
public class MerchantCategory implements Serializable {

    @Id
    @JsonIgnore
    private String id;

    @SerializedName("ParentId")
    private Long parentId;

    @SerializedName("Name")
    private String name;

    @SerializedName("Description")
    private String description;

    @SerializedName("UrlName")
    private String urlName;

    @SerializedName("Id")
    private Long categoryId;

    @SerializedName("MerchantsInCategory")
    private List<MerchantCategoryRelationship> merchants;
}

我使用 gson 进行序列化:

val bulkRequest = new BulkRequest();
entities.subList(startIndex, endIndex).forEach(e ->{
    String source = gson.toJson(e);
    val indexRequest = new IndexRequest(index).source(source, XContentType.JSON).type(type);
        bulkRequest.add(indexRequest);
});

highLevelClient.bulkAsync(bulkRequest, RequestOptions.DEFAULT, getListener());

我还尝试过使用 merchantCategoryElasticsearchRepository.findAll(); 进行相同的操作,但问题仍然存在。

为什么只有字符串 id 字段被序列化,而其他字段却没有被序列化呢?

英文:

Spring Data Elasticsearch version: 3.2.6.RELEASE
Elasticsearch version: 7.6.2

I am attempting to deserialize a list of MerchantCategory using:

SearchQuery getAllQuery = new NativeSearchQueryBuilder()
        .withQuery(matchAllQuery())
        .build();
return elasticsearchTemplate.queryForList(getAllQuery, MerchantCategory.class);

The String id field is correctly set on the list of MerchsntCategory but the other fields remain null.

I have confirmed that the documents fields are persisted in Elasticsearch using Kibana.

The field mappings are submitted to Elasticsearch when the Spring Boot application is started:

request [PUT http://127.0.0.1:9200/merchantcategory/_mapping/merchantcategory?master_timeout=30s&include_type_name=true&timeout=30s] returned 1 warnings: [299 Elasticsearch-7.6.2-ef48eb35cf30adf4db14086e8aabd07ef6fb113f "[types removal] Using include_type_name in put mapping requests is deprecated. The parameter will be removed in the next major version."]

Here is the MerchantCategory class:

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Document(indexName = "merchantcategory", type = "merchantcategory")
public class MerchantCategory implements Serializable {

    @Id
    @JsonIgnore
    private String id;

    @SerializedName("ParentId")
    private Long parentId;

    @SerializedName("Name")
    private String name;

    @SerializedName("Description")
    private String description;

    @SerializedName("UrlName")
    private String urlName;

    @SerializedName("Id")
    private Long categoryId;

    @SerializedName("MerchantsInCategory")
    private List<MerchantCategoryRelationship> merchants;
}

I use gson to serialize:

val bulkRequest = new BulkRequest();
entities.subList(startIndex, endIndex).forEach(e ->{
    String source = gson.toJson(e);
    val indexRequest = new IndexRequest(index).source(source, XContentType.JSON).type(type);
        bulkRequest.add(indexRequest);
});

highLevelClient.bulkAsync(bulkRequest, RequestOptions.DEFAULT, getListener());

I have also tried the same thing with merchantCategoryElasticsearchRepository.findAll(); and have the same issue.

Why is it that the only field that is serialized is the String id field and not the others?

答案1

得分: 1

你使用Jackson作为序列化器吗?

那么你需要使用:@JsonProperty

@JsonProperty("UrlName")
private String urlName;

如果你正在使用GSON作为序列化器,你需要使用@SerializedName

@SerializedName("UrlName")
private String urlName;
英文:

Do you use Jackson as serializer?

Than you have to use: @JsonProperty

@JsonProperty("UrlName")
private String urlName;

If you are using GSON as serializer you have to use @SerializedName.

@SerializedName("UrlName")
private String urlName;

huangapple
  • 本文由 发表于 2020年5月5日 05:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定