V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hujianxin
V2EX  ›  程序员

Spring-data-elasticsearch 有盆友用过吗? index 是动态生成的,我如何写程序去适配呢?

  •  
  •   hujianxin · 2017-04-17 16:40:19 +08:00 · 1776 次点击
    这是一个创建于 2582 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 spring-data-elasticsearch 里面,实体类需要使用 @Document 修饰,并且指定 index 名字。但是我要读取的 es 数据库里面, index 是随着时间添加的。

    例如 index 会是这样的 index-201602 、 index-201603 、 index-201604 这样的。这些 index-*都有一个 alias ,叫做 index 。

    我现在想要读取这些 index 里面的内容,但是没法动态的新建类,并制定 index 名字。

    我尝试通过 index 这个 alias 作为 index 名字读取,发现只能读出 index-201602 里面的数据。

    各位大大有没有遇到过这个问题的,非常渴望得到大家的帮助,谢谢!

    2 条回复    2017-04-18 23:43:26 +08:00
    yzmm
        1
    yzmm  
       2017-04-18 12:44:19 +08:00
    spring-data-elasticsearch 这个库刚开始出就开始用了,后来发现很多坑然后而且没法跟上 elasticsearch 的更新速度。后来我撸了个库( https://github.com/javasec/javaweb-elasticsearch )解决高版本的 elasticsearch 无法使用 spring-data-elasticsearch 的问题。
    你先在 elasticsearch 创建个 aliases 如 web-aliases,参考 https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

    在你的实体类上加这样的映射
    @Document(indexName = "web-aliases", type = "documents", indexStoreType = "memory", shards = 10, replicas = 0, refreshInterval = "-1")

    还有,如果你的成员变量和 es 的字段不一致可用 fasterxml 的注解指定。

    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonProperty;

    @Field(type = FieldType.Nested)
    @JsonProperty("header_info")
    private Map<String, Object> headerInfo;

    @Field(type = FieldType.Nested)
    private Map<String, Object> location;

    private Set<AppFeature> appFeatureList;

    @Field(type = Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date ctime;

    还有 id 的问题,如果你的 es 字段里 id 和_id 值不一样也没法映射。默认 id 和 documentId 的值都会被 Spring data 给强制设置为_id 。
    hujianxin
        2
    hujianxin  
    OP
       2017-04-18 23:43:26 +08:00
    @yzmm 你好,谢谢回复。我用的 es 2.1,spring data es 是支持的,但是在我的那种情况下,虽然使用了 alias ,但是实体类默认见的是使用同一个 alias 的第一个 index 的类。也就是说,如果 index-201602 , index-201603, index-201604 的 alias 都为 index-alias 的话, spring data 建立的对象为 index-201602 的,而不是全部的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5574 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:46 · PVG 13:46 · LAX 22:46 · JFK 01:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.