elasticsearch是基于lucene的,lucene是可以做到实时的,就是创建索引之后,立即能查询到。
但是这样,要么是牺牲索引的效率,每次都索引之后都刷新,要么就是牺牲查询的效率每次查询之前都进行刷新。
索引之后进行刷新是通过:
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();
进行搜索前进行刷新
elasticClient.admin().indices().refresh(new RefreshRequest("indexName"));
无论哪一种,都会让你的性能下降10倍以上,所以只能采取一种折中的方案,每隔n秒自动刷新,这样你创建索引之后,最多在ns之内肯定能查到。
这就是所谓的准实时(near real-time)查询。
构建客户端的时候设置
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true)
.put("index.refresh_interval", "1s")
.put("cluster.name","elasticsearch")
.build();
TransportClient client = new TransportClient(settings);
注意:上面这些写法是elasticsearch5.x以前的版本。
Elasctisearch5.x实现方式如下:
①索引之后立刻更新实现:.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
String index = "wareic-bak";
String type = "product";
String productId = "4";
String data = "{\"categoryID\":1,\"categorySubID\":\"3\"}";
IndexResponse response = client.prepareIndex(index,type,productId).setSource(data).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
curl -XPUT 'http://localhost:9200/wareic_bak/_settings?preserve_existing=true' -d '{
"index.refresh_interval" : "100s"
}'
若是已经设置过refresh_interval,想再修改refresh_interval使用如下方式
curl -XPUT 'http://localhost:9200/wareic_bak/_settings' -d '{
"index.refresh_interval" : "100s"
}'
文章来源:http://zhaoyanblog.com/archives/299.html