index
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Create or update a document in an index.
Add a JSON document to the specified data stream or index and make itsearchable.If the target is an index and the document already exists, the requestupdates the document and increments its version.
NOTE: You cannot use this API to send update requests for existing documentsin a data stream.
If the Elasticsearch security features are enabled, you must have thefollowing index privileges for the target data stream, index, or index alias:
* To add or overwrite a document using the `PUT /<target>/_doc/<_id>` requestformat, you must have the `create`, `index`, or `write` index privilege.* To add a document using the `POST /<target>/_doc/` request format, you musthave the `create_doc`, `create`, `index`, or `write` index privilege.* To automatically create a data stream or index with this API request, youmust have the `auto_configure`, `create_index`, or `manage` index privilege.
Automatic data stream creation requires a matching index template with datastream enabled.
NOTE: Replica shards might not all be started when an indexing operationreturns successfully.By default, only the primary is required. Set `wait_for_active_shards` tochange this default behavior.
**Automatically create data streams and indices**
If the request's target doesn't exist and matches an index template with a`data_stream` definition, the index operation automatically creates the datastream.
If the target doesn't exist and doesn't match a data stream template, theoperation automatically creates the index and applies any matching indextemplates.
NOTE: Elasticsearch includes several built-in index templates. To avoidnaming collisions with these templates, refer to index pattern documentation.
If no mapping exists, the index operation creates a dynamic mapping.By default, new fields and objects are automatically added to the mapping ifneeded.
Automatic index creation is controlled by the `action.auto_create_index`setting.If it is `true`, any index can be created automatically.You can modify this setting to explicitly allow or block automatic creationof indices that match specified patterns or set it to `false` to turn offautomatic index creation entirely.Specify a comma-separated list of patterns you want to allow or prefix eachpattern with `+` or `-` to indicate whether it should be allowed or blocked.When a list is specified, the default behaviour is to disallow.
NOTE: The `action.auto_create_index` setting affects the automatic creationof indices only.It does not affect the creation of data streams.
**Optimistic concurrency control**
Index operations can be made conditional and only be performed if the lastmodification to the document was assigned the sequence number and primaryterm specified by the `if_seq_no` and `if_primary_term` parameters.If a mismatch is detected, the operation will result in a`VersionConflictException` and a status code of `409`.
**Routing**
By default, shard placement — or routing — is controlled by using a hash ofthe document's ID value.For more explicit control, the value fed into the hash function used by therouter can be directly specified on a per-operation basis using the `routing`parameter.
When setting up explicit mapping, you can also use the `_routing` field todirect the index operation to extract the routing value from the documentitself.This does come at the (very minimal) cost of an additional document parsingpass.If the `_routing` mapping is defined and set to be required, the indexoperation will fail if no routing value is provided or extracted.
NOTE: Data streams do not support custom routing unless they were createdwith the `allow_custom_routing` setting enabled in the template.
**Distributed**
The index operation is directed to the primary shard based on its route andperformed on the actual node containing this shard.After the primary shard completes the operation, if needed, the update isdistributed to applicable replicas.
**Active shards**
To improve the resiliency of writes to the system, indexing operations can beconfigured to wait for a certain number of active shard copies beforeproceeding with the operation.If the requisite number of active shard copies are not available, then thewrite operation must wait and retry, until either the requisite shard copieshave started or a timeout occurs.By default, write operations only wait for the primary shards to be activebefore proceeding (that is to say `wait_for_active_shards` is `1`).This default can be overridden in the index settings dynamically by setting`index.write.wait_for_active_shards`.To alter this behavior per operation, use the `wait_for_active_shardsrequest` parameter.
Valid values are all or any positive integer up to the total number ofconfigured copies per shard in the index (which is `number_of_replicas`+1).Specifying a negative value or a number greater than the number of shardcopies will throw an error.
For example, suppose you have a cluster of three nodes, A, B, and C and youcreate an index index with the number of replicas set to 3 (resulting in 4shard copies, one more copy than there are nodes).If you attempt an indexing operation, by default the operation will onlyensure the primary copy of each shard is available before proceeding.This means that even if B and C went down and A hosted the primary shardcopies, the indexing operation would still proceed with only one copy of thedata.If `wait_for_active_shards` is set on the request to `3` (and all three nodesare up), the indexing operation will require 3 active shard copies beforeproceeding.This requirement should be met because there are 3 active nodes in thecluster, each one holding a copy of the shard.However, if you set `wait_for_active_shards` to `all` (or to `4`, which isthe same in this situation), the indexing operation will not proceed as youdo not have all 4 copies of each shard active in the index.The operation will timeout unless a new node is brought up in the cluster tohost the fourth copy of the shard.
It is important to note that this setting greatly reduces the chances of thewrite operation not writing to the requisite number of shard copies, but itdoes not completely eliminate the possibility, because this check occursbefore the write operation starts.After the write operation is underway, it is still possible for replicationto fail on any number of shard copies but still succeed on the primary.The `_shards` section of the API response reveals the number of shard copieson which replication succeeded and failed.
**No operation (noop) updates**
When updating a document by using this API, a new version of the document isalways created even if the document hasn't changed.If this isn't acceptable use the `_update` API with `detect_noop` set to`true`.The `detect_noop` option isn't available on this API because it doesn’t fetchthe old source and isn't able to compare it against the new source.
There isn't a definitive rule for when noop updates aren't acceptable.It's a combination of lots of factors like how frequently your data sourcesends updates that are actually noops and how many queries per secondElasticsearch runs on the shard receiving the updates.
**Versioning**
Each indexed document is given a version number.By default, internal versioning is used that starts at 1 and increments witheach update, deletes included.Optionally, the version number can be set to an external value (for example,if maintained in a database).To enable this functionality, `version_type` should be set to `external`.The value provided must be a numeric, long value greater than or equal to 0,and less than around `9.2e+18`.
NOTE: Versioning is completely real time, and is not affected by the nearreal time aspects of search operations.If no version is provided, the operation runs without any version checks.
When using the external version type, the system checks to see if the versionnumber passed to the index request is greater than the version of thecurrently stored document.If true, the document will be indexed and the new version number used.If the value provided is less than or equal to the stored document's versionnumber, a version conflict will occur and the index operation will fail. Forexample:
```PUT my-index-000001/_doc/1?version=2&version_type=external
{ "user": { "id": "elkbee" }}In this example, the operation will succeed since the supplied version of 2is higher than the current document version of 1.If the document was already updated and its version was set to 2 or higher,the indexing command will fail and result in a conflict (409 HTTP statuscode).
A nice side effect is that there is no need to maintain strict ordering ofasync indexing operations run as a result of changes to a source database, aslong as version numbers from the source database are used.Even the simple case of updating the Elasticsearch index using data from adatabase is simplified if external versioning is used, as only the latestversion will be used if the index operations arrive out of order.
Index¶
- Variables
- type Index
- func (r Index) Do(providedCtx context.Context) (*Response, error)
- func (r *Index) Document(document any) *Index
- func (r *Index) ErrorTrace(errortrace bool) *Index
- func (r *Index) FilterPath(filterpaths ...string) *Index
- func (r *Index) Header(key, value string) *Index
- func (r *Index) HttpRequest(ctx context.Context) (*http.Request, error)
- func (r *Index) Human(human bool) *Index
- func (r *Index) Id(id string) *Index
- func (r *Index) IfPrimaryTerm(ifprimaryterm string) *Index
- func (r *Index) IfSeqNo(sequencenumber string) *Index
- func (r *Index) IncludeSourceOnError(includesourceonerror bool) *Index
- func (r *Index) OpType(optype optype.OpType) *Index
- func (r Index) Perform(providedCtx context.Context) (*http.Response, error)
- func (r *Index) Pipeline(pipeline string) *Index
- func (r *Index) Pretty(pretty bool) *Index
- func (r *Index) Raw(raw io.Reader) *Index
- func (r *Index) Refresh(refresh refresh.Refresh) *Index
- func (r *Index) Request(req any) *Index
- func (r *Index) RequireAlias(requirealias bool) *Index
- func (r *Index) RequireDataStream(requiredatastream bool) *Index
- func (r *Index) Routing(routing string) *Index
- func (r *Index) Timeout(duration string) *Index
- func (r *Index) Version(versionnumber string) *Index
- func (r *Index) VersionType(versiontype versiontype.VersionType) *Index
- func (r *Index) WaitForActiveShards(waitforactiveshards string) *Index
- type NewIndex
- type Request
- type Response
Constants¶
This section is empty.
Variables¶
var ErrBuildPath =errors.New("cannot build path, check for missing path parameters")ErrBuildPath is returned in case of missing parameters within the build of the request.
Functions¶
This section is empty.
Types¶
typeIndex¶
type Index struct {// contains filtered or unexported fields}funcNew¶
func New(tpelastictransport.Interface) *Index
Create or update a document in an index.
Add a JSON document to the specified data stream or index and make itsearchable.If the target is an index and the document already exists, the requestupdates the document and increments its version.
NOTE: You cannot use this API to send update requests for existing documentsin a data stream.
If the Elasticsearch security features are enabled, you must have thefollowing index privileges for the target data stream, index, or index alias:
* To add or overwrite a document using the `PUT /<target>/_doc/<_id>` requestformat, you must have the `create`, `index`, or `write` index privilege.* To add a document using the `POST /<target>/_doc/` request format, you musthave the `create_doc`, `create`, `index`, or `write` index privilege.* To automatically create a data stream or index with this API request, youmust have the `auto_configure`, `create_index`, or `manage` index privilege.
Automatic data stream creation requires a matching index template with datastream enabled.
NOTE: Replica shards might not all be started when an indexing operationreturns successfully.By default, only the primary is required. Set `wait_for_active_shards` tochange this default behavior.
**Automatically create data streams and indices**
If the request's target doesn't exist and matches an index template with a`data_stream` definition, the index operation automatically creates the datastream.
If the target doesn't exist and doesn't match a data stream template, theoperation automatically creates the index and applies any matching indextemplates.
NOTE: Elasticsearch includes several built-in index templates. To avoidnaming collisions with these templates, refer to index pattern documentation.
If no mapping exists, the index operation creates a dynamic mapping.By default, new fields and objects are automatically added to the mapping ifneeded.
Automatic index creation is controlled by the `action.auto_create_index`setting.If it is `true`, any index can be created automatically.You can modify this setting to explicitly allow or block automatic creationof indices that match specified patterns or set it to `false` to turn offautomatic index creation entirely.Specify a comma-separated list of patterns you want to allow or prefix eachpattern with `+` or `-` to indicate whether it should be allowed or blocked.When a list is specified, the default behaviour is to disallow.
NOTE: The `action.auto_create_index` setting affects the automatic creationof indices only.It does not affect the creation of data streams.
**Optimistic concurrency control**
Index operations can be made conditional and only be performed if the lastmodification to the document was assigned the sequence number and primaryterm specified by the `if_seq_no` and `if_primary_term` parameters.If a mismatch is detected, the operation will result in a`VersionConflictException` and a status code of `409`.
**Routing**
By default, shard placement — or routing — is controlled by using a hash ofthe document's ID value.For more explicit control, the value fed into the hash function used by therouter can be directly specified on a per-operation basis using the `routing`parameter.
When setting up explicit mapping, you can also use the `_routing` field todirect the index operation to extract the routing value from the documentitself.This does come at the (very minimal) cost of an additional document parsingpass.If the `_routing` mapping is defined and set to be required, the indexoperation will fail if no routing value is provided or extracted.
NOTE: Data streams do not support custom routing unless they were createdwith the `allow_custom_routing` setting enabled in the template.
**Distributed**
The index operation is directed to the primary shard based on its route andperformed on the actual node containing this shard.After the primary shard completes the operation, if needed, the update isdistributed to applicable replicas.
**Active shards**
To improve the resiliency of writes to the system, indexing operations can beconfigured to wait for a certain number of active shard copies beforeproceeding with the operation.If the requisite number of active shard copies are not available, then thewrite operation must wait and retry, until either the requisite shard copieshave started or a timeout occurs.By default, write operations only wait for the primary shards to be activebefore proceeding (that is to say `wait_for_active_shards` is `1`).This default can be overridden in the index settings dynamically by setting`index.write.wait_for_active_shards`.To alter this behavior per operation, use the `wait_for_active_shardsrequest` parameter.
Valid values are all or any positive integer up to the total number ofconfigured copies per shard in the index (which is `number_of_replicas`+1).Specifying a negative value or a number greater than the number of shardcopies will throw an error.
For example, suppose you have a cluster of three nodes, A, B, and C and youcreate an index index with the number of replicas set to 3 (resulting in 4shard copies, one more copy than there are nodes).If you attempt an indexing operation, by default the operation will onlyensure the primary copy of each shard is available before proceeding.This means that even if B and C went down and A hosted the primary shardcopies, the indexing operation would still proceed with only one copy of thedata.If `wait_for_active_shards` is set on the request to `3` (and all three nodesare up), the indexing operation will require 3 active shard copies beforeproceeding.This requirement should be met because there are 3 active nodes in thecluster, each one holding a copy of the shard.However, if you set `wait_for_active_shards` to `all` (or to `4`, which isthe same in this situation), the indexing operation will not proceed as youdo not have all 4 copies of each shard active in the index.The operation will timeout unless a new node is brought up in the cluster tohost the fourth copy of the shard.
It is important to note that this setting greatly reduces the chances of thewrite operation not writing to the requisite number of shard copies, but itdoes not completely eliminate the possibility, because this check occursbefore the write operation starts.After the write operation is underway, it is still possible for replicationto fail on any number of shard copies but still succeed on the primary.The `_shards` section of the API response reveals the number of shard copieson which replication succeeded and failed.
**No operation (noop) updates**
When updating a document by using this API, a new version of the document isalways created even if the document hasn't changed.If this isn't acceptable use the `_update` API with `detect_noop` set to`true`.The `detect_noop` option isn't available on this API because it doesn’t fetchthe old source and isn't able to compare it against the new source.
There isn't a definitive rule for when noop updates aren't acceptable.It's a combination of lots of factors like how frequently your data sourcesends updates that are actually noops and how many queries per secondElasticsearch runs on the shard receiving the updates.
**Versioning**
Each indexed document is given a version number.By default, internal versioning is used that starts at 1 and increments witheach update, deletes included.Optionally, the version number can be set to an external value (for example,if maintained in a database).To enable this functionality, `version_type` should be set to `external`.The value provided must be a numeric, long value greater than or equal to 0,and less than around `9.2e+18`.
NOTE: Versioning is completely real time, and is not affected by the nearreal time aspects of search operations.If no version is provided, the operation runs without any version checks.
When using the external version type, the system checks to see if the versionnumber passed to the index request is greater than the version of thecurrently stored document.If true, the document will be indexed and the new version number used.If the value provided is less than or equal to the stored document's versionnumber, a version conflict will occur and the index operation will fail. Forexample:
```PUT my-index-000001/_doc/1?version=2&version_type=external
{ "user": { "id": "elkbee" }}In this example, the operation will succeed since the supplied version of 2is higher than the current document version of 1.If the document was already updated and its version was set to 2 or higher,the indexing command will fail and result in a conflict (409 HTTP statuscode).
A nice side effect is that there is no need to maintain strict ordering ofasync indexing operations run as a result of changes to a source database, aslong as version numbers from the source database are used.Even the simple case of updating the Elasticsearch index using data from adatabase is simplified if external versioning is used, as only the latestversion will be used if the index operations arrive out of order.
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
func (Index)Do¶
Do runs the request through the transport, handle the response and returns a index.Response
func (*Index)Document¶added inv8.9.0
Document allows to set the request property with the appropriate payload.
func (*Index)ErrorTrace¶added inv8.14.0
ErrorTrace When set to `true` Elasticsearch will include the full stack trace of errorswhen they occur.API name: error_trace
func (*Index)FilterPath¶added inv8.14.0
FilterPath Comma-separated list of filters in dot notation which reduce the responsereturned by Elasticsearch.API name: filter_path
func (*Index)HttpRequest¶
HttpRequest returns the http.Request object built from thegiven parameters.
func (*Index)Human¶added inv8.14.0
Human When set to `true` will return statistics in a format suitable for humans.For example `"exists_time": "1h"` for humans and`"eixsts_time_in_millis": 3600000` for computers. When disabled the humanreadable values will be omitted. This makes sense for responses beingconsumedonly by machines.API name: human
func (*Index)Id¶
Id A unique identifier for the document.To automatically generate a document ID, use the `POST /<target>/_doc/`request format and omit this parameter.API Name: id
func (*Index)IfPrimaryTerm¶
IfPrimaryTerm Only perform the operation if the document has this primary term.API name: if_primary_term
func (*Index)IfSeqNo¶
IfSeqNo Only perform the operation if the document has this sequence number.API name: if_seq_no
func (*Index)IncludeSourceOnError¶added inv8.18.0
IncludeSourceOnError True or false if to include the document source in the error message in caseof parsing errors.API name: include_source_on_error
func (*Index)OpType¶
OpType Set to `create` to only index the document if it does not already exist (putif absent).If a document with the specified `_id` already exists, the indexing operationwill fail.The behavior is the same as using the `<index>/_create` endpoint.If a document ID is specified, this paramater defaults to `index`.Otherwise, it defaults to `create`.If the request targets a data stream, an `op_type` of `create` is required.API name: op_type
func (Index)Perform¶added inv8.7.0
Perform runs the http.Request through the provided transport and returns an http.Response.
func (*Index)Pipeline¶
Pipeline The ID of the pipeline to use to preprocess incoming documents.If the index has a default ingest pipeline specified, then setting the valueto `_none` disables the default ingest pipeline for this request.If a final pipeline is configured it will always run, regardless of the valueof this parameter.API name: pipeline
func (*Index)Pretty¶added inv8.14.0
Pretty If set to `true` the returned JSON will be "pretty-formatted". Only usethis option for debugging only.API name: pretty
func (*Index)Raw¶
Raw takes a json payload as input which is then passed to the http.RequestIf specified Raw takes precedence on Request method.
func (*Index)Refresh¶
Refresh If `true`, Elasticsearch refreshes the affected shards to make this operationvisible to search.If `wait_for`, it waits for a refresh to make this operation visible tosearch.If `false`, it does nothing with refreshes.API name: refresh
func (*Index)RequireAlias¶
RequireAlias If `true`, the destination must be an index alias.API name: require_alias
func (*Index)RequireDataStream¶added inv8.19.0
RequireDataStream If `true`, the request's actions must target a data stream (existing or to becreated).API name: require_data_stream
func (*Index)Routing¶
Routing A custom value that is used to route operations to a specific shard.API name: routing
func (*Index)Timeout¶
Timeout The period the request waits for the following operations: automatic indexcreation, dynamic mapping updates, waiting for active shards.
This parameter is useful for situations where the primary shard assigned toperform the operation might not be available when the operation runs.Some reasons for this might be that the primary shard is currently recoveringfrom a gateway or undergoing relocation.By default, the operation will wait on the primary shard to become availablefor at least 1 minute before failing and responding with an error.The actual wait time could be longer, particularly when multiple waits occur.API name: timeout
func (*Index)Version¶
Version An explicit version number for concurrency control.It must be a non-negative long number.API name: version
func (*Index)VersionType¶
func (r *Index) VersionType(versiontypeversiontype.VersionType) *Index
VersionType The version type.API name: version_type
func (*Index)WaitForActiveShards¶
WaitForActiveShards The number of shard copies that must be active before proceeding with theoperation.You can set it to `all` or any positive integer up to the total number ofshards in the index (`number_of_replicas+1`).The default value of `1` means it waits for each primary shard to be active.API name: wait_for_active_shards
typeNewIndex¶
NewIndex type alias for index.
funcNewIndexFunc¶
func NewIndexFunc(tpelastictransport.Interface)NewIndex
NewIndexFunc returns a new instance of Index with the provided transport.Used in the index of the library this allows to retrieve every apis in once place.
typeRequest¶added inv8.11.0
type Request =json.RawMessage
Request holds the request body struct for the package index
typeResponse¶added inv8.7.0
type Response struct {ForcedRefresh *bool `json:"forced_refresh,omitempty"`// Id_ The unique identifier for the added document.Id_string `json:"_id"`// Index_ The name of the index the document was added to.Index_string `json:"_index"`// PrimaryTerm_ The primary term assigned to the document for the indexing operation.PrimaryTerm_ *int64 `json:"_primary_term,omitempty"`// Result The result of the indexing operation: `created` or `updated`.Resultresult.Result `json:"result"`// SeqNo_ The sequence number assigned to the document for the indexing operation.// Sequence numbers are used to ensure an older version of a document doesn't// overwrite a newer version.SeqNo_ *int64 `json:"_seq_no,omitempty"`// Shards_ Information about the replication process of the operation.Shards_types.ShardStatistics `json:"_shards"`// Version_ The document version, which is incremented each time the document is updated.Version_int64 `json:"_version"`}Response holds the response body struct for the package index