java - Elasticsearch不根据createdBy字段查询?

标签 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
}

Elasticsearch 配置:

@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 时,它​​可以工作。当我使用 createBy 查询字段时,它不起作用! 这是为什么?

最佳答案

我找到了解决方案。

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

当我像这样更新代码并重新保存数据时,它就可以工作。

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

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

好消息。它有效。

关于java - Elasticsearch不根据createdBy字段查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37541554/

相关文章:

java - 我可以通过扫描 spring 配置文件中的 beans 来动态创建一个列表吗?

Elasticsearch如何在映射中指定索引为false的关键字数组?

python - 有没有办法以原子方式删除和重新创建 Elasticsearch 别名?

hadoop - 文档中的Elastic Search更新字段

jhipster - Spring Roo和JHipster之间的区别

java - Spingfox 在为 swagger 生成 JSON 模型时无法识别自定义序列化程序

java - 无法在基于 JHipster 的 Web 应用程序中运行从 Gradle BootRepackage 生成的 war

java - DataOutputStream 中的 System.out 未在控制台上打印

java - java Mock 文件的建议(模拟 java.io.File)

java - 更改后 Firebase 数据库无法正确同步