Elasticsearch不根据createdBy字段查询?
Elasticsearch not query according createdBy field?
我使用 elasticsearch,SecurityApp 实体:
public class SecurityApp extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(max = 50)
@Column(name = "app_name", length = 50, nullable = false)
private String appName;
@Size(max = 20)
@Column(name = "app_key", length = 20, updatable = false)
private String appKey;
@Size(max = 20)
@Column(name = "app_secret", length = 20, updatable = false)
private String appSecret;
// hide getter and setter
}
AbstractAuditingEntity 实体:
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
// @JsonIgnore
private String createdBy;
@CreatedDate
@Column(name = "created_date", nullable = false)
@JsonIgnore
private ZonedDateTime createdDate = ZonedDateTime.now();
@LastModifiedBy
@Column(name = "last_modified_by", length = 50)
@JsonIgnore
private String lastModifiedBy;
@LastModifiedDate
@Column(name = "last_modified_date")
@JsonIgnore
private ZonedDateTime lastModifiedDate = ZonedDateTime.now();
// hide getter and setter
}
弹性搜索配置:
@Configuration
@AutoConfigureAfter(value = { JacksonConfiguration.class })
public class ElasticSearchConfiguration {
@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
return new ElasticsearchTemplate(client, new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build()));
}
public class CustomEntityMapper implements EntityMapper {
private ObjectMapper objectMapper;
public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@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);
}
}
}
我的问题:
当我使用查询字段 appName 时,它起作用了。当我使用 createdBy 查询字段时,它不起作用!
为什么?
我找到了解决方案。
因为@JsonIgnore 注解和私有权限。
当我像这样更新代码并重新保存数据时,它起作用了。
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
// @JsonIgnore
protected String createdBy;
也许 elasticsearch 使用 json?考虑不可用的反射扩展私有字段?我不知道。
好消息。有效。
我使用 elasticsearch,SecurityApp 实体:
public class SecurityApp extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(max = 50)
@Column(name = "app_name", length = 50, nullable = false)
private String appName;
@Size(max = 20)
@Column(name = "app_key", length = 20, updatable = false)
private String appKey;
@Size(max = 20)
@Column(name = "app_secret", length = 20, updatable = false)
private String appSecret;
// hide getter and setter
}
AbstractAuditingEntity 实体:
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
// @JsonIgnore
private String createdBy;
@CreatedDate
@Column(name = "created_date", nullable = false)
@JsonIgnore
private ZonedDateTime createdDate = ZonedDateTime.now();
@LastModifiedBy
@Column(name = "last_modified_by", length = 50)
@JsonIgnore
private String lastModifiedBy;
@LastModifiedDate
@Column(name = "last_modified_date")
@JsonIgnore
private ZonedDateTime lastModifiedDate = ZonedDateTime.now();
// hide getter and setter
}
弹性搜索配置:
@Configuration
@AutoConfigureAfter(value = { JacksonConfiguration.class })
public class ElasticSearchConfiguration {
@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
return new ElasticsearchTemplate(client, new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build()));
}
public class CustomEntityMapper implements EntityMapper {
private ObjectMapper objectMapper;
public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@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);
}
}
}
我的问题: 当我使用查询字段 appName 时,它起作用了。当我使用 createdBy 查询字段时,它不起作用! 为什么?
我找到了解决方案。
因为@JsonIgnore 注解和私有权限。
当我像这样更新代码并重新保存数据时,它起作用了。
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
// @JsonIgnore
protected String createdBy;
也许 elasticsearch 使用 json?考虑不可用的反射扩展私有字段?我不知道。
好消息。有效。