- Elasticsearch 7.0 Cookbook(Fourth Edition)
- Alberto Paro
- 309字
- 2021-06-24 14:52:28
There's more...
Elasticsearch allows you to pass the index API URL to several query parameters to control how the document is indexed. The most used ones are as follows:
- routing: This controls the shard to be used for indexing, that is:
POST /myindex/_doc?routing=1
- consistency(one/quorum/all): By default, an index operation succeeds if a quorum (>replica/2+1) of active shards is available. The right consistency value can be changed for index action:
POST /myindex/_doc?consistency=one
- replication (sync/async): Elasticsearch returns from an index operation when all the shards of the current replication group have executed the index operation. Setting the async replication allows us to execute the index action synchronously only on the primary shard and asynchronously on secondary shards. In this way, the API call returns the response action faster:
POST /myindex/_doc?replication=async
- version: The version allows us to use the optimistic concurrency control (http://en.wikipedia.org/wiki/Optimistic_concurrency_control). The first time index of a document, its version 1, is set on the document. At every update, this value is incremented. Optimistic concurrency control is a way to manage concurrency in every insert or update operation. The passed version value is the last seen version (usually returned by a get or a search). The index happens only if the current index version value is equal to the passed one:
POST /myindex/_doc?version=2
- op_type: This can be used to force a create on a document. If a document with the same ID exists, the index fails:
POST /myindex/_doc?op_type=create
- refresh: This forces a refresh after having indexed the document. It allows documents to be ready for searching after their indexing:
POST /myindex/_doc?refresh=true
- timeout: This defines a time to wait for the primary shard to be available. Sometimes, the primary shard is not in a writable status (if it's relocating or recovering from a gateway) and a timeout for the write operation is raised after 1 minute:
POST /myindex/_doc?timeout=5m