我正在elasticsearch文档中进行更新,同时一个rest api正在调用搜索结果。下面是我的代码。
public String updateElasticsearchDocument(String index, String type, List<String> indexID) {
Client client = ESClientFactory.getInstance();
UpdateResponse updateResponse = null;
JSONObject jsonResponse = new JSONObject();
JSONObject json =new JSONObject();
int i=1;
try {
for(String id : indexID)
{
updateResponse = client.prepareUpdate(index, type, id)
.setDoc(jsonBuilder()
.startObject().field("view_mode", "read")
.endObject())
.setDocAsUpsert(true)
.setFields("_source")
.execute().actionGet();
logger.info("updating the document for type= "+ updateResponse.getType()+ " for id= "+ updateResponse.getId());
json.put("indexID"+i, updateResponse.getId());
i++;
}
jsonResponse.put("updated_index", json);
} catch (ActionRequestValidationException e) {
logger.warn(this.getClass().getName() + ":" + "updateDocument: "
+ e.getMessage(), e);
}
catch (ElasticsearchException e) {
logger.warn(this.getClass().getName() + ":" + "updateDocument: "
+ e.getMessage(), e);
e.printStackTrace();
} catch (IOException e) {
logger.warn(this.getClass().getName() + ":" + "updateDocument: "
+ e.getMessage(), e);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResponse.toString();
}
我正在使用的搜索查询在这里
POST /monitoring/quota-management/_search
{
"query": {"match": {
"view_mode": "read"
}},
"sort": [
{
"_timestamp": {
"order": "desc"
}
}
],
"size": 10
}
现在这两个请求之间存在时间间隔,我必须等待大约 40-50 秒才能获得更新的搜索结果。这正在影响生产应用。请让我知道这里需要做什么来最大限度地减少所花费的时间
-苏巴迪普
最佳答案
如果您希望数据立即建立索引,您可以设置更新时刷新,如下所示:
client.prepareUpdate(index, type, id).setRefresh(true).setDoc(...).execute().actionGet();
只要知道,如果您更新非常频繁,那么这样做就需要权衡。索引会消耗大量 IO/CPU。
一个好的中间立场是将刷新间隔设置为合理的间隔。
关于java - elasticsearch 在同时进行更新和搜索时需要时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25443945/