【问题标题】:Elasticsearch not query according createdBy field?Elasticsearch不根据createdBy字段查询?
【发布时间】:2016-09-29 05:18:53
【问题描述】:

我使用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 时,它不起作用! 这是为什么呢?

【问题讨论】:

    标签: java elasticsearch jhipster spring-data-elasticsearch


    【解决方案1】:

    我找到了解决办法。

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

    当我像这样更新代码并重新保存数据时,它会起作用。

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

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

    好消息。它有效。

    【讨论】:

    • 是的,在 JHipster 应用程序中,Elasticsearch 将您的对象存储为 JSON 并使用 Jackson 进行序列化/反序列化,因此任何带有 @JsonIgnore 的字段都不会被索引且不可搜索。
    • @geraldhumphries 为什么字段的权力是私有的,它不起作用?
    • 根据我的经验,它只需删除 JsonIgnore 即可。你有公共的 getter 和 setter 吗?
    • @geraldhumphries 是的,我有。非常感谢,我重试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2022-12-04
    • 1970-01-01
    • 2021-11-15
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多