Search code examples
c#elasticsearchnest

What is the difference between CreateAsync and IndexAsync methods on ElasticSearch NEST client?


I found that Create() method needs an Id field in the entity class but Index() doesn't need it. I cannot figure out why.


Solution

  • There's a fundamental difference between the create and index APIs in Elasticsearch;

    1. create API will create the document if it does not exist, and will return an error if it does exist. The create API request must contain the index, type and id in the request URI. The id field can be inferred from the POCO or can be explicitly set on the request.
    2. index API will create the document if it does not exist and will overwrite the document if it does exist. Passing an id is optional, and if an id is not passed, Elasticsearch will generate an id for the document.

    This difference is reflected in the Create() and Index() methods exposed on the .NET clients.