-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy path__init__.py
3250 lines (3044 loc) · 139 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ------------------------------------------------------------------------------------------
# THIS CODE IS AUTOMATICALLY GENERATED AND MANUAL EDITS WILL BE LOST
#
# To contribute, kindly make modifications in the opensearch-py client generator
# or in the OpenSearch API specification, and run `nox -rs generate`. See DEVELOPER_GUIDE.md
# and https://github.com/opensearch-project/opensearch-api-specification for details.
# -----------------------------------------------------------------------------------------+
import logging
from typing import Any, Type
from ..transport import Transport, TransportError
from .cat import CatClient
from .client import Client
from .cluster import ClusterClient
from .dangling_indices import DanglingIndicesClient
from .features import FeaturesClient
from .http import HttpClient
from .indices import IndicesClient
from .ingest import IngestClient
from .insights import InsightsClient
from .list import ListClient
from .nodes import NodesClient
from .plugins import PluginsClient
from .remote import RemoteClient
from .remote_store import RemoteStoreClient
from .search_pipeline import SearchPipelineClient
from .security import SecurityClient
from .snapshot import SnapshotClient
from .tasks import TasksClient
from .utils import SKIP_IN_PATH, _bulk_body, _make_path, query_params
from .wlm import WlmClient
logger = logging.getLogger("opensearch")
class OpenSearch(Client):
"""
OpenSearch client. Provides a straightforward mapping from
Python to OpenSearch REST endpoints.
The instance has attributes ``cat``, ``cluster``, ``indices``, ``ingest``,
``nodes``, ``snapshot`` and ``tasks`` that provide access to instances of
:class:`~opensearchpy.client.CatClient`,
:class:`~opensearchpy.client.ClusterClient`,
:class:`~opensearchpy.client.IndicesClient`,
:class:`~opensearchpy.client.IngestClient`,
:class:`~opensearchpy.client.NodesClient`,
:class:`~opensearchpy.client.SnapshotClient` and
:class:`~opensearchpy.client.TasksClient` respectively. This is the
preferred (and only supported) way to get access to those classes and their
methods.
You can specify your own connection class which should be used by providing
the ``connection_class`` parameter::
# create connection to localhost using the ThriftConnection
client = OpenSearch(connection_class=ThriftConnection)
If you want to turn on sniffing you have several options (described
in :class:`~opensearchpy.Transport`)::
# create connection that will automatically inspect the cluster to get
# the list of active nodes. Start with nodes running on
# 'opensearchnode1' and 'opensearchnode2'
client = OpenSearch(
['opensearchnode1', 'opensearchnode2'],
# sniff before doing anything
sniff_on_start=True,
# refresh nodes after a node fails to respond
sniff_on_connection_fail=True,
# and also every 60 seconds
sniffer_timeout=60
)
Different hosts can have different parameters, use a dictionary per node to
specify those::
# connect to localhost directly and another node using SSL on port 443
# and an url_prefix. Note that ``port`` needs to be an int.
client = OpenSearch([
{'host': 'localhost'},
{'host': 'othernode', 'port': 443, 'url_prefix': 'opensearch', 'use_ssl': True},
])
If using SSL, there are several parameters that control how we deal with
certificates (see :class:`~opensearchpy.AIOHttpConnection` for
detailed description of the options)::
client = OpenSearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# make sure we verify SSL certificates
verify_certs=True,
# provide a path to CA certs on disk
ca_certs='/path/to/CA_certs'
)
If using SSL, but don't verify the certs, a warning message is showed
optionally (see :class:`~opensearchpy.AIOHttpConnection` for
detailed description of the options)::
client = OpenSearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# no verify SSL certificates
verify_certs=False,
# don't verify the hostname in the certificate
ssl_assert_hostname=False,
# don't show warnings about ssl certs verification
ssl_show_warn=False
)
SSL client authentication is supported
(see :class:`~opensearchpy.AIOHttpConnection` for
detailed description of the options)::
client = OpenSearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# make sure we verify SSL certificates
verify_certs=True,
# provide a path to CA certs on disk
ca_certs='/path/to/CA_certs',
# PEM formatted SSL client certificate
client_cert='/path/to/clientcert.pem',
# PEM formatted SSL client key
client_key='/path/to/clientkey.pem'
)
Alternatively you can use RFC-1738 formatted URLs, as long as they are not
in conflict with other options::
client = OpenSearch(
[
'http://user:secret@localhost:9200/',
'https://user:secret@other_host:443/production'
],
verify_certs=True
)
By default, `JSONSerializer
<https://github.com/opensearch-project/opensearch-py/blob/master/opensearch/serializer.py#L24>`_
is used to encode all outgoing requests.
However, you can implement your own custom serializer::
from opensearchpy.serializer import JSONSerializer
class SetEncoder(JSONSerializer):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
if isinstance(obj, Something):
return 'CustomSomethingRepresentation'
return JSONSerializer.default(self, obj)
client = OpenSearch(serializer=SetEncoder())
"""
# include PIT functions inside _patch.py
from ._patch import ( # type: ignore
create_point_in_time,
delete_point_in_time,
list_all_point_in_time,
)
def __init__(
self,
hosts: Any = None,
transport_class: Type[Transport] = Transport,
**kwargs: Any,
) -> None:
"""
:arg hosts: list of nodes, or a single node, we should connect to.
Node should be a dictionary ({"host": "localhost", "port": 9200}),
the entire dictionary will be passed to the :class:`~opensearchpy.Connection`
class as kwargs, or a string in the format of ``host[:port]`` which will be
translated to a dictionary automatically. If no value is given the
:class:`~opensearchpy.Connection` class defaults will be used.
:arg transport_class: :class:`~opensearchpy.Transport` subclass to use.
:arg kwargs: any additional arguments will be passed on to the
:class:`~opensearchpy.Transport` class and, subsequently, to the
:class:`~opensearchpy.Connection` instances.
"""
super().__init__(hosts, transport_class, **kwargs)
# namespaced clients for compatibility with API names
self.wlm = WlmClient(self)
self.list = ListClient(self)
self.insights = InsightsClient(self)
self.search_pipeline = SearchPipelineClient(self)
self.cat = CatClient(self)
self.cluster = ClusterClient(self)
self.dangling_indices = DanglingIndicesClient(self)
self.indices = IndicesClient(self)
self.ingest = IngestClient(self)
self.nodes = NodesClient(self)
self.remote = RemoteClient(self)
self.security = SecurityClient(self)
self.snapshot = SnapshotClient(self)
self.tasks = TasksClient(self)
self.remote_store = RemoteStoreClient(self)
self.features = FeaturesClient(self)
self.plugins = PluginsClient(self)
self.http = HttpClient(self)
def __repr__(self) -> Any:
try:
# get a list of all connections
cons: Any = self.transport.hosts
# truncate to 5 if there are too many
if len(cons) > 5:
cons = cons[:5] + ["..."]
return f"<{self.__class__.__name__}({cons})>"
except Exception:
# probably operating on custom transport and connection_pool, ignore
return super().__repr__()
def __enter__(self) -> Any:
if hasattr(self.transport, "_async_call"):
self.transport._async_call()
return self
def __exit__(self, *_: Any) -> None:
self.close()
def close(self) -> None:
"""Closes the Transport and all internal connections"""
self.transport.close()
# AUTO-GENERATED-API-DEFINITIONS #
@query_params("error_trace", "filter_path", "human", "pretty", "source")
def ping(
self,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Returns whether the cluster is running.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
"""
try:
return self.transport.perform_request(
"HEAD", "/", params=params, headers=headers
)
except TransportError:
return False
@query_params("error_trace", "filter_path", "human", "pretty", "source")
def info(
self,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Returns basic information about the cluster.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
"""
return self.transport.perform_request(
"GET", "/", params=params, headers=headers
)
@query_params(
"error_trace",
"filter_path",
"human",
"pipeline",
"pretty",
"refresh",
"routing",
"source",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def create(
self,
index: Any,
id: Any,
body: Any,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Creates a new document in the index. Returns a 409 response when a document
with a same ID already exists in the index.
:arg index: Name of the data stream or index to target. If the
target doesn't exist and matches the name or wildcard (`*`) pattern of
an index template with a `data_stream` definition, this request creates
the data stream. If the target doesn't exist and doesn't match a data
stream template, this request creates the index.
:arg id: Unique identifier for the document.
:arg body: The document
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pipeline: ID of the pipeline to use to preprocess incoming
documents. If the index has a default ingest pipeline specified, then
setting the value to `_none` disables the default ingest pipeline for
this request. If a final pipeline is configured it will always run,
regardless of the value of this parameter.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg refresh: If `true`, OpenSearch refreshes the affected
shards to make this operation visible to search, if `wait_for` then wait
for a refresh to make this operation visible to search, if `false` do
nothing with refreshes. Valid values: `true`, `false`, `wait_for`.
:arg routing: Custom value used to route operations to a
specific shard.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg timeout: Period the request waits for the following
operations: automatic index creation, dynamic mapping updates, waiting
for active shards.
:arg version: Explicit version number for concurrency control.
The specified version must match the current version of the document for
the request to succeed.
:arg version_type: Specific version type: `external`,
`external_gte`. Valid choices are external, external_gte, force,
internal.
:arg wait_for_active_shards: The number of shard copies that
must be active before proceeding with the operation. Set to `all` or any
positive integer up to the total number of shards in the index
(`number_of_replicas+1`). Valid choices are all, index-setting.
"""
for param in (index, id, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
path = _make_path(index, "_create", id)
return self.transport.perform_request(
"PUT", path, params=params, headers=headers, body=body
)
@query_params(
"error_trace",
"filter_path",
"human",
"if_primary_term",
"if_seq_no",
"op_type",
"pipeline",
"pretty",
"refresh",
"require_alias",
"routing",
"source",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def index(
self,
index: Any,
body: Any,
id: Any = None,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Creates or updates a document in an index.
:arg index: Name of the data stream or index to target.
:arg body: The document
:arg id: Unique identifier for the document.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg if_primary_term: Only perform the operation if the document
has this primary term.
:arg if_seq_no: Only perform the operation if the document has
this sequence number.
:arg op_type: Set to create to only index the document if it
does not already exist (put if absent). If a document with the specified
`_id` already exists, the indexing operation will fail. Same as using
the `<index>/_create` endpoint. Valid values: `index`, `create`. If
document id is specified, it defaults to `index`. Otherwise, it defaults
to `create`.
:arg pipeline: ID of the pipeline to use to preprocess incoming
documents. If the index has a default ingest pipeline specified, then
setting the value to `_none` disables the default ingest pipeline for
this request. If a final pipeline is configured it will always run,
regardless of the value of this parameter.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg refresh: If `true`, OpenSearch refreshes the affected
shards to make this operation visible to search, if `wait_for` then wait
for a refresh to make this operation visible to search, if `false` do
nothing with refreshes. Valid values: `true`, `false`, `wait_for`.
:arg require_alias: If `true`, the destination must be an index
alias. Default is false.
:arg routing: Custom value used to route operations to a
specific shard.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg timeout: Period the request waits for the following
operations: automatic index creation, dynamic mapping updates, waiting
for active shards.
:arg version: Explicit version number for concurrency control.
The specified version must match the current version of the document for
the request to succeed.
:arg version_type: Specific version type: `external`,
`external_gte`. Valid choices are external, external_gte, force,
internal.
:arg wait_for_active_shards: The number of shard copies that
must be active before proceeding with the operation. Set to all or any
positive integer up to the total number of shards in the index
(`number_of_replicas+1`). Valid choices are all, index-setting.
"""
for param in (index, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
return self.transport.perform_request(
"POST" if id in SKIP_IN_PATH else "PUT",
_make_path(index, "_doc", id),
params=params,
headers=headers,
body=body,
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"error_trace",
"filter_path",
"human",
"pipeline",
"pretty",
"refresh",
"require_alias",
"routing",
"source",
"timeout",
"wait_for_active_shards",
)
def bulk(
self,
body: Any,
index: Any = None,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Allows to perform multiple index/update/delete operations in a single request.
:arg body: The operation definition and data (action-data
pairs), separated by newlines
:arg index: Name of the data stream, index, or index alias to
perform bulk actions on.
:arg _source: `true` or `false` to return the `_source` field or
not, or a list of fields to return.
:arg _source_excludes: A comma-separated list of source fields
to exclude from the response.
:arg _source_includes: A comma-separated list of source fields
to include in the response.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pipeline: ID of the pipeline to use to preprocess incoming
documents. If the index has a default ingest pipeline specified, then
setting the value to `_none` disables the default ingest pipeline for
this request. If a final pipeline is configured it will always run,
regardless of the value of this parameter.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg refresh: If `true`, OpenSearch refreshes the affected
shards to make this operation visible to search, if `wait_for` then wait
for a refresh to make this operation visible to search, if `false` do
nothing with refreshes. Valid values: `true`, `false`, `wait_for`.
:arg require_alias: If `true`, the request's actions must target
an index alias. Default is false.
:arg routing: Custom value used to route operations to a
specific shard.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg timeout: Period each action waits for the following
operations: automatic index creation, dynamic mapping updates, waiting
for active shards.
:arg wait_for_active_shards: The number of shard copies that
must be active before proceeding with the operation. Set to all or any
positive integer up to the total number of shards in the index
(`number_of_replicas+1`). Valid choices are all, index-setting.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")
body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request(
"POST",
_make_path(index, "_bulk"),
params=params,
headers=headers,
body=body,
)
@query_params("error_trace", "filter_path", "human", "pretty", "source")
def clear_scroll(
self,
body: Any = None,
scroll_id: Any = None,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Explicitly clears the search context for a scroll.
:arg body: Comma-separated list of scroll IDs to clear if none
was specified using the `scroll_id` parameter
:arg scroll_id: Comma-separated list of scroll IDs to clear. To
clear all scroll IDs, use `_all`.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
"""
if scroll_id in SKIP_IN_PATH and body in SKIP_IN_PATH:
raise ValueError("You need to supply scroll_id or body.")
elif scroll_id and not body:
body = {"scroll_id": [scroll_id]}
elif scroll_id:
params["scroll_id"] = scroll_id
return self.transport.perform_request(
"DELETE", "/_search/scroll", params=params, headers=headers, body=body
)
@query_params(
"allow_no_indices",
"analyze_wildcard",
"analyzer",
"default_operator",
"df",
"error_trace",
"expand_wildcards",
"filter_path",
"human",
"ignore_throttled",
"ignore_unavailable",
"lenient",
"min_score",
"preference",
"pretty",
"q",
"routing",
"source",
"terminate_after",
)
def count(
self,
body: Any = None,
index: Any = None,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Returns number of documents matching a query.
:arg body: Query to restrict the results specified with the
Query DSL (optional)
:arg index: Comma-separated list of data streams, indexes, and
aliases to search. Supports wildcards (`*`). To search all data streams
and indexes, omit this parameter or use `*` or `_all`.
:arg allow_no_indices: If `false`, the request returns an error
if any wildcard expression, index alias, or `_all` value targets only
missing or closed indexes. This behavior applies even if the request
targets other open indexes.
:arg analyze_wildcard: If `true`, wildcard and prefix queries
are analyzed. This parameter can only be used when the `q` query string
parameter is specified. Default is false.
:arg analyzer: Analyzer to use for the query string. This
parameter can only be used when the `q` query string parameter is
specified.
:arg default_operator: The default operator for query string
query: `AND` or `OR`. This parameter can only be used when the `q` query
string parameter is specified. Valid choices are and, or.
:arg df: Field to use as default where no field prefix is given
in the query string. This parameter can only be used when the `q` query
string parameter is specified.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg expand_wildcards: Type of index that wildcard patterns can
match. If the request can target data streams, this argument determines
whether wildcard expressions match hidden data streams. Supports comma-
separated values, such as `open,hidden`.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg ignore_throttled: If `true`, concrete, expanded or aliased
indexes are ignored when frozen.
:arg ignore_unavailable: If `false`, the request returns an
error if it targets a missing or closed index.
:arg lenient: If `true`, format-based query failures (such as
providing text to a numeric field) in the query string will be ignored.
:arg min_score: Sets the minimum `_score` value that documents
must have to be included in the result.
:arg preference: Specifies the node or shard the operation
should be performed on. Random by default. Default is random.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg q: Query in the Lucene query string syntax.
:arg routing: Custom value used to route operations to a
specific shard.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg terminate_after: Maximum number of documents to collect for
each shard. If a query reaches this limit, OpenSearch terminates the
query early. OpenSearch collects documents before sorting.
"""
return self.transport.perform_request(
"POST",
_make_path(index, "_count"),
params=params,
headers=headers,
body=body,
)
@query_params(
"error_trace",
"filter_path",
"human",
"if_primary_term",
"if_seq_no",
"pretty",
"refresh",
"routing",
"source",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def delete(
self,
index: Any,
id: Any,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Removes a document from the index.
:arg index: Name of the target index.
:arg id: Unique identifier for the document.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg if_primary_term: Only perform the operation if the document
has this primary term.
:arg if_seq_no: Only perform the operation if the document has
this sequence number.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg refresh: If `true`, OpenSearch refreshes the affected
shards to make this operation visible to search, if `wait_for` then wait
for a refresh to make this operation visible to search, if `false` do
nothing with refreshes. Valid values: `true`, `false`, `wait_for`.
:arg routing: Custom value used to route operations to a
specific shard.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg timeout: Period to wait for active shards.
:arg version: Explicit version number for concurrency control.
The specified version must match the current version of the document for
the request to succeed.
:arg version_type: Specific version type: `external`,
`external_gte`. Valid choices are external, external_gte, force,
internal.
:arg wait_for_active_shards: The number of shard copies that
must be active before proceeding with the operation. Set to `all` or any
positive integer up to the total number of shards in the index
(`number_of_replicas+1`). Valid choices are all, index-setting.
"""
for param in (index, id):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
return self.transport.perform_request(
"DELETE", _make_path(index, "_doc", id), params=params, headers=headers
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"allow_no_indices",
"analyze_wildcard",
"analyzer",
"conflicts",
"default_operator",
"df",
"error_trace",
"expand_wildcards",
"filter_path",
"from_",
"human",
"ignore_unavailable",
"lenient",
"max_docs",
"preference",
"pretty",
"q",
"refresh",
"request_cache",
"requests_per_second",
"routing",
"scroll",
"scroll_size",
"search_timeout",
"search_type",
"size",
"slices",
"sort",
"source",
"stats",
"terminate_after",
"timeout",
"version",
"wait_for_active_shards",
"wait_for_completion",
)
def delete_by_query(
self,
index: Any,
body: Any,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Deletes documents matching the provided query.
:arg index: Comma-separated list of data streams, indexes, and
aliases to search. Supports wildcards (`*`). To search all data streams
or indexes, omit this parameter or use `*` or `_all`.
:arg body: The search definition using the Query DSL
:arg _source: Set to `true` or `false` to return the `_source`
field or not, or a list of fields to return.
:arg _source_excludes: List of fields to exclude from the
returned `_source` field.
:arg _source_includes: List of fields to extract and return from
the `_source` field.
:arg allow_no_indices: If `false`, the request returns an error
if any wildcard expression, index alias, or `_all` value targets only
missing or closed indexes. This behavior applies even if the request
targets other open indexes. For example, a request targeting `foo*,bar*`
returns an error if an index starts with `foo` but no index starts with
`bar`.
:arg analyze_wildcard: If `true`, wildcard and prefix queries
are analyzed. Default is false.
:arg analyzer: Analyzer to use for the query string.
:arg conflicts: What to do if delete by query hits version
conflicts: `abort` or `proceed`. Valid choices are abort, proceed.
:arg default_operator: The default operator for query string
query: `AND` or `OR`. Valid choices are and, or.
:arg df: Field to use as default where no field prefix is given
in the query string.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg expand_wildcards: Type of index that wildcard patterns can
match. If the request can target data streams, this argument determines
whether wildcard expressions match hidden data streams. Supports comma-
separated values, such as `open,hidden`. Valid values are: `all`,
`open`, `closed`, `hidden`, `none`.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg from_: Starting offset. Default is 0.
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg ignore_unavailable: If `false`, the request returns an
error if it targets a missing or closed index.
:arg lenient: If `true`, format-based query failures (such as
providing text to a numeric field) in the query string will be ignored.
:arg max_docs: Maximum number of documents to process. Defaults
to all documents.
:arg preference: Specifies the node or shard the operation
should be performed on. Random by default. Default is random.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg q: Query in the Lucene query string syntax.
:arg refresh: If `true`, OpenSearch refreshes all shards
involved in the delete by query after the request completes.
:arg request_cache: If `true`, the request cache is used for
this request. Defaults to the index-level setting.
:arg requests_per_second: The throttle for this request in sub-
requests per second. Default is 0.
:arg routing: Custom value used to route operations to a
specific shard.
:arg scroll: Period to retain the search context for scrolling.
:arg scroll_size: Size of the scroll request that powers the
operation. Default is 100.
:arg search_timeout: Explicit timeout for each search request.
Defaults to no timeout.
:arg search_type: The type of the search operation. Available
options: `query_then_fetch`, `dfs_query_then_fetch`. Valid choices are
dfs_query_then_fetch, query_then_fetch.
:arg size: Deprecated, use `max_docs` instead.
:arg slices: The number of slices this task should be divided
into. Valid choices are auto.
:arg sort: A comma-separated list of <field>:<direction> pairs.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
:arg stats: Specific `tag` of the request for logging and
statistical purposes.
:arg terminate_after: Maximum number of documents to collect for
each shard. If a query reaches this limit, OpenSearch terminates the
query early. OpenSearch collects documents before sorting. Use with
caution. OpenSearch applies this parameter to each shard handling the
request. When possible, let OpenSearch perform early termination
automatically. Avoid specifying this parameter for requests that target
data streams with backing indexes across multiple data tiers.
:arg timeout: Period each deletion request waits for active
shards.
:arg version: If `true`, returns the document version as part of
a hit.
:arg wait_for_active_shards: The number of shard copies that
must be active before proceeding with the operation. Set to all or any
positive integer up to the total number of shards in the index
(`number_of_replicas+1`). Valid choices are all, index-setting.
:arg wait_for_completion: If `true`, the request blocks until
the operation is complete. Default is True.
"""
# from is a reserved word so it cannot be used, use from_ instead
if "from_" in params:
params["from"] = params.pop("from_")
for param in (index, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
return self.transport.perform_request(
"POST",
_make_path(index, "_delete_by_query"),
params=params,
headers=headers,
body=body,
)
@query_params(
"error_trace", "filter_path", "human", "pretty", "requests_per_second", "source"
)
def delete_by_query_rethrottle(
self,
task_id: Any,
params: Any = None,
headers: Any = None,
) -> Any:
"""
Changes the number of requests per second for a particular Delete By Query
operation.
:arg task_id: The ID for the task.
:arg error_trace: Whether to include the stack trace of returned
errors. Default is false.
:arg filter_path: Used to reduce the response. This parameter
takes a comma-separated list of filters. It supports using wildcards to
match any field or part of a field’s name. You can also exclude fields
with "-".
:arg human: Whether to return human readable values for
statistics. Default is True.
:arg pretty: Whether to pretty format the returned JSON
response. Default is false.
:arg requests_per_second: The throttle for this request in sub-
requests per second.
:arg source: The URL-encoded request definition. Useful for
libraries that do not accept a request body for non-POST requests.
"""
if task_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'task_id'.")
return self.transport.perform_request(
"POST",
_make_path("_delete_by_query", task_id, "_rethrottle"),
params=params,
headers=headers,
)
@query_params(
"cluster_manager_timeout",
"error_trace",
"filter_path",
"human",
"master_timeout",
"pretty",
"source",
"timeout",
)
def delete_script(
self,
id: Any,
params: Any = None,
headers: Any = None,
) -> Any:
"""