繁体   English   中英

Spring 数据 Elasticsearch 文档未被反序列化

[英]Spring Data Elasticsearch documents not being deserialized

Spring 数据 Elasticsearch 版本:3.2.6.RELEASE Elasticsearch 版本:7.6.2

我正在尝试使用以下方法反序列化MerchantCategory列表:

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

MerchsntCategory列表中的 String id 字段已正确设置,但其他字段仍为null

我已经确认文档字段使用 Kibana 保存在 Elasticsearch 中。

当 Spring Boot 应用程序启动时,将字段映射提交给 Elasticsearch:

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."]

这是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;
}

我使用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(); 并有同样的问题。

为什么唯一序列化的字段是 String id 字段而不是其他字段?

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

比你必须使用: @JsonProperty

@JsonProperty("UrlName")
private String urlName;

如果您使用 GSON 作为序列化程序,则必须使用@SerializedName

@SerializedName("UrlName")
private String urlName;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM