-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
__init__.py
2165 lines (1958 loc) · 90.4 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
# -*- coding: utf-8 -*-
# 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.
from __future__ import unicode_literals
import logging
from ..transport import Transport, TransportError
from .async_search import AsyncSearchClient
from .autoscaling import AutoscalingClient
from .cat import CatClient
from .ccr import CcrClient
from .cluster import ClusterClient
from .dangling_indices import DanglingIndicesClient
from .data_frame import Data_FrameClient
from .deprecation import DeprecationClient
from .enrich import EnrichClient
from .eql import EqlClient
from .features import FeaturesClient
from .fleet import FleetClient
from .graph import GraphClient
from .ilm import IlmClient
from .indices import IndicesClient
from .ingest import IngestClient
from .license import LicenseClient
from .logstash import LogstashClient
from .migration import MigrationClient
from .ml import MlClient
from .monitoring import MonitoringClient
from .nodes import NodesClient
from .remote import RemoteClient
from .rollup import RollupClient
from .searchable_snapshots import SearchableSnapshotsClient
from .security import SecurityClient
from .shutdown import ShutdownClient
from .slm import SlmClient
from .snapshot import SnapshotClient
from .sql import SqlClient
from .ssl import SslClient
from .tasks import TasksClient
from .text_structure import TextStructureClient
from .transform import TransformClient
from .utils import SKIP_IN_PATH, _bulk_body, _make_path, _normalize_hosts, query_params
from .watcher import WatcherClient
# xpack APIs
from .xpack import XPackClient
logger = logging.getLogger("elasticsearch")
class Elasticsearch(object):
"""
Elasticsearch low-level client. Provides a straightforward mapping from
Python to ES REST endpoints.
The instance has attributes ``cat``, ``cluster``, ``indices``, ``ingest``,
``nodes``, ``snapshot`` and ``tasks`` that provide access to instances of
:class:`~elasticsearch.client.CatClient`,
:class:`~elasticsearch.client.ClusterClient`,
:class:`~elasticsearch.client.IndicesClient`,
:class:`~elasticsearch.client.IngestClient`,
:class:`~elasticsearch.client.NodesClient`,
:class:`~elasticsearch.client.SnapshotClient` and
:class:`~elasticsearch.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
es = Elasticsearch(connection_class=ThriftConnection)
If you want to turn on :ref:`sniffing` you have several options (described
in :class:`~elasticsearch.Transport`)::
# create connection that will automatically inspect the cluster to get
# the list of active nodes. Start with nodes running on 'esnode1' and
# 'esnode2'
es = Elasticsearch(
['esnode1', 'esnode2'],
# 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.
es = Elasticsearch([
{'host': 'localhost'},
{'host': 'othernode', 'port': 443, 'url_prefix': 'es', 'use_ssl': True},
])
If using SSL, there are several parameters that control how we deal with
certificates (see :class:`~elasticsearch.Urllib3HttpConnection` for
detailed description of the options)::
es = Elasticsearch(
['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:`~elasticsearch.Urllib3HttpConnection` for
detailed description of the options)::
es = Elasticsearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# no verify SSL certificates
verify_certs=False,
# don't show warnings about ssl certs verification
ssl_show_warn=False
)
SSL client authentication is supported
(see :class:`~elasticsearch.Urllib3HttpConnection` for
detailed description of the options)::
es = Elasticsearch(
['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::
es = Elasticsearch(
[
'http://user:secret@localhost:9200/',
'https://user:secret@other_host:443/production'
],
verify_certs=True
)
By default, `JSONSerializer
<https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/serializer.py#L24>`_
is used to encode all outgoing requests.
However, you can implement your own custom serializer::
from elasticsearch.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)
es = Elasticsearch(serializer=SetEncoder())
"""
def __init__(self, hosts=None, transport_class=Transport, **kwargs):
"""
: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:`~elasticsearch.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:`~elasticsearch.Connection` class defaults will be used.
:arg transport_class: :class:`~elasticsearch.Transport` subclass to use.
:arg kwargs: any additional arguments will be passed on to the
:class:`~elasticsearch.Transport` class and, subsequently, to the
:class:`~elasticsearch.Connection` instances.
"""
self.transport = transport_class(_normalize_hosts(hosts), **kwargs)
# namespaced clients for compatibility with API names
self.async_search = AsyncSearchClient(self)
self.autoscaling = AutoscalingClient(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.snapshot = SnapshotClient(self)
self.tasks = TasksClient(self)
self.xpack = XPackClient(self)
self.ccr = CcrClient(self)
self.data_frame = Data_FrameClient(self)
self.deprecation = DeprecationClient(self)
self.enrich = EnrichClient(self)
self.eql = EqlClient(self)
self.features = FeaturesClient(self)
self.fleet = FleetClient(self)
self.graph = GraphClient(self)
self.ilm = IlmClient(self)
self.license = LicenseClient(self)
self.logstash = LogstashClient(self)
self.migration = MigrationClient(self)
self.ml = MlClient(self)
self.monitoring = MonitoringClient(self)
self.rollup = RollupClient(self)
self.searchable_snapshots = SearchableSnapshotsClient(self)
self.security = SecurityClient(self)
self.slm = SlmClient(self)
self.shutdown = ShutdownClient(self)
self.sql = SqlClient(self)
self.ssl = SslClient(self)
self.text_structure = TextStructureClient(self)
self.transform = TransformClient(self)
self.watcher = WatcherClient(self)
def __repr__(self):
try:
# get a list of all connections
cons = self.transport.hosts
# truncate to 5 if there are too many
if len(cons) > 5:
cons = cons[:5] + ["..."]
return "<{cls}({cons})>".format(cls=self.__class__.__name__, cons=cons)
except Exception:
# probably operating on custom transport and connection_pool, ignore
return super(Elasticsearch, self).__repr__()
def __enter__(self):
if hasattr(self.transport, "_async_call"):
self.transport._async_call()
return self
def __exit__(self, *_):
self.close()
def close(self):
"""Closes the Transport and all internal connections"""
self.transport.close()
# AUTO-GENERATED-API-DEFINITIONS #
@query_params()
def ping(self, params=None, headers=None):
"""
Returns whether the cluster is running.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/index.html>`_
"""
try:
return self.transport.perform_request(
"HEAD", "/", params=params, headers=headers
)
except TransportError:
return False
@query_params()
def info(self, params=None, headers=None):
"""
Returns basic information about the cluster.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/index.html>`_
"""
return self.transport.perform_request(
"GET", "/", params=params, headers=headers
)
@query_params(
"pipeline",
"refresh",
"routing",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def create(self, index, id, body, doc_type=None, params=None, headers=None):
"""
Creates a new document in the index. Returns a 409 response when a document
with a same ID already exists in the index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-index_.html>`_
:arg index: The name of the index
:arg id: Document ID
:arg body: The document
:arg doc_type: The type of the document
:arg pipeline: The pipeline id to preprocess incoming documents
with
:arg refresh: If `true` then refresh 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` (the default) then
do nothing with refreshes. Valid choices: true, false, wait_for
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type Valid choices:
internal, external, external_gte
:arg wait_for_active_shards: Sets the number of shard copies
that must be active before proceeding with the index operation. Defaults
to 1, meaning the primary shard only. Set to `all` for all shard copies,
otherwise set to any non-negative value less than or equal to the total
number of copies for the shard (number of replicas + 1)
"""
for param in (index, id, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type in SKIP_IN_PATH:
path = _make_path(index, "_create", id)
else:
path = _make_path(index, doc_type, id, "_create")
return self.transport.perform_request(
"PUT", path, params=params, headers=headers, body=body
)
@query_params(
"if_primary_term",
"if_seq_no",
"op_type",
"pipeline",
"refresh",
"require_alias",
"routing",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def index(self, index, body, doc_type=None, id=None, params=None, headers=None):
"""
Creates or updates a document in an index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-index_.html>`_
:arg index: The name of the index
:arg body: The document
:arg doc_type: The type of the document
:arg id: Document ID
:arg if_primary_term: only perform the index operation if the
last operation that has changed the document has the specified primary
term
:arg if_seq_no: only perform the index operation if the last
operation that has changed the document has the specified sequence
number
:arg op_type: Explicit operation type. Defaults to `index` for
requests with an explicit document ID, and to `create`for requests
without an explicit document ID Valid choices: index, create
:arg pipeline: The pipeline id to preprocess incoming documents
with
:arg refresh: If `true` then refresh 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` (the default) then
do nothing with refreshes. Valid choices: true, false, wait_for
:arg require_alias: When true, requires destination to be an
alias. Default is false
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type Valid choices:
internal, external, external_gte
:arg wait_for_active_shards: Sets the number of shard copies
that must be active before proceeding with the index operation. Defaults
to 1, meaning the primary shard only. Set to `all` for all shard copies,
otherwise set to any non-negative value less than or equal to the total
number of copies for the shard (number of replicas + 1)
"""
for param in (index, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type is None:
doc_type = "_doc"
return self.transport.perform_request(
"POST" if id in SKIP_IN_PATH else "PUT",
_make_path(index, doc_type, id),
params=params,
headers=headers,
body=body,
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"pipeline",
"refresh",
"require_alias",
"routing",
"timeout",
"wait_for_active_shards",
)
def bulk(self, body, index=None, doc_type=None, params=None, headers=None):
"""
Allows to perform multiple index/update/delete operations in a single request.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-bulk.html>`_
:arg body: The operation definition and data (action-data
pairs), separated by newlines
:arg index: Default index for items which don't provide one
:arg doc_type: Default document type for items which don't
provide one
:arg _source: True or false to return the _source field or not,
or default list of fields to return, can be overridden on each sub-
request
:arg _source_excludes: Default list of fields to exclude from
the returned _source field, can be overridden on each sub-request
:arg _source_includes: Default list of fields to extract and
return from the _source field, can be overridden on each sub-request
:arg pipeline: The pipeline id to preprocess incoming documents
with
:arg refresh: If `true` then refresh 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` (the default) then
do nothing with refreshes. Valid choices: true, false, wait_for
:arg require_alias: Sets require_alias for all incoming
documents. Defaults to unset (false)
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg wait_for_active_shards: Sets the number of shard copies
that must be active before proceeding with the bulk operation. Defaults
to 1, meaning the primary shard only. Set to `all` for all shard copies,
otherwise set to any non-negative value less than or equal to the total
number of copies for the shard (number of replicas + 1)
"""
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, doc_type, "_bulk"),
params=params,
headers=headers,
body=body,
)
@query_params()
def clear_scroll(self, body=None, scroll_id=None, params=None, headers=None):
"""
Explicitly clears the search context for a scroll.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/clear-scroll-api.html>`_
:arg body: A comma-separated list of scroll IDs to clear if none
was specified via the scroll_id parameter
:arg scroll_id: A comma-separated list of scroll IDs to clear
"""
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",
"expand_wildcards",
"ignore_throttled",
"ignore_unavailable",
"lenient",
"min_score",
"preference",
"q",
"routing",
"terminate_after",
)
def count(self, body=None, index=None, doc_type=None, params=None, headers=None):
"""
Returns number of documents matching a query.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-count.html>`_
:arg body: A query to restrict the results specified with the
Query DSL (optional)
:arg index: A comma-separated list of indices to restrict the
results
:arg doc_type: A comma-separated list of types to restrict the
results
:arg allow_no_indices: Whether to ignore if a wildcard indices
expression resolves into no concrete indices. (This includes `_all`
string or when no indices have been specified)
:arg analyze_wildcard: Specify whether wildcard and prefix
queries should be analyzed (default: false)
:arg analyzer: The analyzer to use for the query string
:arg default_operator: The default operator for query string
query (AND or OR) Valid choices: AND, OR Default: OR
:arg df: The field to use as default where no field prefix is
given in the query string
:arg expand_wildcards: Whether to expand wildcard expression to
concrete indices that are open, closed or both. Valid choices: open,
closed, hidden, none, all Default: open
:arg ignore_throttled: Whether specified concrete, expanded or
aliased indices should be ignored when throttled
:arg ignore_unavailable: Whether specified concrete indices
should be ignored when unavailable (missing or closed)
:arg lenient: Specify whether format-based query failures (such
as providing text to a numeric field) should be ignored
:arg min_score: Include only documents with a specific `_score`
value in the result
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg q: Query in the Lucene query string syntax
:arg routing: A comma-separated list of specific routing values
:arg terminate_after: The maximum count for each shard, upon
reaching which the query execution will terminate early
"""
return self.transport.perform_request(
"POST",
_make_path(index, doc_type, "_count"),
params=params,
headers=headers,
body=body,
)
@query_params(
"if_primary_term",
"if_seq_no",
"refresh",
"routing",
"timeout",
"version",
"version_type",
"wait_for_active_shards",
)
def delete(self, index, id, doc_type=None, params=None, headers=None):
"""
Removes a document from the index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-delete.html>`_
:arg index: The name of the index
:arg id: The document ID
:arg doc_type: The type of the document
:arg if_primary_term: only perform the delete operation if the
last operation that has changed the document has the specified primary
term
:arg if_seq_no: only perform the delete operation if the last
operation that has changed the document has the specified sequence
number
:arg refresh: If `true` then refresh 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` (the default) then
do nothing with refreshes. Valid choices: true, false, wait_for
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type Valid choices:
internal, external, external_gte, force
:arg wait_for_active_shards: Sets the number of shard copies
that must be active before proceeding with the delete operation.
Defaults to 1, meaning the primary shard only. Set to `all` for all
shard copies, otherwise set to any non-negative value less than or equal
to the total number of copies for the shard (number of replicas + 1)
"""
for param in (index, id):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type in SKIP_IN_PATH:
doc_type = "_doc"
return self.transport.perform_request(
"DELETE", _make_path(index, doc_type, id), params=params, headers=headers
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"allow_no_indices",
"analyze_wildcard",
"analyzer",
"conflicts",
"default_operator",
"df",
"expand_wildcards",
"from_",
"ignore_unavailable",
"lenient",
"max_docs",
"preference",
"q",
"refresh",
"request_cache",
"requests_per_second",
"routing",
"scroll",
"scroll_size",
"search_timeout",
"search_type",
"size",
"slices",
"sort",
"stats",
"terminate_after",
"timeout",
"version",
"wait_for_active_shards",
"wait_for_completion",
)
def delete_by_query(self, index, body, doc_type=None, params=None, headers=None):
"""
Deletes documents matching the provided query.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-delete-by-query.html>`_
:arg index: A comma-separated list of index names to search; use
`_all` or empty string to perform the operation on all indices
:arg body: The search definition using the Query DSL
:arg doc_type: A comma-separated list of document types to
search; leave empty to perform the operation on all types
:arg _source: True or false to return the _source field or not,
or a list of fields to return
:arg _source_excludes: A list of fields to exclude from the
returned _source field
:arg _source_includes: A list of fields to extract and return
from the _source field
:arg allow_no_indices: Whether to ignore if a wildcard indices
expression resolves into no concrete indices. (This includes `_all`
string or when no indices have been specified)
:arg analyze_wildcard: Specify whether wildcard and prefix
queries should be analyzed (default: false)
:arg analyzer: The analyzer to use for the query string
:arg conflicts: What to do when the delete by query hits version
conflicts? Valid choices: abort, proceed Default: abort
:arg default_operator: The default operator for query string
query (AND or OR) Valid choices: AND, OR Default: OR
:arg df: The field to use as default where no field prefix is
given in the query string
:arg expand_wildcards: Whether to expand wildcard expression to
concrete indices that are open, closed or both. Valid choices: open,
closed, hidden, none, all Default: open
:arg from_: Starting offset (default: 0)
:arg ignore_unavailable: Whether specified concrete indices
should be ignored when unavailable (missing or closed)
:arg lenient: Specify whether format-based query failures (such
as providing text to a numeric field) should be ignored
:arg max_docs: Maximum number of documents to process (default:
all documents)
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg q: Query in the Lucene query string syntax
:arg refresh: Should the effected indexes be refreshed?
:arg request_cache: Specify if request cache should be used for
this request or not, defaults to index level setting
:arg requests_per_second: The throttle for this request in sub-
requests per second. -1 means no throttle.
:arg routing: A comma-separated list of specific routing values
:arg scroll: Specify how long a consistent view of the index
should be maintained for scrolled search
:arg scroll_size: Size on the scroll request powering the delete
by query Default: 100
:arg search_timeout: Explicit timeout for each search request.
Defaults to no timeout.
:arg search_type: Search operation type Valid choices:
query_then_fetch, dfs_query_then_fetch
:arg size: Deprecated, please use `max_docs` instead
:arg slices: The number of slices this task should be divided
into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be
set to `auto`. Default: 1
:arg sort: A comma-separated list of <field>:<direction> pairs
:arg stats: Specific 'tag' of the request for logging and
statistical purposes
:arg terminate_after: The maximum number of documents to collect
for each shard, upon reaching which the query execution will terminate
early.
:arg timeout: Time each individual bulk request should wait for
shards that are unavailable. Default: 1m
:arg version: Specify whether to return document version as part
of a hit
:arg wait_for_active_shards: Sets the number of shard copies
that must be active before proceeding with the delete by query
operation. Defaults to 1, meaning the primary shard only. Set to `all`
for all shard copies, otherwise set to any non-negative value less than
or equal to the total number of copies for the shard (number of replicas
+ 1)
:arg wait_for_completion: Should the request should block until
the delete by query is complete. Default: 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, doc_type, "_delete_by_query"),
params=params,
headers=headers,
body=body,
)
@query_params("requests_per_second")
def delete_by_query_rethrottle(self, task_id, params=None, headers=None):
"""
Changes the number of requests per second for a particular Delete By Query
operation.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-delete-by-query.html>`_
:arg task_id: The task id to rethrottle
:arg requests_per_second: The throttle to set on this request in
floating sub-requests per second. -1 means set no throttle.
"""
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("master_timeout", "timeout")
def delete_script(self, id, params=None, headers=None):
"""
Deletes a script.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/modules-scripting.html>`_
:arg id: Script ID
:arg master_timeout: Specify timeout for connection to master
:arg timeout: Explicit operation timeout
"""
if id in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'id'.")
return self.transport.perform_request(
"DELETE", _make_path("_scripts", id), params=params, headers=headers
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"preference",
"realtime",
"refresh",
"routing",
"stored_fields",
"version",
"version_type",
)
def exists(self, index, id, doc_type=None, params=None, headers=None):
"""
Returns information about whether a document exists in an index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-get.html>`_
:arg index: The name of the index
:arg id: The document ID
:arg doc_type: The type of the document (use `_all` to fetch the
first document matching the ID across all types)
:arg _source: True or false to return the _source field or not,
or a list of fields to return
:arg _source_excludes: A list of fields to exclude from the
returned _source field
:arg _source_includes: A list of fields to extract and return
from the _source field
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg realtime: Specify whether to perform the operation in
realtime or search mode
:arg refresh: Refresh the shard containing the document before
performing the operation
:arg routing: Specific routing value
:arg stored_fields: A comma-separated list of stored fields to
return in the response
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type Valid choices:
internal, external, external_gte, force
"""
for param in (index, id):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type in SKIP_IN_PATH:
doc_type = "_doc"
return self.transport.perform_request(
"HEAD", _make_path(index, doc_type, id), params=params, headers=headers
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"preference",
"realtime",
"refresh",
"routing",
"version",
"version_type",
)
def exists_source(self, index, id, doc_type=None, params=None, headers=None):
"""
Returns information about whether a document source exists in an index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-get.html>`_
:arg index: The name of the index
:arg id: The document ID
:arg doc_type: The type of the document; deprecated and optional
starting with 7.0
:arg _source: True or false to return the _source field or not,
or a list of fields to return
:arg _source_excludes: A list of fields to exclude from the
returned _source field
:arg _source_includes: A list of fields to extract and return
from the _source field
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg realtime: Specify whether to perform the operation in
realtime or search mode
:arg refresh: Refresh the shard containing the document before
performing the operation
:arg routing: Specific routing value
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type Valid choices:
internal, external, external_gte, force
"""
for param in (index, id):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type in SKIP_IN_PATH:
path = _make_path(index, "_source", id)
else:
path = _make_path(index, doc_type, id, "_source")
return self.transport.perform_request(
"HEAD", path, params=params, headers=headers
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"analyze_wildcard",
"analyzer",
"default_operator",
"df",
"lenient",
"preference",
"q",
"routing",
"stored_fields",
)
def explain(self, index, id, body=None, doc_type=None, params=None, headers=None):
"""
Returns information about why a specific matches (or doesn't match) a query.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-explain.html>`_
:arg index: The name of the index
:arg id: The document ID
:arg body: The query definition using the Query DSL
:arg doc_type: The type of the document
:arg _source: True or false to return the _source field or not,
or a list of fields to return
:arg _source_excludes: A list of fields to exclude from the
returned _source field
:arg _source_includes: A list of fields to extract and return
from the _source field
:arg analyze_wildcard: Specify whether wildcards and prefix
queries in the query string query should be analyzed (default: false)
:arg analyzer: The analyzer for the query string query
:arg default_operator: The default operator for query string
query (AND or OR) Valid choices: AND, OR Default: OR
:arg df: The default field for query string query (default:
_all)
:arg lenient: Specify whether format-based query failures (such
as providing text to a numeric field) should be ignored
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg q: Query in the Lucene query string syntax
:arg routing: Specific routing value
:arg stored_fields: A comma-separated list of stored fields to
return in the response
"""
for param in (index, id):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
if doc_type in SKIP_IN_PATH:
path = _make_path(index, "_explain", id)
else:
path = _make_path(index, doc_type, id, "_explain")
return self.transport.perform_request(
"POST", path, params=params, headers=headers, body=body
)
@query_params(
"allow_no_indices",
"expand_wildcards",
"fields",
"ignore_unavailable",
"include_unmapped",
)
def field_caps(self, body=None, index=None, params=None, headers=None):
"""
Returns the information about the capabilities of fields among multiple
indices.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-field-caps.html>`_
:arg body: An index filter specified with the Query DSL
:arg index: A comma-separated list of index names; use `_all` or
empty string to perform the operation on all indices
:arg allow_no_indices: Whether to ignore if a wildcard indices
expression resolves into no concrete indices. (This includes `_all`
string or when no indices have been specified)
:arg expand_wildcards: Whether to expand wildcard expression to
concrete indices that are open, closed or both. Valid choices: open,
closed, hidden, none, all Default: open
:arg fields: A comma-separated list of field names
:arg ignore_unavailable: Whether specified concrete indices
should be ignored when unavailable (missing or closed)
:arg include_unmapped: Indicates whether unmapped fields should
be included in the response.
"""
return self.transport.perform_request(
"POST",
_make_path(index, "_field_caps"),
params=params,
headers=headers,
body=body,
)
@query_params(
"_source",
"_source_excludes",
"_source_includes",
"preference",
"realtime",
"refresh",
"routing",
"stored_fields",
"version",
"version_type",
)
def get(self, index, id, doc_type=None, params=None, headers=None):
"""
Returns a document.
`<https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-get.html>`_
:arg index: The name of the index
:arg id: The document ID
:arg doc_type: The type of the document (use `_all` to fetch the
first document matching the ID across all types)
:arg _source: True or false to return the _source field or not,
or a list of fields to return