温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Spring Data Elasticsearch Repository query define date input parameter format
elasticsearch spring spring-boot spring-data spring-data-elasticsearch

其他 - Spring Data Elasticsearch Repository查询定义日期输入参数格式

发布于 2020-04-05 00:41:33

我正在使用elasticsearch 6.5.3和Spring Boot 2.1.6和spring-data-elasticsearch 3.2.0.M1。

我已经将Elasticsearch配置定义为:

@Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client(), new CustomEntityMapper());
    }

    public static class CustomEntityMapper implements EntityMapper {

        private final ObjectMapper objectMapper;

        public CustomEntityMapper() {
            //we use this so that Elasticsearch understands LocalDate and LocalDateTime objects
            objectMapper = new ObjectMapper()
                              .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                              .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
                              .disable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
                              .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                              //MUST be registered BEFORE calling findAndRegisterModules
                              .registerModule(new JavaTimeModule())
                              .registerModule(new Jdk8Module());
            //only autodetect fields and ignore getters and setters for nonexistent fields when serializing/deserializing
            objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
                            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
            //load the other available modules as well
            objectMapper.findAndRegisterModules();
        }

        @Override
        public String mapToString(Object object) throws IOException {
            return objectMapper.writeValueAsString(object);
        }

        @Override
        public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
            return objectMapper.readValue(source, clazz);
        }
    }

我有一个方法定义为的存储库:

List<AccountDateRollSchedule> findAllByNextRollDateTimeLessThanEqual(final LocalDateTime dateTime);

POJO AccountDateRollSchedule将该字段定义为:

    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute)
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm")
    private LocalDateTime nextRollDateTime;

我看到我的索引正确地创建了声明和预期的字段:

"nextRollDateTime": {
          "type": "date",
          "format": "date_hour_minute"
        }

同样查询索引返回按预期格式的字段:

"nextRollDateTime" : "2019-06-27T13:34"

我的存储库查询将转换为:

{"query": 
  {"bool" : 
    {"must" : 
      {"range" : 
        {"nextRollDateTime" : 
          {"from" : null, 
           "to" : "?0", 
           "include_lower" : true,
           "include_upper" : true
          }
        }
      }
    }
  }
}

但是将任何LocalDateTime输入传递给该方法均不遵守为该字段定义的格式,而是始终使用FULL格式。调用:

findAllByNextRollDateTimeLessThanEqual(LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.MINUTES));

给我以下异常(存储库中方法参数上的任何@DateTimeFormat或@JsonFormat批注都会被忽略):

Unrecognized chars at the end of [2019-07-22T09:07:00.000]: [:00.000]

如果我改为将存储库方法更改为接受String并传递完全按预期格式设置的String作为输入,则它没有问题。

是否可以以某种方式定义用于输入到存储库方法的输入中的date参数的格式,或者让Spring使用在字段本身上配置的格式?

我不想将这种方法包装为这样的简单转换(我确实做到了,并且可以正常工作),并且我也想避免在日期字段中使用长类型

谢谢和欢呼

供参考,我还在Spring JIRA上发表了文章

查看更多

提问者
grog
被浏览
123
P.J.Meisch 2020-01-31 22:59

这些问题是我们不使用和公开Spring Data Elasticsearch中的JacksonMapper的原因之一。从版本4.0开始,您需要在媒体资源上添加一个注释:

@Field(type = FieldType.Date, format = DateFormat.date_hour_minute)
private LocalDateTime nextRollDateTime;

然后,在为实体建立索引和检索以及处理存储库方法和查询时,将在编写索引映射时使用它。

但是对于3.2.x版本,您将必须使用一种解决方法,例如您提到的包装。