java - elasticsearch 在同时进行更新和搜索时需要时间

标签 java elasticsearch

我正在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/

相关文章:

java - 如何将json反序列化为多态类?

java - Tomcat 7 Liferay 6.2 关于损坏数据库的奇怪错误

elasticsearch - Elastic Search如何选择列并通过过滤器?

json - 尝试从带有Elasticsearch的多个术语的查询中获取过滤后的响应

elasticsearch - 弹性-选择前5个匹配后进行过滤

java - 如何将2个项目集成为一个.WAR,独立部署和维护?

java - 由于内存泄漏导致tomcat崩溃

java - 我的后端 API 名称是什么?它在哪里?从 Android Studio 客户端调用 Google Cloud 端点后端 API

elasticsearch - Elasticsearch在发生脑裂后未分配一些索引

elasticsearch - elasticsearch在字段上返回不正确的value_count