1

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);
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
frosty
  • 2,421
  • 6
  • 26
  • 47

1 Answers1

1

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.

BenG
  • 1,292
  • 8
  • 11
  • Thanks, Is using the "path" not same as generating my own id, since _id is referencing one of my property that is unique? What is the equivalent of GET API in .Net NEST library? – frosty Sep 05 '14 at 04:58
  • sorry, I don't know the NEST library myself, but I found this documentation: http://nest.azurewebsites.net/nest/core/get.html – BenG Sep 05 '14 at 22:27