Spring ElasticSearch将字段另存为JSON

时间:2018-11-06 10:04:38

标签: java spring elasticsearch spring-data-elasticsearch

我有一个Spring Data ElasticSearch App,它可以保存这样的文档

@Data
@Builder
@Document(indexName = "listings_data_pipeline", type = ElasticConfig.ELASTICSEARCH_TYPE)
final class ListingsData {
    private final String listingId;
    private final String actionType;
    private final BackOfficeData backOfficeData;
    private final SearchAndMatchData searchAndMatchData;
}

我只想更新一个字段,例如backOfficeData,所以我有这样的服务

@Slf4j
@RequiredArgsConstructor
class ElasticRepo {

    private final ElasticsearchTemplate elasticsearchTemplate;

    void updateBackOfficeField(final String listingId, final BackOfficeData data) {
        final IndexRequest indexRequest = new IndexRequest();
        indexRequest.source("backOfficeData", data);
        final UpdateQuery updateQuery = new UpdateQueryBuilder().withId(listingId).withClass(ListingsData.class).withIndexRequest(indexRequest).build();
        synchronized (this) {
            elasticsearchTemplate.update(updateQuery);
        }
    }

但是不幸的是,当我这样做时,backOfficeData字段不是作为toString方法的String而是作为String存储在elasticSearch中。所以当我查询弹性时,我得到这个:

                     ...
"_source" : {
          "listingId" : "3453325",
          "actionType" : "OfferRenew",
          "backOfficeData" : "BackOfficeData(listingId=3453325, validity=60, experienceIds=[90002, 90001])",
                      ...

但是我想要这样:

                       ...
"_source" : {
          "listingId" : "3453325",
          "actionType" : "OfferRenew",
          "backOfficeData" : {"listingId": "3453325", "validity": "60", "experienceIds" : [90002, 90001]}
                      ...

如何实现?

0 个答案:

没有答案