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

2017-04-17 16:40:19 +08:00
 hujianxin

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

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

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

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

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

1779 次点击
所在节点    程序员
2 条回复
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
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 的,而不是全部的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/355426

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX