您的位置:首页 > 其它

elasticsearch的准实时(near real-time)查询

2016-04-08 15:46 423 查看
elasticsearch是基于lucene的,lucene是可以做到实时的,就是创建索引之后,立即能查询到。

但是这样,要么是牺牲索引的效率,每次都索引之后都刷新,要么就是牺牲查询的效率每次查询之前都进行刷新。

索引之后进行刷新是通过:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

elasticClient.prepareIndex("indexName", "Person")
.setSource(
XContentFactory.jsonBuilder()
.startObject()
.field("name", "zhangsan")
.field("desc", "you are good chaoji good")
.field("age", 18)
.field("height", 256789l)
.field("sex", "M")
.field("bool", true)
.field("double", 33.6f)
.field("date", new Date(16554755464l))
.endObject())
.setRefresh(true)
.execute().actionGet();

进行搜索前进行刷新

1

elasticClient.admin().indices().refresh(new RefreshRequest("indexName"));

无论哪一种,都会让你的性能下降10倍以上,所以只能采取一种折中的方案,每隔n秒自动刷新,这样你创建索引之后,最多在ns之内肯定能查到。

这就是所谓的准实时(near real-time)查询。

构建客户端的时候设置

12
3
4
5
6
7

Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true)
.put("index.refresh_interval", "1s")
.put("cluster.name","elasticsearch")
.build();
 
TransportClient client = new TransportClient(settings);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: