bagui
bagui

Reputation: 844

elasticsearch is taking time while doing update and search simultaneously

I'm doing update in elasticsearch document and the same time one rest api is calling for search results. Below is my code.

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();
}

Search query I'm pusing is here

POST /monitoring/quota-management/_search

    {
          "query": {"match": {
              "view_mode": "read"
           }}, 
        "sort": [
           {
              "_timestamp": {
                 "order": "desc"
              }
           }
        ],
        "size": 10
    }

Now there is time gap between these 2 requests, I have to wait for like 40-50 seconds to get the updated search result. This is affecting the production application. Please let me know what needs to be done here to minimizes the time taken

-Subhadip

Upvotes: 0

Views: 592

Answers (1)

coffeeaddict
coffeeaddict

Reputation: 868

If you want the data to be indexed immediately, you could set the refresh on update like this:

client.prepareUpdate(index, type, id).setRefresh(true).setDoc(...).execute().actionGet();

Just know that there are tradeoffs to doing this if you update very frequently. Indexing can eat up a lot of IO/cpu.

A good middle ground would be to set the refresh interval to a reasonable interval.

Upvotes: 1

Related Questions