frosty
frosty

Reputation: 2852

ElasticSearch: Query for data immediately after write

I have a requirement to be able to read the data immediately by key after writing into ES. Is this possible? Note that DataMap is a key value pair and _id is pathed to key field.

string v = "Foobar" + i;
string k = Security.Hash(Encoding.UTF8.GetBytes(v));

var data = new DataMap { Key = k, Value = v };
var index = _esclient.Index(data);

// fetch by k
var results = _esclient.Search<DataMap>(p => p
        .Size(1000)
        .Fields(f => f.Key, f => f.Value)
        .Query(q => q.Term("key", k))
        );

// Make sure the record is found
if (!results.Hits.Any())
{
    Console.WriteLine("{0} {1} not found", k, v);
}

Upvotes: 1

Views: 716

Answers (1)

BenG
BenG

Reputation: 1312

Yes. The GET API is realtime and will return a document by its _id immediately after receiving it, even if the index hasn't been refreshed yet.

Note that it might be simpler for you to provide the unique _id to Elasticsearch, instead of letting it generate one for you. Otherwise you'll have to read the response from the indexing operation to learn the _id of the document.

Upvotes: 1

Related Questions