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

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)查询。
构建客户端的时候设置

1
2
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);
  1. akira说道:

    我用的是ES5.6.3,根据文档,ES默认的index.refresh_interval即为1s。
    https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index-modules.html#dynamic-index-settings

回复 取消

提示:你的email不会被公布,欢迎留言^_^

*

验证码 *