Skip to content

Commit e2a4f7f

Browse files
authored
Merge pull request #1548 from sanders41/skip-creation
Add skip creation option when updating documents
2 parents 1009078 + 1ca8d98 commit e2a4f7f

4 files changed

Lines changed: 172 additions & 9 deletions

File tree

meilisearch_python_sdk/index/async_index.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,7 @@ async def update_documents(
21702170
primary_key: str | None = None,
21712171
*,
21722172
custom_metadata: str | None = None,
2173+
skip_creation: bool = False,
21732174
compress: bool = False,
21742175
) -> TaskInfo:
21752176
"""Update documents in the index.
@@ -2179,6 +2180,8 @@ async def update_documents(
21792180
primary_key: The primary key of the documents. This will be ignored if already set.
21802181
Defaults to None.
21812182
custom_metadata: An arbitrary string accessible via the task. Defaults to None.
2183+
skip_creation: When set to true, documents that don't exist in the index are silently
2184+
ignored rather than created. Default = False.
21822185
compress: If set to True the data will be sent in gzip format. Defaults to False.
21832186
21842187
Returns:
@@ -2201,9 +2204,10 @@ async def update_documents(
22012204
params = {}
22022205
if primary_key:
22032206
params["primaryKey"] = primary_key
2204-
22052207
if custom_metadata:
22062208
params["customMetadata"] = custom_metadata
2209+
if skip_creation:
2210+
params["skipCreation"] = "true"
22072211

22082212
if params:
22092213
url = build_encoded_url(self._documents_url, params)
@@ -2320,6 +2324,7 @@ async def update_documents_in_batches(
23202324
primary_key: str | None = None,
23212325
custom_metadata: str | None = None,
23222326
compress: bool = False,
2327+
skip_creation: bool = False,
23232328
concurrency_limit: int | None = None,
23242329
) -> list[TaskInfo]:
23252330
"""Update documents in batches to reduce RAM usage with indexing.
@@ -2334,6 +2339,8 @@ async def update_documents_in_batches(
23342339
Defaults to None.
23352340
custom_metadata: An arbitrary string accessible via the task. Defaults to None.
23362341
compress: If set to True the data will be sent in gzip format. Defaults to False.
2342+
skip_creation: When set to true, documents that don't exist in the index are silently
2343+
ignored rather than created. Default = False.
23372344
concurrency_limit: If set this will limit the number of batches that will be sent
23382345
concurrently. This can be helpful if you find you are overloading the Meilisearch
23392346
server with requests. Defaults to None.
@@ -2364,6 +2371,7 @@ async def update_batch_with_limit(batch_data: Sequence[JsonMapping]) -> TaskInfo
23642371
batch_data,
23652372
primary_key=primary_key,
23662373
custom_metadata=custom_metadata,
2374+
skip_creation=skip_creation,
23672375
compress=compress,
23682376
)
23692377

@@ -2380,7 +2388,11 @@ async def update_batch_with_limit(batch_data: Sequence[JsonMapping]) -> TaskInfo
23802388
if not use_task_groups():
23812389
batches = [
23822390
self.update_documents(
2383-
x, primary_key, custom_metadata=custom_metadata, compress=compress
2391+
x,
2392+
primary_key,
2393+
custom_metadata=custom_metadata,
2394+
skip_creation=skip_creation,
2395+
compress=compress,
23842396
)
23852397
for x in batch(documents, batch_size)
23862398
]
@@ -2390,7 +2402,11 @@ async def update_batch_with_limit(batch_data: Sequence[JsonMapping]) -> TaskInfo
23902402
tasks = [
23912403
tg.create_task(
23922404
self.update_documents(
2393-
x, primary_key, custom_metadata=custom_metadata, compress=compress
2405+
x,
2406+
primary_key,
2407+
custom_metadata=custom_metadata,
2408+
skip_creation=skip_creation,
2409+
compress=compress,
23942410
)
23952411
)
23962412
for x in batch(documents, batch_size)
@@ -2406,6 +2422,7 @@ async def update_documents_from_directory(
24062422
document_type: str = "json",
24072423
csv_delimiter: str | None = None,
24082424
combine_documents: bool = True,
2425+
skip_creation: bool = False,
24092426
compress: bool = False,
24102427
) -> list[TaskInfo]:
24112428
"""Load all json files from a directory and update the documents.
@@ -2422,6 +2439,8 @@ async def update_documents_from_directory(
24222439
can only be used if the file is a csv file. Defaults to comma.
24232440
combine_documents: If set to True this will combine the documents from all the files
24242441
before indexing them. Defaults to True.
2442+
skip_creation: When set to true, documents that don't exist in the index are silently
2443+
ignored rather than created. Default = False.
24252444
compress: If set to True the data will be sent in gzip format. Defaults to False.
24262445
24272446
Returns:
@@ -2458,7 +2477,11 @@ async def update_documents_from_directory(
24582477
combined = await loop.run_in_executor(None, partial(combine_documents_, all_documents))
24592478

24602479
response = await self.update_documents(
2461-
combined, primary_key, custom_metadata=custom_metadata, compress=compress
2480+
combined,
2481+
primary_key,
2482+
custom_metadata=custom_metadata,
2483+
skip_creation=skip_creation,
2484+
compress=compress,
24622485
)
24632486
return [response]
24642487

@@ -2474,6 +2497,7 @@ async def update_documents_from_directory(
24742497
documents,
24752498
primary_key,
24762499
custom_metadata=custom_metadata,
2500+
skip_creation=skip_creation,
24772501
compress=compress,
24782502
)
24792503
)
@@ -2505,6 +2529,7 @@ async def update_documents_from_directory(
25052529
documents,
25062530
primary_key,
25072531
custom_metadata=custom_metadata,
2532+
skip_creation=skip_creation,
25082533
compress=compress,
25092534
)
25102535
]
@@ -2515,6 +2540,7 @@ async def update_documents_from_directory(
25152540
documents,
25162541
primary_key,
25172542
custom_metadata=custom_metadata,
2543+
skip_creation=skip_creation,
25182544
compress=compress,
25192545
)
25202546
)
@@ -2535,6 +2561,7 @@ async def update_documents_from_directory_in_batches(
25352561
csv_delimiter: str | None = None,
25362562
combine_documents: bool = True,
25372563
compress: bool = False,
2564+
skip_creation: bool = False,
25382565
concurrency_limit: int | None = None,
25392566
) -> list[TaskInfo]:
25402567
"""Load all json files from a directory and update the documents.
@@ -2554,6 +2581,8 @@ async def update_documents_from_directory_in_batches(
25542581
combine_documents: If set to True this will combine the documents from all the files
25552582
before indexing them. Defaults to True.
25562583
compress: If set to True the data will be sent in gzip format. Defaults to False.
2584+
skip_creation: When set to true, documents that don't exist in the index are silently
2585+
ignored rather than created. Default = False.
25572586
concurrency_limit: If set this will limit the number of batches that will be sent
25582587
concurrently. This can be helpful if you find you are overloading the Meilisearch
25592588
server with requests. Defaults to None.
@@ -2597,6 +2626,7 @@ async def update_documents_from_directory_in_batches(
25972626
primary_key=primary_key,
25982627
custom_metadata=custom_metadata,
25992628
compress=compress,
2629+
skip_creation=skip_creation,
26002630
concurrency_limit=concurrency_limit,
26012631
)
26022632

@@ -2616,6 +2646,7 @@ async def update_documents_from_directory_in_batches(
26162646
primary_key=primary_key,
26172647
custom_metadata=custom_metadata,
26182648
compress=compress,
2649+
skip_creation=skip_creation,
26192650
concurrency_limit=concurrency_limit,
26202651
)
26212652
)
@@ -2648,6 +2679,7 @@ async def update_documents_from_directory_in_batches(
26482679
primary_key=primary_key,
26492680
custom_metadata=custom_metadata,
26502681
compress=compress,
2682+
skip_creation=skip_creation,
26512683
concurrency_limit=concurrency_limit,
26522684
)
26532685
else:
@@ -2659,6 +2691,7 @@ async def update_documents_from_directory_in_batches(
26592691
primary_key=primary_key,
26602692
custom_metadata=custom_metadata,
26612693
compress=compress,
2694+
skip_creation=skip_creation,
26622695
concurrency_limit=concurrency_limit,
26632696
)
26642697
)
@@ -2675,6 +2708,7 @@ async def update_documents_from_file(
26752708
csv_delimiter: str | None = None,
26762709
*,
26772710
custom_metadata: str | None = None,
2711+
skip_creation: bool = False,
26782712
compress: bool = False,
26792713
) -> TaskInfo:
26802714
"""Add documents in the index from a json file.
@@ -2686,6 +2720,8 @@ async def update_documents_from_file(
26862720
csv_delimiter: A single ASCII character to specify the delimiter for csv files. This
26872721
can only be used if the file is a csv file. Defaults to comma.
26882722
custom_metadata: An arbitrary string accessible via the task. Defaults to None.
2723+
skip_creation: When set to true, documents that don't exist in the index are silently
2724+
ignored rather than created. Default = False.
26892725
compress: If set to True the data will be sent in gzip format. Defaults to False.
26902726
26912727
Returns:
@@ -2708,7 +2744,11 @@ async def update_documents_from_file(
27082744
)
27092745

27102746
return await self.update_documents(
2711-
documents, primary_key=primary_key, custom_metadata=custom_metadata, compress=compress
2747+
documents,
2748+
primary_key=primary_key,
2749+
custom_metadata=custom_metadata,
2750+
skip_creation=skip_creation,
2751+
compress=compress,
27122752
)
27132753

27142754
async def update_documents_from_file_in_batches(
@@ -2719,6 +2759,7 @@ async def update_documents_from_file_in_batches(
27192759
primary_key: str | None = None,
27202760
custom_metadata: str | None = None,
27212761
compress: bool = False,
2762+
skip_creation: bool = False,
27222763
concurrency_limit: int | None = None,
27232764
) -> list[TaskInfo]:
27242765
"""Updates documents form a json file in batches to reduce RAM usage with indexing.
@@ -2731,6 +2772,8 @@ async def update_documents_from_file_in_batches(
27312772
Defaults to None.
27322773
custom_metadata: An arbitrary string accessible via the task. Defaults to None.
27332774
compress: If set to True the data will be sent in gzip format. Defaults to False.
2775+
skip_creation: When set to true, documents that don't exist in the index are silently
2776+
ignored rather than created. Default = False.
27342777
concurrency_limit: If set this will limit the number of batches that will be sent
27352778
concurrently. This can be helpful if you find you are overloading the Meilisearch
27362779
server with requests. Defaults to None.
@@ -2760,6 +2803,7 @@ async def update_documents_from_file_in_batches(
27602803
primary_key=primary_key,
27612804
custom_metadata=custom_metadata,
27622805
compress=compress,
2806+
skip_creation=skip_creation,
27632807
concurrency_limit=concurrency_limit,
27642808
)
27652809

@@ -2770,6 +2814,7 @@ async def update_documents_from_raw_file(
27702814
csv_delimiter: str | None = None,
27712815
*,
27722816
custom_metadata: str | None = None,
2817+
skip_creation: bool = False,
27732818
compress: bool = False,
27742819
) -> TaskInfo:
27752820
"""Directly send csv or ndjson files to Meilisearch without pre-processing.
@@ -2785,6 +2830,8 @@ async def update_documents_from_raw_file(
27852830
csv_delimiter: A single ASCII character to specify the delimiter for csv files. This
27862831
can only be used if the file is a csv file. Defaults to comma.
27872832
custom_metadata: An arbitrary string accessible via the task. Defaults to None.
2833+
skip_creation: When set to true, documents that don't exist in the index are silently
2834+
ignored rather than created. Default = False.
27882835
compress: If set to True the data will be sent in gzip format. Defaults to False.
27892836
27902837
Returns:
@@ -2832,6 +2879,8 @@ async def update_documents_from_raw_file(
28322879
parameters["csvDelimiter"] = csv_delimiter
28332880
if custom_metadata:
28342881
parameters["customMetadata"] = custom_metadata
2882+
if skip_creation:
2883+
parameters["skipCreation"] = "true"
28352884

28362885
if parameters:
28372886
url = build_encoded_url(self._documents_url, parameters)

0 commit comments

Comments
 (0)