The new item is not fetching with search result immediately after add

Hello,

I am working on a very small sample for CURD operations in elasticsearch. I am using Nest library in my ASP.NET MVC web application. The problem I am setting is that when I add any item and then redirect to list (search result) the the newly added item never exist in result. When I refresh the page (query again) then only I am able to get the newly added item.

Here I am sharing the methods:
Get:

 public IEnumerable<Department> GetDepartments()
        {
            var searchResponse = ESClient().Search<Department>(s => s
                                                  .From(0)
                                                  .Size(200)
                                                  .Index("myindex").Type("department"));
            return searchResponse.Documents;
        }

Add:

public string CreateDepartment(Department dept)
{
var response = ESClient().Index(dept, i => i.Index("myindex").Type("department").Id(dept.DepartmentId));
return response.IsValid == false ? response.OriginalException.Message : string.Empty;
}

When my controller post the form and call CreateDepartment, code successfully runs and I return to the action which calls GetDepartments, but this result never include my new item.

Hope my issue is clear. Please help and guide ..why elasticsearch showing this behavior .. how to get the new item in search.

Have a read of https://www.elastic.co/guide/en/elasticsearch/guide/2.x/near-real-time.html#near-real-time, it's still valid for 5 and 6.

Thanks for quick response.

So, should I refresh after add/update operations?

That is correct.

The post is suggesting not to refresh, what should I do than .. as a user I will accept that the added item should appear in the list .. any suggestions.

refresh=wait_for

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.