Elasticsearch NEST - document immediately after indexing is not found

Viewed 429

I'm using .NET NEST for searching in Elasticsearch.

When I index a document and immediately search for it it is not found:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("products_test");
settings.DisableDirectStreaming(true);
ElasticClient client = new ElasticClient(settings);

Product p = new Product("My Product", "6");
client.IndexDocument(p);

var results = client.Search<Product>(s => s.Query(q => q.MatchAll()));

results.HitsMetadata.Total //is 0 and results.Hits are empty

Why?

Do I have to commit somehow?

Thanks

EDIT: But when I run the Console App again and comment out the creation, the document IS found.

1 Answers

An indexed document is not searchable until the document is written to a shard segment of an index. The refresh_interval index setting is responsible for how often this occurs, with a default of 1 second. Note that an indexed document is immediately available after indexing, retrievable by ID.

When indexing a document, you can specify that a refresh happens following indexing, so that the document is searchable after the response is returned

var client = new ElasticClient();

client.Index(new MyDocument(1) { Message = "foo" }, i => i
    .Refresh(Refresh.WaitFor)
);

or calling the Refresh API

client.Refresh("my-index");

In a production environment however, it's generally not recommended to do this because writing many small segments will have a larger performance impact on the cluster with regards to resources and segment merging operations. It can be useful for ad-hoc and testing purposes however.

Related