|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import typing as t |
| 19 | + |
| 20 | +from elastic_transport import ObjectApiResponse |
| 21 | + |
| 22 | +from ._base import NamespacedClient |
| 23 | +from .utils import ( |
| 24 | + SKIP_IN_PATH, |
| 25 | + Stability, |
| 26 | + _quote, |
| 27 | + _rewrite_parameters, |
| 28 | + _stability_warning, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class SimulateClient(NamespacedClient): |
| 33 | + |
| 34 | + @_rewrite_parameters( |
| 35 | + body_fields=( |
| 36 | + "docs", |
| 37 | + "component_template_substitutions", |
| 38 | + "index_template_subtitutions", |
| 39 | + "mapping_addition", |
| 40 | + "pipeline_substitutions", |
| 41 | + ), |
| 42 | + ) |
| 43 | + @_stability_warning(Stability.EXPERIMENTAL) |
| 44 | + async def ingest( |
| 45 | + self, |
| 46 | + *, |
| 47 | + docs: t.Optional[t.Sequence[t.Mapping[str, t.Any]]] = None, |
| 48 | + index: t.Optional[str] = None, |
| 49 | + component_template_substitutions: t.Optional[ |
| 50 | + t.Mapping[str, t.Mapping[str, t.Any]] |
| 51 | + ] = None, |
| 52 | + error_trace: t.Optional[bool] = None, |
| 53 | + filter_path: t.Optional[t.Union[str, t.Sequence[str]]] = None, |
| 54 | + human: t.Optional[bool] = None, |
| 55 | + index_template_subtitutions: t.Optional[ |
| 56 | + t.Mapping[str, t.Mapping[str, t.Any]] |
| 57 | + ] = None, |
| 58 | + mapping_addition: t.Optional[t.Mapping[str, t.Any]] = None, |
| 59 | + pipeline: t.Optional[str] = None, |
| 60 | + pipeline_substitutions: t.Optional[ |
| 61 | + t.Mapping[str, t.Mapping[str, t.Any]] |
| 62 | + ] = None, |
| 63 | + pretty: t.Optional[bool] = None, |
| 64 | + body: t.Optional[t.Dict[str, t.Any]] = None, |
| 65 | + ) -> ObjectApiResponse[t.Any]: |
| 66 | + """ |
| 67 | + Simulate data ingestion. Run ingest pipelines against a set of provided documents, |
| 68 | + optionally with substitute pipeline definitions, to simulate ingesting data into |
| 69 | + an index. This API is meant to be used for troubleshooting or pipeline development, |
| 70 | + as it does not actually index any data into Elasticsearch. The API runs the default |
| 71 | + and final pipeline for that index against a set of documents provided in the |
| 72 | + body of the request. If a pipeline contains a reroute processor, it follows that |
| 73 | + reroute processor to the new index, running that index's pipelines as well the |
| 74 | + same way that a non-simulated ingest would. No data is indexed into Elasticsearch. |
| 75 | + Instead, the transformed document is returned, along with the list of pipelines |
| 76 | + that have been run and the name of the index where the document would have been |
| 77 | + indexed if this were not a simulation. The transformed document is validated |
| 78 | + against the mappings that would apply to this index, and any validation error |
| 79 | + is reported in the result. This API differs from the simulate pipeline API in |
| 80 | + that you specify a single pipeline for that API, and it runs only that one pipeline. |
| 81 | + The simulate pipeline API is more useful for developing a single pipeline, while |
| 82 | + the simulate ingest API is more useful for troubleshooting the interaction of |
| 83 | + the various pipelines that get applied when ingesting into an index. By default, |
| 84 | + the pipeline definitions that are currently in the system are used. However, |
| 85 | + you can supply substitute pipeline definitions in the body of the request. These |
| 86 | + will be used in place of the pipeline definitions that are already in the system. |
| 87 | + This can be used to replace existing pipeline definitions or to create new ones. |
| 88 | + The pipeline substitutions are used only within this request. |
| 89 | +
|
| 90 | + `<https://www.elastic.co/guide/en/elasticsearch/reference/master/simulate-ingest-api.html>`_ |
| 91 | +
|
| 92 | + :param docs: Sample documents to test in the pipeline. |
| 93 | + :param index: The index to simulate ingesting into. This value can be overridden |
| 94 | + by specifying an index on each document. If you specify this parameter in |
| 95 | + the request path, it is used for any documents that do not explicitly specify |
| 96 | + an index argument. |
| 97 | + :param component_template_substitutions: A map of component template names to |
| 98 | + substitute component template definition objects. |
| 99 | + :param index_template_subtitutions: A map of index template names to substitute |
| 100 | + index template definition objects. |
| 101 | + :param mapping_addition: |
| 102 | + :param pipeline: The pipeline to use as the default pipeline. This value can |
| 103 | + be used to override the default pipeline of the index. |
| 104 | + :param pipeline_substitutions: Pipelines to test. If you don’t specify the `pipeline` |
| 105 | + request path parameter, this parameter is required. If you specify both this |
| 106 | + and the request path parameter, the API only uses the request path parameter. |
| 107 | + """ |
| 108 | + if docs is None and body is None: |
| 109 | + raise ValueError("Empty value passed for parameter 'docs'") |
| 110 | + __path_parts: t.Dict[str, str] |
| 111 | + if index not in SKIP_IN_PATH: |
| 112 | + __path_parts = {"index": _quote(index)} |
| 113 | + __path = f'/_ingest/{__path_parts["index"]}/_simulate' |
| 114 | + else: |
| 115 | + __path_parts = {} |
| 116 | + __path = "/_ingest/_simulate" |
| 117 | + __query: t.Dict[str, t.Any] = {} |
| 118 | + __body: t.Dict[str, t.Any] = body if body is not None else {} |
| 119 | + if error_trace is not None: |
| 120 | + __query["error_trace"] = error_trace |
| 121 | + if filter_path is not None: |
| 122 | + __query["filter_path"] = filter_path |
| 123 | + if human is not None: |
| 124 | + __query["human"] = human |
| 125 | + if pipeline is not None: |
| 126 | + __query["pipeline"] = pipeline |
| 127 | + if pretty is not None: |
| 128 | + __query["pretty"] = pretty |
| 129 | + if not __body: |
| 130 | + if docs is not None: |
| 131 | + __body["docs"] = docs |
| 132 | + if component_template_substitutions is not None: |
| 133 | + __body["component_template_substitutions"] = ( |
| 134 | + component_template_substitutions |
| 135 | + ) |
| 136 | + if index_template_subtitutions is not None: |
| 137 | + __body["index_template_subtitutions"] = index_template_subtitutions |
| 138 | + if mapping_addition is not None: |
| 139 | + __body["mapping_addition"] = mapping_addition |
| 140 | + if pipeline_substitutions is not None: |
| 141 | + __body["pipeline_substitutions"] = pipeline_substitutions |
| 142 | + __headers = {"accept": "application/json", "content-type": "application/json"} |
| 143 | + return await self.perform_request( # type: ignore[return-value] |
| 144 | + "POST", |
| 145 | + __path, |
| 146 | + params=__query, |
| 147 | + headers=__headers, |
| 148 | + body=__body, |
| 149 | + endpoint_id="simulate.ingest", |
| 150 | + path_parts=__path_parts, |
| 151 | + ) |
0 commit comments