0
# Client Operations
1
2
Core document and search operations that form the foundation of Elasticsearch interactions. These operations handle the primary CRUD (Create, Read, Update, Delete) operations and basic search functionality.
3
4
## Capabilities
5
6
### Document Indexing
7
8
Index documents into Elasticsearch with automatic or explicit ID assignment, routing, and refresh control.
9
10
```python { .api }
11
def index(
12
self,
13
index: str,
14
document: Dict[str, Any],
15
id: Optional[str] = None,
16
op_type: Optional[str] = None,
17
routing: Optional[str] = None,
18
refresh: Optional[str] = None,
19
timeout: Optional[str] = None,
20
version: Optional[int] = None,
21
version_type: Optional[str] = None,
22
if_seq_no: Optional[int] = None,
23
if_primary_term: Optional[int] = None,
24
pipeline: Optional[str] = None,
25
require_alias: Optional[bool] = None,
26
**kwargs
27
) -> ObjectApiResponse:
28
"""
29
Index a document.
30
31
Parameters:
32
- index: Target index name
33
- document: Document to index
34
- id: Document ID (auto-generated if not provided)
35
- op_type: Operation type ('index' or 'create')
36
- routing: Custom routing value
37
- refresh: Refresh policy ('true', 'false', 'wait_for')
38
- timeout: Request timeout
39
- version: Document version for optimistic concurrency
40
- version_type: Version type ('internal', 'external', 'external_gte')
41
- if_seq_no: Sequence number for optimistic concurrency
42
- if_primary_term: Primary term for optimistic concurrency
43
- pipeline: Ingest pipeline to use
44
- require_alias: Require index to be an alias
45
46
Returns:
47
ObjectApiResponse with result metadata
48
"""
49
```
50
51
### Document Retrieval
52
53
Retrieve documents by ID with support for routing, version checking, and field filtering.
54
55
```python { .api }
56
def get(
57
self,
58
index: str,
59
id: str,
60
routing: Optional[str] = None,
61
preference: Optional[str] = None,
62
realtime: Optional[bool] = None,
63
refresh: Optional[bool] = None,
64
stored_fields: Optional[List[str]] = None,
65
_source: Optional[Union[bool, List[str]]] = None,
66
_source_excludes: Optional[List[str]] = None,
67
_source_includes: Optional[List[str]] = None,
68
version: Optional[int] = None,
69
version_type: Optional[str] = None,
70
**kwargs
71
) -> ObjectApiResponse:
72
"""
73
Get a document by ID.
74
75
Parameters:
76
- index: Index name
77
- id: Document ID
78
- routing: Custom routing value
79
- preference: Node preference for request routing
80
- realtime: Whether to retrieve from real-time refresh
81
- refresh: Whether to refresh before retrieval
82
- stored_fields: Specific stored fields to retrieve
83
- _source: Whether to return source and which fields
84
- _source_excludes: Source fields to exclude
85
- _source_includes: Source fields to include
86
- version: Expected document version
87
- version_type: Version type for comparison
88
89
Returns:
90
ObjectApiResponse with document data
91
"""
92
```
93
94
### Document Updates
95
96
Update documents using partial document updates or scripts with upsert support.
97
98
```python { .api }
99
def update(
100
self,
101
index: str,
102
id: str,
103
document: Optional[Dict[str, Any]] = None,
104
script: Optional[Dict[str, Any]] = None,
105
upsert: Optional[Dict[str, Any]] = None,
106
routing: Optional[str] = None,
107
refresh: Optional[str] = None,
108
retry_on_conflict: Optional[int] = None,
109
timeout: Optional[str] = None,
110
_source: Optional[Union[bool, List[str]]] = None,
111
_source_excludes: Optional[List[str]] = None,
112
_source_includes: Optional[List[str]] = None,
113
version: Optional[int] = None,
114
version_type: Optional[str] = None,
115
if_seq_no: Optional[int] = None,
116
if_primary_term: Optional[int] = None,
117
**kwargs
118
) -> ObjectApiResponse:
119
"""
120
Update a document.
121
122
Parameters:
123
- index: Index name
124
- id: Document ID
125
- document: Partial document for update
126
- script: Script for programmatic updates
127
- upsert: Document to create if it doesn't exist
128
- routing: Custom routing value
129
- refresh: Refresh policy
130
- retry_on_conflict: Number of retries on version conflicts
131
- timeout: Request timeout
132
- _source: Whether to return updated source
133
- _source_excludes: Source fields to exclude from response
134
- _source_includes: Source fields to include in response
135
- version: Expected current version
136
- version_type: Version type for comparison
137
- if_seq_no: Expected sequence number
138
- if_primary_term: Expected primary term
139
140
Returns:
141
ObjectApiResponse with update result
142
"""
143
```
144
145
### Document Deletion
146
147
Delete documents by ID with support for routing and version checking.
148
149
```python { .api }
150
def delete(
151
self,
152
index: str,
153
id: str,
154
routing: Optional[str] = None,
155
refresh: Optional[str] = None,
156
timeout: Optional[str] = None,
157
version: Optional[int] = None,
158
version_type: Optional[str] = None,
159
if_seq_no: Optional[int] = None,
160
if_primary_term: Optional[int] = None,
161
**kwargs
162
) -> ObjectApiResponse:
163
"""
164
Delete a document by ID.
165
166
Parameters:
167
- index: Index name
168
- id: Document ID
169
- routing: Custom routing value
170
- refresh: Refresh policy
171
- timeout: Request timeout
172
- version: Expected document version
173
- version_type: Version type for comparison
174
- if_seq_no: Expected sequence number
175
- if_primary_term: Expected primary term
176
177
Returns:
178
ObjectApiResponse with deletion result
179
"""
180
```
181
182
### Bulk Operations
183
184
Execute multiple operations in a single request for efficient batch processing.
185
186
```python { .api }
187
def bulk(
188
self,
189
operations: List[Dict[str, Any]],
190
index: Optional[str] = None,
191
routing: Optional[str] = None,
192
refresh: Optional[str] = None,
193
timeout: Optional[str] = None,
194
pipeline: Optional[str] = None,
195
require_alias: Optional[bool] = None,
196
**kwargs
197
) -> ObjectApiResponse:
198
"""
199
Perform multiple operations in a single request.
200
201
Parameters:
202
- operations: List of operation dictionaries
203
- index: Default index for operations without explicit index
204
- routing: Default routing value
205
- refresh: Refresh policy
206
- timeout: Request timeout
207
- pipeline: Default ingest pipeline
208
- require_alias: Require indices to be aliases
209
210
Returns:
211
ObjectApiResponse with results for each operation
212
"""
213
```
214
215
### Multi-Get Operations
216
217
Retrieve multiple documents in a single request.
218
219
```python { .api }
220
def mget(
221
self,
222
docs: List[Dict[str, Any]],
223
index: Optional[str] = None,
224
routing: Optional[str] = None,
225
preference: Optional[str] = None,
226
realtime: Optional[bool] = None,
227
refresh: Optional[bool] = None,
228
stored_fields: Optional[List[str]] = None,
229
_source: Optional[Union[bool, List[str]]] = None,
230
_source_excludes: Optional[List[str]] = None,
231
_source_includes: Optional[List[str]] = None,
232
**kwargs
233
) -> ObjectApiResponse:
234
"""
235
Get multiple documents.
236
237
Parameters:
238
- docs: List of document specifiers with _index, _id, and other parameters
239
- index: Default index name
240
- routing: Default routing value
241
- preference: Node preference
242
- realtime: Whether to use real-time refresh
243
- refresh: Whether to refresh before retrieval
244
- stored_fields: Stored fields to retrieve
245
- _source: Source field configuration
246
- _source_excludes: Source fields to exclude
247
- _source_includes: Source fields to include
248
249
Returns:
250
ObjectApiResponse with array of document results
251
"""
252
```
253
254
### Document Existence Checks
255
256
Check if documents exist without retrieving their content.
257
258
```python { .api }
259
def exists(
260
self,
261
index: str,
262
id: str,
263
routing: Optional[str] = None,
264
preference: Optional[str] = None,
265
realtime: Optional[bool] = None,
266
refresh: Optional[bool] = None,
267
version: Optional[int] = None,
268
version_type: Optional[str] = None,
269
**kwargs
270
) -> bool:
271
"""
272
Check if a document exists.
273
274
Parameters:
275
- index: Index name
276
- id: Document ID
277
- routing: Custom routing value
278
- preference: Node preference
279
- realtime: Whether to use real-time refresh
280
- refresh: Whether to refresh before check
281
- version: Expected document version
282
- version_type: Version type for comparison
283
284
Returns:
285
True if document exists, False otherwise
286
"""
287
```
288
289
### Basic Search
290
291
Execute search queries against one or more indices.
292
293
```python { .api }
294
def search(
295
self,
296
index: Optional[Union[str, List[str]]] = None,
297
query: Optional[Dict[str, Any]] = None,
298
aggs: Optional[Dict[str, Any]] = None,
299
sort: Optional[List[Dict[str, Any]]] = None,
300
_source: Optional[Union[bool, List[str]]] = None,
301
_source_excludes: Optional[List[str]] = None,
302
_source_includes: Optional[List[str]] = None,
303
size: Optional[int] = None,
304
from_: Optional[int] = None,
305
timeout: Optional[str] = None,
306
terminate_after: Optional[int] = None,
307
routing: Optional[str] = None,
308
preference: Optional[str] = None,
309
**kwargs
310
) -> ObjectApiResponse:
311
"""
312
Execute a search query.
313
314
Parameters:
315
- index: Index name(s) to search
316
- query: Query DSL query
317
- aggs: Aggregations to compute
318
- sort: Sort configuration
319
- _source: Source field configuration
320
- _source_excludes: Source fields to exclude
321
- _source_includes: Source fields to include
322
- size: Number of results to return
323
- from_: Starting offset for results
324
- timeout: Search timeout
325
- terminate_after: Terminate after collecting this many documents
326
- routing: Custom routing value
327
- preference: Node preference
328
329
Returns:
330
ObjectApiResponse with search results
331
"""
332
```
333
334
### Connection Management
335
336
```python { .api }
337
def ping(self, **kwargs) -> bool:
338
"""
339
Check cluster connectivity.
340
341
Returns:
342
True if cluster is reachable, False otherwise
343
"""
344
345
def info(self, **kwargs) -> ObjectApiResponse:
346
"""
347
Get basic cluster information.
348
349
Returns:
350
ObjectApiResponse with cluster info including version and build details
351
"""
352
353
def close(self) -> None:
354
"""
355
Close the client and cleanup resources.
356
"""
357
```
358
359
## Usage Examples
360
361
### Basic Document Operations
362
363
```python
364
from elasticsearch import Elasticsearch
365
366
client = Elasticsearch(hosts=['http://localhost:9200'])
367
368
# Index a document
369
response = client.index(
370
index="products",
371
id="1",
372
document={
373
"name": "Laptop",
374
"price": 999.99,
375
"category": "Electronics"
376
},
377
refresh='wait_for'
378
)
379
380
# Get the document
381
doc = client.get(index="products", id="1")
382
print(doc.body['_source'])
383
384
# Update the document
385
client.update(
386
index="products",
387
id="1",
388
document={"price": 899.99},
389
refresh='wait_for'
390
)
391
392
# Delete the document
393
client.delete(index="products", id="1", refresh='wait_for')
394
```
395
396
### Bulk Operations
397
398
```python
399
# Bulk indexing
400
operations = [
401
{"index": {"_index": "products", "_id": "1"}},
402
{"name": "Laptop", "price": 999.99},
403
{"index": {"_index": "products", "_id": "2"}},
404
{"name": "Mouse", "price": 29.99},
405
{"update": {"_index": "products", "_id": "1"}},
406
{"doc": {"price": 899.99}}
407
]
408
409
response = client.bulk(operations=operations, refresh='wait_for')
410
411
# Check for errors
412
if response.body['errors']:
413
for item in response.body['items']:
414
if 'error' in item.get('index', {}):
415
print(f"Error: {item['index']['error']}")
416
```
417
418
### Document Creation
419
420
Create documents with explicit ID assignment, ensuring they don't already exist.
421
422
```python { .api }
423
def create(
424
self,
425
index: str,
426
id: str,
427
document: Dict[str, Any],
428
routing: Optional[str] = None,
429
refresh: Optional[str] = None,
430
timeout: Optional[str] = None,
431
version: Optional[int] = None,
432
version_type: Optional[str] = None,
433
pipeline: Optional[str] = None,
434
**kwargs
435
) -> ObjectApiResponse:
436
"""
437
Create a document with explicit ID, failing if it already exists.
438
439
Parameters:
440
- index: Target index name
441
- id: Document ID (must not exist)
442
- document: Document to create
443
- routing: Custom routing value
444
- refresh: Refresh policy ('true', 'false', 'wait_for')
445
- timeout: Request timeout
446
- version: Expected version (must be 1 for creation)
447
- version_type: Version type ('internal', 'external')
448
- pipeline: Ingest pipeline to use
449
450
Returns:
451
ObjectApiResponse with creation result
452
"""
453
```
454
455
### Source-Only Operations
456
457
Retrieve or check document source without metadata overhead.
458
459
```python { .api }
460
def get_source(
461
self,
462
index: str,
463
id: str,
464
routing: Optional[str] = None,
465
preference: Optional[str] = None,
466
realtime: Optional[bool] = None,
467
refresh: Optional[bool] = None,
468
_source_excludes: Optional[List[str]] = None,
469
_source_includes: Optional[List[str]] = None,
470
version: Optional[int] = None,
471
version_type: Optional[str] = None,
472
**kwargs
473
) -> ObjectApiResponse:
474
"""
475
Get only the source of a document without metadata.
476
477
Parameters:
478
- index: Index name
479
- id: Document ID
480
- routing: Custom routing value
481
- preference: Node preference for request routing
482
- realtime: Whether to retrieve from real-time refresh
483
- refresh: Whether to refresh before retrieval
484
- _source_excludes: Source fields to exclude
485
- _source_includes: Source fields to include
486
- version: Expected document version
487
- version_type: Version type for comparison
488
489
Returns:
490
ObjectApiResponse with only document source
491
"""
492
493
def exists_source(
494
self,
495
index: str,
496
id: str,
497
routing: Optional[str] = None,
498
preference: Optional[str] = None,
499
realtime: Optional[bool] = None,
500
refresh: Optional[bool] = None,
501
_source_excludes: Optional[List[str]] = None,
502
_source_includes: Optional[List[str]] = None,
503
version: Optional[int] = None,
504
version_type: Optional[str] = None,
505
**kwargs
506
) -> HeadApiResponse:
507
"""
508
Check if document source exists.
509
510
Parameters:
511
- index: Index name
512
- id: Document ID
513
- routing: Custom routing value
514
- preference: Node preference
515
- realtime: Whether to check real-time refresh
516
- refresh: Whether to refresh before check
517
- _source_excludes: Source fields to exclude from check
518
- _source_includes: Source fields to include in check
519
- version: Expected document version
520
- version_type: Version type for comparison
521
522
Returns:
523
HeadApiResponse (status 200 if exists, 404 if not)
524
"""
525
```
526
527
### Advanced Search Operations
528
529
Extended search capabilities including multi-search, templates, and explanations.
530
531
```python { .api }
532
def msearch(
533
self,
534
searches: List[Dict[str, Any]],
535
index: Optional[str] = None,
536
allow_no_indices: Optional[bool] = None,
537
expand_wildcards: Optional[str] = None,
538
ignore_unavailable: Optional[bool] = None,
539
max_concurrent_searches: Optional[int] = None,
540
rest_total_hits_as_int: Optional[bool] = None,
541
typed_keys: Optional[bool] = None,
542
**kwargs
543
) -> ObjectApiResponse:
544
"""
545
Execute multiple search requests in a single API call.
546
547
Parameters:
548
- searches: List of search request objects
549
- index: Default index for searches without explicit index
550
- allow_no_indices: Whether to allow searches with no matching indices
551
- expand_wildcards: How to expand wildcard patterns
552
- ignore_unavailable: Whether to ignore unavailable indices
553
- max_concurrent_searches: Maximum concurrent searches
554
- rest_total_hits_as_int: Return total hits as integer
555
- typed_keys: Return typed keys in response
556
557
Returns:
558
ObjectApiResponse with responses for each search
559
"""
560
561
def search_template(
562
self,
563
index: Optional[str] = None,
564
id: Optional[str] = None,
565
params: Optional[Dict[str, Any]] = None,
566
source: Optional[str] = None,
567
allow_no_indices: Optional[bool] = None,
568
expand_wildcards: Optional[str] = None,
569
ignore_unavailable: Optional[bool] = None,
570
**kwargs
571
) -> ObjectApiResponse:
572
"""
573
Execute a search using a stored or inline template.
574
575
Parameters:
576
- index: Index name(s) to search
577
- id: ID of stored search template
578
- params: Template parameters
579
- source: Inline template source
580
- allow_no_indices: Whether to allow no matching indices
581
- expand_wildcards: How to expand wildcards
582
- ignore_unavailable: Whether to ignore unavailable indices
583
584
Returns:
585
ObjectApiResponse with search results
586
"""
587
588
def msearch_template(
589
self,
590
search_templates: List[Dict[str, Any]],
591
index: Optional[str] = None,
592
**kwargs
593
) -> ObjectApiResponse:
594
"""
595
Execute multiple search template requests.
596
597
Parameters:
598
- search_templates: List of search template requests
599
- index: Default index name
600
601
Returns:
602
ObjectApiResponse with results for each template search
603
"""
604
605
def explain(
606
self,
607
index: str,
608
id: str,
609
query: Optional[Dict[str, Any]] = None,
610
analyzer: Optional[str] = None,
611
analyze_wildcard: Optional[bool] = None,
612
default_operator: Optional[str] = None,
613
df: Optional[str] = None,
614
lenient: Optional[bool] = None,
615
preference: Optional[str] = None,
616
routing: Optional[str] = None,
617
**kwargs
618
) -> ObjectApiResponse:
619
"""
620
Explain why a document matches or doesn't match a query.
621
622
Parameters:
623
- index: Index name
624
- id: Document ID to explain
625
- query: Query to explain against
626
- analyzer: Analyzer for query analysis
627
- analyze_wildcard: Whether to analyze wildcard queries
628
- default_operator: Default operator for query parsing
629
- df: Default field for query parsing
630
- lenient: Whether to ignore format errors
631
- preference: Node preference
632
- routing: Custom routing
633
634
Returns:
635
ObjectApiResponse with explanation details
636
"""
637
```
638
639
### Scroll Operations
640
641
Manage scroll contexts for paginating through large result sets efficiently.
642
643
```python { .api }
644
def scroll(
645
self,
646
scroll_id: str,
647
scroll: Optional[str] = "5m",
648
rest_total_hits_as_int: Optional[bool] = None,
649
**kwargs
650
) -> ObjectApiResponse:
651
"""
652
Continue scrolling through search results.
653
654
Parameters:
655
- scroll_id: Scroll ID from previous search/scroll request
656
- scroll: How long to keep scroll context alive
657
- rest_total_hits_as_int: Return total hits as integer
658
659
Returns:
660
ObjectApiResponse with next batch of results
661
"""
662
663
def clear_scroll(
664
self,
665
scroll_id: Optional[Union[str, List[str]]] = None,
666
**kwargs
667
) -> ObjectApiResponse:
668
"""
669
Clear scroll contexts to free resources.
670
671
Parameters:
672
- scroll_id: Scroll ID(s) to clear (clears all if not provided)
673
674
Returns:
675
ObjectApiResponse confirming cleared contexts
676
"""
677
```
678
679
### Point in Time Operations
680
681
Manage point-in-time contexts for consistent pagination across index changes.
682
683
```python { .api }
684
def open_point_in_time(
685
self,
686
index: str,
687
keep_alive: str,
688
routing: Optional[str] = None,
689
preference: Optional[str] = None,
690
expand_wildcards: Optional[str] = None,
691
ignore_unavailable: Optional[bool] = None,
692
**kwargs
693
) -> ObjectApiResponse:
694
"""
695
Open a point in time for consistent search across index changes.
696
697
Parameters:
698
- index: Index name(s) for point in time
699
- keep_alive: How long to keep point in time alive
700
- routing: Custom routing
701
- preference: Node preference
702
- expand_wildcards: How to expand wildcards
703
- ignore_unavailable: Whether to ignore unavailable indices
704
705
Returns:
706
ObjectApiResponse with point in time ID
707
"""
708
709
def close_point_in_time(
710
self,
711
id: str,
712
**kwargs
713
) -> ObjectApiResponse:
714
"""
715
Close a point in time to free resources.
716
717
Parameters:
718
- id: Point in time ID to close
719
720
Returns:
721
ObjectApiResponse confirming closure
722
"""
723
```
724
725
### Term Vector Operations
726
727
Analyze term vectors for document fields to understand indexing and scoring.
728
729
```python { .api }
730
def termvectors(
731
self,
732
index: str,
733
id: Optional[str] = None,
734
doc: Optional[Dict[str, Any]] = None,
735
fields: Optional[List[str]] = None,
736
field_statistics: Optional[bool] = None,
737
offsets: Optional[bool] = None,
738
payloads: Optional[bool] = None,
739
positions: Optional[bool] = None,
740
preference: Optional[str] = None,
741
realtime: Optional[bool] = None,
742
routing: Optional[str] = None,
743
term_statistics: Optional[bool] = None,
744
version: Optional[int] = None,
745
version_type: Optional[str] = None,
746
**kwargs
747
) -> ObjectApiResponse:
748
"""
749
Get term vectors for document fields.
750
751
Parameters:
752
- index: Index name
753
- id: Document ID (or provide doc)
754
- doc: Document to analyze (if no ID)
755
- fields: Fields to analyze
756
- field_statistics: Include field statistics
757
- offsets: Include term offsets
758
- payloads: Include term payloads
759
- positions: Include term positions
760
- preference: Node preference
761
- realtime: Use real-time refresh
762
- routing: Custom routing
763
- term_statistics: Include term statistics
764
- version: Document version
765
- version_type: Version type
766
767
Returns:
768
ObjectApiResponse with term vector analysis
769
"""
770
771
def mtermvectors(
772
self,
773
docs: Optional[List[Dict[str, Any]]] = None,
774
ids: Optional[List[str]] = None,
775
index: Optional[str] = None,
776
field_statistics: Optional[bool] = None,
777
fields: Optional[List[str]] = None,
778
offsets: Optional[bool] = None,
779
payloads: Optional[bool] = None,
780
positions: Optional[bool] = None,
781
preference: Optional[str] = None,
782
realtime: Optional[bool] = None,
783
routing: Optional[str] = None,
784
term_statistics: Optional[bool] = None,
785
**kwargs
786
) -> ObjectApiResponse:
787
"""
788
Get term vectors for multiple documents.
789
790
Parameters:
791
- docs: List of documents with their indices and IDs
792
- ids: List of document IDs (requires index parameter)
793
- index: Default index for IDs list
794
- field_statistics: Include field statistics
795
- fields: Fields to analyze
796
- offsets: Include term offsets
797
- payloads: Include term payloads
798
- positions: Include term positions
799
- preference: Node preference
800
- realtime: Use real-time refresh
801
- routing: Custom routing
802
- term_statistics: Include term statistics
803
804
Returns:
805
ObjectApiResponse with term vectors for each document
806
"""
807
```
808
809
### Query Operations
810
811
Execute queries that modify documents based on query criteria.
812
813
```python { .api }
814
def delete_by_query(
815
self,
816
index: str,
817
query: Optional[Dict[str, Any]] = None,
818
analyzer: Optional[str] = None,
819
analyze_wildcard: Optional[bool] = None,
820
conflicts: Optional[str] = None,
821
default_operator: Optional[str] = None,
822
df: Optional[str] = None,
823
expand_wildcards: Optional[str] = None,
824
from_: Optional[int] = None,
825
ignore_unavailable: Optional[bool] = None,
826
lenient: Optional[bool] = None,
827
preference: Optional[str] = None,
828
q: Optional[str] = None,
829
refresh: Optional[bool] = None,
830
request_cache: Optional[bool] = None,
831
requests_per_second: Optional[float] = None,
832
routing: Optional[str] = None,
833
scroll: Optional[str] = None,
834
scroll_size: Optional[int] = None,
835
search_type: Optional[str] = None,
836
search_timeout: Optional[str] = None,
837
size: Optional[int] = None,
838
slices: Optional[Union[int, str]] = None,
839
sort: Optional[List[str]] = None,
840
stats: Optional[List[str]] = None,
841
terminate_after: Optional[int] = None,
842
timeout: Optional[str] = None,
843
version: Optional[bool] = None,
844
version_type: Optional[bool] = None,
845
wait_for_active_shards: Optional[str] = None,
846
wait_for_completion: Optional[bool] = None,
847
**kwargs
848
) -> ObjectApiResponse:
849
"""
850
Delete all documents matching a query.
851
852
Parameters:
853
- index: Index name(s) to delete from
854
- query: Query to match documents for deletion
855
- conflicts: How to handle version conflicts ('abort' or 'proceed')
856
- refresh: Whether to refresh after deletion
857
- requests_per_second: Throttle rate for deletions
858
- slices: Number of slices for parallel processing
859
- wait_for_completion: Whether to wait for completion
860
- (many other search parameters)
861
862
Returns:
863
ObjectApiResponse with deletion results
864
"""
865
866
def update_by_query(
867
self,
868
index: str,
869
query: Optional[Dict[str, Any]] = None,
870
script: Optional[Dict[str, Any]] = None,
871
analyzer: Optional[str] = None,
872
analyze_wildcard: Optional[bool] = None,
873
conflicts: Optional[str] = None,
874
default_operator: Optional[str] = None,
875
df: Optional[str] = None,
876
expand_wildcards: Optional[str] = None,
877
from_: Optional[int] = None,
878
ignore_unavailable: Optional[bool] = None,
879
lenient: Optional[bool] = None,
880
pipeline: Optional[str] = None,
881
preference: Optional[str] = None,
882
q: Optional[str] = None,
883
refresh: Optional[bool] = None,
884
request_cache: Optional[bool] = None,
885
requests_per_second: Optional[float] = None,
886
routing: Optional[str] = None,
887
scroll: Optional[str] = None,
888
scroll_size: Optional[int] = None,
889
search_type: Optional[str] = None,
890
search_timeout: Optional[str] = None,
891
size: Optional[int] = None,
892
slices: Optional[Union[int, str]] = None,
893
sort: Optional[List[str]] = None,
894
stats: Optional[List[str]] = None,
895
terminate_after: Optional[int] = None,
896
timeout: Optional[str] = None,
897
version: Optional[bool] = None,
898
version_type: Optional[bool] = None,
899
wait_for_active_shards: Optional[str] = None,
900
wait_for_completion: Optional[bool] = None,
901
**kwargs
902
) -> ObjectApiResponse:
903
"""
904
Update all documents matching a query.
905
906
Parameters:
907
- index: Index name(s) to update
908
- query: Query to match documents for update
909
- script: Script to apply to matched documents
910
- conflicts: How to handle version conflicts
911
- pipeline: Ingest pipeline to use
912
- refresh: Whether to refresh after updates
913
- requests_per_second: Throttle rate for updates
914
- slices: Number of slices for parallel processing
915
- wait_for_completion: Whether to wait for completion
916
917
Returns:
918
ObjectApiResponse with update results
919
"""
920
921
def reindex(
922
self,
923
source: Dict[str, Any],
924
dest: Dict[str, Any],
925
conflicts: Optional[str] = None,
926
max_docs: Optional[int] = None,
927
refresh: Optional[bool] = None,
928
requests_per_second: Optional[float] = None,
929
script: Optional[Dict[str, Any]] = None,
930
scroll: Optional[str] = None,
931
size: Optional[int] = None,
932
slices: Optional[Union[int, str]] = None,
933
timeout: Optional[str] = None,
934
wait_for_active_shards: Optional[str] = None,
935
wait_for_completion: Optional[bool] = None,
936
**kwargs
937
) -> ObjectApiResponse:
938
"""
939
Copy documents from source to destination index with optional transformation.
940
941
Parameters:
942
- source: Source index configuration with query, sort, etc.
943
- dest: Destination index configuration
944
- conflicts: How to handle conflicts ('abort' or 'proceed')
945
- max_docs: Maximum documents to reindex
946
- refresh: Whether to refresh destination after reindex
947
- requests_per_second: Throttle rate
948
- script: Transformation script
949
- scroll: Scroll context lifetime
950
- size: Batch size for reindexing
951
- slices: Number of slices for parallel processing
952
- timeout: Request timeout
953
- wait_for_active_shards: Wait for shard availability
954
- wait_for_completion: Whether to wait for completion
955
956
Returns:
957
ObjectApiResponse with reindex results
958
"""
959
```
960
961
### Utility Operations
962
963
Additional utility operations for document counting, field capabilities, and cluster information.
964
965
```python { .api }
966
def count(
967
self,
968
index: Optional[str] = None,
969
query: Optional[Dict[str, Any]] = None,
970
analyzer: Optional[str] = None,
971
analyze_wildcard: Optional[bool] = None,
972
default_operator: Optional[str] = None,
973
df: Optional[str] = None,
974
expand_wildcards: Optional[str] = None,
975
ignore_unavailable: Optional[bool] = None,
976
lenient: Optional[bool] = None,
977
min_score: Optional[float] = None,
978
preference: Optional[str] = None,
979
q: Optional[str] = None,
980
routing: Optional[str] = None,
981
terminate_after: Optional[int] = None,
982
**kwargs
983
) -> ObjectApiResponse:
984
"""
985
Count documents matching a query.
986
987
Parameters:
988
- index: Index name(s) to count in
989
- query: Query to match documents
990
- analyzer: Analyzer for query string
991
- analyze_wildcard: Whether to analyze wildcards
992
- default_operator: Default operator for query string
993
- df: Default field for query string
994
- expand_wildcards: How to expand wildcards
995
- ignore_unavailable: Whether to ignore unavailable indices
996
- lenient: Whether to ignore format errors
997
- min_score: Minimum score threshold
998
- preference: Node preference
999
- q: Query string
1000
- routing: Custom routing
1001
- terminate_after: Stop counting after this many matches
1002
1003
Returns:
1004
ObjectApiResponse with document count
1005
"""
1006
1007
def field_caps(
1008
self,
1009
index: Optional[str] = None,
1010
fields: Optional[List[str]] = None,
1011
allow_no_indices: Optional[bool] = None,
1012
expand_wildcards: Optional[str] = None,
1013
ignore_unavailable: Optional[bool] = None,
1014
include_unmapped: Optional[bool] = None,
1015
**kwargs
1016
) -> ObjectApiResponse:
1017
"""
1018
Get field capabilities across indices.
1019
1020
Parameters:
1021
- index: Index name(s) to analyze
1022
- fields: Field names to analyze
1023
- allow_no_indices: Whether to allow no matching indices
1024
- expand_wildcards: How to expand wildcards
1025
- ignore_unavailable: Whether to ignore unavailable indices
1026
- include_unmapped: Whether to include unmapped fields
1027
1028
Returns:
1029
ObjectApiResponse with field capabilities
1030
"""
1031
1032
def rank_eval(
1033
self,
1034
requests: List[Dict[str, Any]],
1035
index: Optional[str] = None,
1036
allow_no_indices: Optional[bool] = None,
1037
expand_wildcards: Optional[str] = None,
1038
ignore_unavailable: Optional[bool] = None,
1039
**kwargs
1040
) -> ObjectApiResponse:
1041
"""
1042
Evaluate search quality using ranked evaluation.
1043
1044
Parameters:
1045
- requests: List of evaluation requests with queries and ratings
1046
- index: Index name(s) to evaluate against
1047
- allow_no_indices: Whether to allow no matching indices
1048
- expand_wildcards: How to expand wildcards
1049
- ignore_unavailable: Whether to ignore unavailable indices
1050
1051
Returns:
1052
ObjectApiResponse with evaluation metrics
1053
"""
1054
1055
def terms_enum(
1056
self,
1057
index: str,
1058
field: str,
1059
string: Optional[str] = None,
1060
case_insensitive: Optional[bool] = None,
1061
index_filter: Optional[Dict[str, Any]] = None,
1062
search_after: Optional[str] = None,
1063
size: Optional[int] = None,
1064
timeout: Optional[str] = None,
1065
**kwargs
1066
) -> ObjectApiResponse:
1067
"""
1068
Enumerate terms in a field for autocomplete and term exploration.
1069
1070
Parameters:
1071
- index: Index name
1072
- field: Field name to enumerate terms from
1073
- string: String prefix to match terms
1074
- case_insensitive: Whether matching is case insensitive
1075
- index_filter: Filter to apply to documents
1076
- search_after: Term to search after for pagination
1077
- size: Maximum number of terms to return
1078
- timeout: Request timeout
1079
1080
Returns:
1081
ObjectApiResponse with enumerated terms
1082
"""
1083
1084
def info(
1085
self,
1086
**kwargs
1087
) -> ObjectApiResponse:
1088
"""
1089
Get basic information about the cluster and node.
1090
1091
Returns:
1092
ObjectApiResponse with cluster information
1093
"""
1094
1095
def ping(
1096
self,
1097
**kwargs
1098
) -> HeadApiResponse:
1099
"""
1100
Ping the cluster to check connectivity.
1101
1102
Returns:
1103
HeadApiResponse (200 if cluster is available)
1104
"""
1105
```
1106
1107
### Script Operations
1108
1109
Manage stored scripts for reuse across search and update operations.
1110
1111
```python { .api }
1112
def put_script(
1113
self,
1114
id: str,
1115
script: Dict[str, Any],
1116
context: Optional[str] = None,
1117
timeout: Optional[str] = None,
1118
**kwargs
1119
) -> ObjectApiResponse:
1120
"""
1121
Store a script for later execution.
1122
1123
Parameters:
1124
- id: Script ID
1125
- script: Script definition with source and lang
1126
- context: Script context (default is search and update)
1127
- timeout: Request timeout
1128
1129
Returns:
1130
ObjectApiResponse confirming script storage
1131
"""
1132
1133
def get_script(
1134
self,
1135
id: str,
1136
**kwargs
1137
) -> ObjectApiResponse:
1138
"""
1139
Retrieve a stored script.
1140
1141
Parameters:
1142
- id: Script ID to retrieve
1143
1144
Returns:
1145
ObjectApiResponse with script definition
1146
"""
1147
1148
def delete_script(
1149
self,
1150
id: str,
1151
timeout: Optional[str] = None,
1152
**kwargs
1153
) -> ObjectApiResponse:
1154
"""
1155
Delete a stored script.
1156
1157
Parameters:
1158
- id: Script ID to delete
1159
- timeout: Request timeout
1160
1161
Returns:
1162
ObjectApiResponse confirming deletion
1163
"""
1164
```