Elasticsearch不根据createdBy字段查询?

时间:2016-05-31 09:14:47

标签: java elasticsearch jhipster spring-data-elasticsearch

我使用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
}

ElasticSearchConfiguration:

@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时,它不起作用! 那是为什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

因为@JsonIgnore注释和私有权限。

当我更新这样的代码和重新保存数据时,它可以正常工作。

@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
// @JsonIgnore
protected String createdBy;

也许elasticsearch使用json?反思不可用扩展私有字段?我不知道。

好消息。它有效。