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

eimct9ow  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(225)

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

我的实体类:

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Builder
  5. @Document(indexName = "kibana_sample_data_ecommerce")
  6. public class Order {
  7. @Id
  8. @Field(type = FieldType.Keyword)
  9. private String id;
  10. @Field(type = FieldType.Text)
  11. private List<String> category;
  12. @Field(type = FieldType.Keyword)
  13. private String currency;
  14. @Field(type = FieldType.Keyword, name = "customer_id")
  15. private String customerId;
  16. @Field(type = FieldType.Text, name = "customer_first_name")
  17. private String customerFirstName;
  18. @Field(type = FieldType.Text, name = "customer_last_name")
  19. private String customerLastName;
  20. @Field(type = FieldType.Text, name = "customer_full_name")
  21. private String customerFullName;
  22. @Field(type = FieldType.Keyword, name = "customer_gender")
  23. private String customerGender;
  24. @Field(type = FieldType.Keyword, name = "customer_phone")
  25. private String customerPhone;
  26. @Field(type = FieldType.Keyword, name = "email")
  27. private String customerEmail;
  28. @Field(type = FieldType.Keyword, name = "day_of_week")
  29. private String dayOfWeek;
  30. @Field(type = FieldType.Integer, name = "day_of_week_i")
  31. private Integer dayOfWeekI;
  32. @Field(type = FieldType.Text, name = "manufacturer")
  33. private List<String> manufacturer;
  34. @Field(type = FieldType.Date, name = "order_date")
  35. private LocalDateTime orderDate;
  36. }

索引属性:

  1. {
  2. "_index": "kibana_sample_data_ecommerce",
  3. "_id": "hQizwooBeIhKUSFbYS8X",
  4. "_version": 1,
  5. "_score": 0,
  6. "_source": {
  7. "category": [
  8. "Men's Accessories"
  9. ],
  10. "currency": "EUR",
  11. "customer_first_name": "Robbie",
  12. "customer_full_name": "Robbie Shaw",
  13. "customer_gender": "MALE",
  14. "customer_id": 48,
  15. "customer_last_name": "Shaw",
  16. "customer_phone": "",
  17. "day_of_week": "Saturday",
  18. "day_of_week_i": 5,
  19. "email": "[email protected]",
  20. "manufacturer": [
  21. "Elitelligence"
  22. ],
  23. "order_date": "2023-09-23T18:11:12+00:00",
  24. "order_id": 739980,
  25. "products": [
  26. {
  27. "base_price": 7.99,
  28. "discount_percentage": 0,
  29. "quantity": 1,
  30. "manufacturer": "Elitelligence",
  31. "tax_amount": 0,
  32. "product_id": 20788,
  33. "category": "Men's Accessories",
  34. "sku": "ZO0597505975",
  35. "taxless_price": 7.99,
  36. "unit_discount_amount": 0,
  37. "min_price": 3.76,
  38. "_id": "sold_product_739980_20788",
  39. "discount_amount": 0,
  40. "created_on": "2016-12-17T18:11:12+00:00",
  41. "product_name": "Hat - dark grey",
  42. "price": 7.99,
  43. "taxful_price": 7.99,
  44. "base_unit_price": 7.99
  45. }
  46. ],
  47. "sku": [
  48. "ZO0597505975"
  49. ],
  50. "taxful_total_price": 7.99,
  51. "taxless_total_price": 7.99,
  52. "total_quantity": 1,
  53. "total_unique_products": 1,
  54. "type": "order",
  55. "user": "robbie",
  56. "geoip": {
  57. "country_iso_code": "AE",
  58. "location": {
  59. "lon": 55.3,
  60. "lat": 25.3
  61. },
  62. "region_name": "Dubai",
  63. "continent_name": "Asia",
  64. "city_name": "Dubai"
  65. },
  66. "event": {
  67. "dataset": "sample_ecommerce"
  68. }
  69. },
  70. "fields": {
  71. "products.manufacturer": [
  72. "Elitelligence"
  73. ],
  74. "products.base_unit_price": [
  75. 7.9882812
  76. ],
  77. "products.discount_amount": [
  78. 0
  79. ],
  80. "type": [
  81. "order"
  82. ],
  83. "products.discount_percentage": [
  84. 0
  85. ],
  86. "products._id.keyword": [
  87. "sold_product_739980_20788"
  88. ],
  89. "day_of_week_i": [
  90. 5
  91. ],
  92. "total_quantity": [
  93. 1
  94. ],
  95. "taxless_total_price": [
  96. 7.9882812
  97. ],
  98. "total_unique_products": [
  99. 1
  100. ],
  101. "geoip.continent_name": [
  102. "Asia"
  103. ],
  104. "sku": [
  105. "ZO0597505975"
  106. ],
  107. "customer_full_name.keyword": [
  108. "Robbie Shaw"
  109. ],
  110. "category.keyword": [
  111. "Men's Accessories"
  112. ],
  113. "products.taxless_price": [
  114. 7.9882812
  115. ],
  116. "products.quantity": [
  117. 1
  118. ],
  119. "customer_first_name": [
  120. "Robbie"
  121. ],
  122. "products.price": [
  123. 7.9882812
  124. ],
  125. "customer_phone": [
  126. ""
  127. ],
  128. "geoip.region_name": [
  129. "Dubai"
  130. ],
  131. "customer_full_name": [
  132. "Robbie Shaw"
  133. ],
  134. "geoip.country_iso_code": [
  135. "AE"
  136. ],
  137. "order_id": [
  138. "739980"
  139. ],
  140. "products._id": [
  141. "sold_product_739980_20788"
  142. ],
  143. "products.product_name.keyword": [
  144. "Hat - dark grey"
  145. ],
  146. "products.product_id": [
  147. 20788
  148. ],
  149. "products.category": [
  150. "Men's Accessories"
  151. ],
  152. "products.manufacturer.keyword": [
  153. "Elitelligence"
  154. ],
  155. "manufacturer": [
  156. "Elitelligence"
  157. ],
  158. "products.unit_discount_amount": [
  159. 0
  160. ],
  161. "customer_last_name": [
  162. "Shaw"
  163. ],
  164. "geoip.location": [
  165. {
  166. "coordinates": [
  167. 55.3,
  168. 25.3
  169. ],
  170. "type": "Point"
  171. }
  172. ],
  173. "products.product_name": [
  174. "Hat - dark grey"
  175. ],
  176. "products.tax_amount": [
  177. 0
  178. ],
  179. "manufacturer.keyword": [
  180. "Elitelligence"
  181. ],
  182. "products.min_price": [
  183. 3.7597656
  184. ],
  185. "currency": [
  186. "EUR"
  187. ],
  188. "products.taxful_price": [
  189. 7.9882812
  190. ],
  191. "products.base_price": [
  192. 7.9882812
  193. ],
  194. "email": [
  195. "[email protected]"
  196. ],
  197. "day_of_week": [
  198. "Saturday"
  199. ],
  200. "customer_last_name.keyword": [
  201. "Shaw"
  202. ],
  203. "products.sku": [
  204. "ZO0597505975"
  205. ],
  206. "products.category.keyword": [
  207. "Men's Accessories"
  208. ],
  209. "geoip.city_name": [
  210. "Dubai"
  211. ],
  212. "customer_first_name.keyword": [
  213. "Robbie"
  214. ],
  215. "order_date": [
  216. "2023-09-23T18:11:12.000Z"
  217. ],
  218. "products.created_on": [
  219. "2016-12-17T18:11:12.000Z"
  220. ],
  221. "category": [
  222. "Men's Accessories"
  223. ],
  224. "customer_id": [
  225. "48"
  226. ],
  227. "user": [
  228. "robbie"
  229. ],
  230. "customer_gender": [
  231. "MALE"
  232. ],
  233. "event.dataset": [
  234. "sample_ecommerce"
  235. ],
  236. "taxful_total_price": [
  237. 7.9882812
  238. ]
  239. }
  240. }

当我试图获取所有数据时

  1. 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

我该如何解决这个问题?

hts6caw3

hts6caw31#

LocalDateTime的Javadoc:
此类不存储或表示时区。相反,它是对日期的描述,如用于生日,结合当地时间,如在挂钟上看到的。它不能表示时间线上的一个时刻,而没有附加信息,如偏移量或时区。
您试图解析一个即时时间,但该类不支持,因为它不携带时区,因此无法确定本地时间。考虑:

  1. import java.time.LocalDateTime;
  2. public class Test {
  3. public static void main(String[] args) {
  4. String d1 = "2023-09-23T18:11:12";
  5. LocalDateTime ldt1 = LocalDateTime.parse(d1);
  6. System.out.println(ldt1);
  7. String d2 = "2023-09-23T18:11:12+00:00";
  8. LocalDateTime ldt2 = LocalDateTime.parse(d2);
  9. System.out.println(ldt2);
  10. }
  11. }

生产:

  1. 2023-09-23T18:11:12
  2. 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
  3. at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2055)
  4. at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
  5. at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
  6. at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:479)
  7. at Playground/net.jhmg.play.Test.main(Test.java:13)

您需要使用不同的数据类型(即,Instant),或提供一个自定义转换器,将应用正确的时区。通过使用LocalDateTime,您正在丢弃信息。

展开查看全部

相关问题