使用 Spring Elasticsearch 数据存储库构建查询
Build query using Spring Elasticsearch data repository
我正在使用elasticsearch 6,和Spring数据jpa来查询。我写了这段代码:
- 持久对象:
@Document(indexName="vehicle_event", type="vehicle_event")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VehicleEventEL {
@Id
private String id;
private Long id_vehicle;
private Long id_road;
private Float latitude;
private Float longitude;
private String name_road;
private Date timestamp;
}
- 存储库:
@Component
public interface VehicleEventELRepository extends ElasticsearchRepository<VehicleEventEL, String> {
Page<VehicleEventEL> findByTimestampBetween(String startTime, String endTime, Pageable page);
Page<VehicleEventEL> findByTimestampBetweenAndId_vehicleEquals(String startTime, String endTime, Long id_vehicle, Pageable page);
}
方法 findByTimestampBetween
有效。
方法 findByTimestampBetweenAndId_vehicleEquals
不起作用,并出现以下错误:
Error creating bean with name 'vehicleEventELRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property vehicle found for type String! Traversed path: VehicleEventEL.id.
我找不到解决方案。
下划线是一个问题,解决方案是遵循 java 命名约定。
Spring documentation 说:
To resolve this ambiguity you can use _ inside your method name to
manually define traversal points. So our method name would be as
follows:
List<Person> findByAddress_ZipCode(ZipCode zipCode);
Because we treat the underscore character as a reserved character, we strongly advise
following standard Java naming conventions (that is, not using
underscores in property names but using camel case instead).
如果您不能更改名称,一种选择是使用@Query 注释。
我正在使用elasticsearch 6,和Spring数据jpa来查询。我写了这段代码:
- 持久对象:
@Document(indexName="vehicle_event", type="vehicle_event")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VehicleEventEL {
@Id
private String id;
private Long id_vehicle;
private Long id_road;
private Float latitude;
private Float longitude;
private String name_road;
private Date timestamp;
}
- 存储库:
@Component
public interface VehicleEventELRepository extends ElasticsearchRepository<VehicleEventEL, String> {
Page<VehicleEventEL> findByTimestampBetween(String startTime, String endTime, Pageable page);
Page<VehicleEventEL> findByTimestampBetweenAndId_vehicleEquals(String startTime, String endTime, Long id_vehicle, Pageable page);
}
方法 findByTimestampBetween
有效。
方法 findByTimestampBetweenAndId_vehicleEquals
不起作用,并出现以下错误:
Error creating bean with name 'vehicleEventELRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property vehicle found for type String! Traversed path: VehicleEventEL.id.
我找不到解决方案。
下划线是一个问题,解决方案是遵循 java 命名约定。 Spring documentation 说:
To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would be as follows:
List<Person> findByAddress_ZipCode(ZipCode zipCode);
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
如果您不能更改名称,一种选择是使用@Query 注释。