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();
②設定 每隔100秒自動重新整理
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