Spring Boot 和 Elasticsearch @Field(type = FieldType.Date) 出现 LocalDateTime 转换错误

问题描述 投票:0回答:1

我在 Spring Boot 应用程序中使用 Elasticsearch。我正在尝试将“kibana_sample_data_ecommerce”示例索引(Kibana 中的示例数据库之一)转换为 Spring Boot Elasticsearch 实体。

我的实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(indexName = "kibana_sample_data_ecommerce")
public class Order {

    @Id
    @Field(type = FieldType.Keyword)
    private String id;

    @Field(type = FieldType.Text)
    private List<String> category;

    @Field(type = FieldType.Keyword)
    private String currency;

    @Field(type = FieldType.Keyword, name = "customer_id")
    private String customerId;

    @Field(type = FieldType.Text, name = "customer_first_name")
    private String customerFirstName;

    @Field(type = FieldType.Text, name = "customer_last_name")
    private String customerLastName;

    @Field(type = FieldType.Text, name = "customer_full_name")
    private String customerFullName;

    @Field(type = FieldType.Keyword, name = "customer_gender")
    private String customerGender;

    @Field(type = FieldType.Keyword, name = "customer_phone")
    private String customerPhone;

    @Field(type = FieldType.Keyword, name = "email")
    private String customerEmail;

    @Field(type = FieldType.Keyword, name = "day_of_week")
    private String dayOfWeek;

    @Field(type = FieldType.Integer, name = "day_of_week_i")
    private Integer dayOfWeekI;

    @Field(type = FieldType.Text, name = "manufacturer")
    private List<String> manufacturer;

    @Field(type = FieldType.Date, name = "order_date")
    private LocalDateTime orderDate;
}

索引属性:

{
  "_index": "kibana_sample_data_ecommerce",
  "_id": "hQizwooBeIhKUSFbYS8X",
  "_version": 1,
  "_score": 0,
  "_source": {
    "category": [
      "Men's Accessories"
    ],
    "currency": "EUR",
    "customer_first_name": "Robbie",
    "customer_full_name": "Robbie Shaw",
    "customer_gender": "MALE",
    "customer_id": 48,
    "customer_last_name": "Shaw",
    "customer_phone": "",
    "day_of_week": "Saturday",
    "day_of_week_i": 5,
    "email": "[email protected]",
    "manufacturer": [
      "Elitelligence"
    ],
    "order_date": "2023-09-23T18:11:12+00:00",
    "order_id": 739980,
    "products": [
      {
        "base_price": 7.99,
        "discount_percentage": 0,
        "quantity": 1,
        "manufacturer": "Elitelligence",
        "tax_amount": 0,
        "product_id": 20788,
        "category": "Men's Accessories",
        "sku": "ZO0597505975",
        "taxless_price": 7.99,
        "unit_discount_amount": 0,
        "min_price": 3.76,
        "_id": "sold_product_739980_20788",
        "discount_amount": 0,
        "created_on": "2016-12-17T18:11:12+00:00",
        "product_name": "Hat - dark grey",
        "price": 7.99,
        "taxful_price": 7.99,
        "base_unit_price": 7.99
      }
    ],
    "sku": [
      "ZO0597505975"
    ],
    "taxful_total_price": 7.99,
    "taxless_total_price": 7.99,
    "total_quantity": 1,
    "total_unique_products": 1,
    "type": "order",
    "user": "robbie",
    "geoip": {
      "country_iso_code": "AE",
      "location": {
        "lon": 55.3,
        "lat": 25.3
      },
      "region_name": "Dubai",
      "continent_name": "Asia",
      "city_name": "Dubai"
    },
    "event": {
      "dataset": "sample_ecommerce"
    }
  },
  "fields": {
    "products.manufacturer": [
      "Elitelligence"
    ],
    "products.base_unit_price": [
      7.9882812
    ],
    "products.discount_amount": [
      0
    ],
    "type": [
      "order"
    ],
    "products.discount_percentage": [
      0
    ],
    "products._id.keyword": [
      "sold_product_739980_20788"
    ],
    "day_of_week_i": [
      5
    ],
    "total_quantity": [
      1
    ],
    "taxless_total_price": [
      7.9882812
    ],
    "total_unique_products": [
      1
    ],
    "geoip.continent_name": [
      "Asia"
    ],
    "sku": [
      "ZO0597505975"
    ],
    "customer_full_name.keyword": [
      "Robbie Shaw"
    ],
    "category.keyword": [
      "Men's Accessories"
    ],
    "products.taxless_price": [
      7.9882812
    ],
    "products.quantity": [
      1
    ],
    "customer_first_name": [
      "Robbie"
    ],
    "products.price": [
      7.9882812
    ],
    "customer_phone": [
      ""
    ],
    "geoip.region_name": [
      "Dubai"
    ],
    "customer_full_name": [
      "Robbie Shaw"
    ],
    "geoip.country_iso_code": [
      "AE"
    ],
    "order_id": [
      "739980"
    ],
    "products._id": [
      "sold_product_739980_20788"
    ],
    "products.product_name.keyword": [
      "Hat - dark grey"
    ],
    "products.product_id": [
      20788
    ],
    "products.category": [
      "Men's Accessories"
    ],
    "products.manufacturer.keyword": [
      "Elitelligence"
    ],
    "manufacturer": [
      "Elitelligence"
    ],
    "products.unit_discount_amount": [
      0
    ],
    "customer_last_name": [
      "Shaw"
    ],
    "geoip.location": [
      {
        "coordinates": [
          55.3,
          25.3
        ],
        "type": "Point"
      }
    ],
    "products.product_name": [
      "Hat - dark grey"
    ],
    "products.tax_amount": [
      0
    ],
    "manufacturer.keyword": [
      "Elitelligence"
    ],
    "products.min_price": [
      3.7597656
    ],
    "currency": [
      "EUR"
    ],
    "products.taxful_price": [
      7.9882812
    ],
    "products.base_price": [
      7.9882812
    ],
    "email": [
      "[email protected]"
    ],
    "day_of_week": [
      "Saturday"
    ],
    "customer_last_name.keyword": [
      "Shaw"
    ],
    "products.sku": [
      "ZO0597505975"
    ],
    "products.category.keyword": [
      "Men's Accessories"
    ],
    "geoip.city_name": [
      "Dubai"
    ],
    "customer_first_name.keyword": [
      "Robbie"
    ],
    "order_date": [
      "2023-09-23T18:11:12.000Z"
    ],
    "products.created_on": [
      "2016-12-17T18:11:12.000Z"
    ],
    "category": [
      "Men's Accessories"
    ],
    "customer_id": [
      "48"
    ],
    "user": [
      "robbie"
    ],
    "customer_gender": [
      "MALE"
    ],
    "event.dataset": [
      "sample_ecommerce"
    ],
    "taxful_total_price": [
      7.9882812
    ]
  }
}

当我尝试获取所有数据时

org.springframework.data.elasticsearch.core.convert.ConversionException: Unable to convert value '2023-10-02T09:28:48+00:00' to java.time.LocalDateTime for property 'orderDate'

我收到错误。

版本:

  • JDK 20
  • Elasticsearch 8.9.2

我该如何解决这个问题?

java spring-boot elasticsearch spring-data-elasticsearch
1个回答
0
投票

来自

LocalDateTime
的 Javadoc:

此类不存储或表示时区。相反,它是对日期的描述,如生日,与挂钟上显示的当地时间相结合。如果没有偏移量或时区等附加信息,它无法表示时间线上的瞬间。

您尝试解析时间瞬间,该类不支持该瞬间,因为它不携带时区,因此无法确定本地时间是什么。考虑:

import java.time.LocalDateTime;

public class Test {
    
    public static void main(String[] args) {
        String d1 = "2023-09-23T18:11:12";
        LocalDateTime ldt1 = LocalDateTime.parse(d1);
        System.out.println(ldt1);

        String d2 = "2023-09-23T18:11:12+00:00";
        LocalDateTime ldt2 = LocalDateTime.parse(d2);
        System.out.println(ldt2);
    }

}

产品:

2023-09-23T18:11:12
Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-09-23T18:11:12+00:00' could not be parsed, unparsed text found at index 19
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2055)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:479)
    at Playground/net.jhmg.play.Test.main(Test.java:13)```
© www.soinside.com 2019 - 2024. All rights reserved.