Elasticsearch NEST - document immediately after indexing is not found
Asked Answered
I

2

5

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.

Igniter answered 6/9, 2018 at 18:15 Comment(0)
B
8

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.

Blatherskite answered 6/9, 2018 at 21:59 Comment(1)
Wow, thanks! Yes, my problem occured when writing tests, so your solution is perfect fit for me! ThanksIgniter
W
0

Wait for index refresh after document indexing:

await client.IndexAsync(document, x => x.Refresh(Refresh.WaitFor));

Just ask Elasticsearch to refresh index at any time:

await client.Indices.RefreshAsync("your_index_name");

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html

Watersick answered 17/1, 2023 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.