Skip to content

Commit 2c20831

Browse files
authored
Merge pull request #1189 from awais786/allow_skip_creation
feat: Adds an optional skipCreation. When set to true, documents that…
2 parents c9f60af + 3c22c85 commit 2c20831

3 files changed

Lines changed: 371 additions & 18 deletions

File tree

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ add_or_replace_documents_1: |-
5454
'poster': 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
5555
'overview': 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
5656
'release_date': '2019-03-23'
57-
}])
57+
}], skip_creation=True)
5858
add_or_update_documents_1: |-
5959
client.index('movies').update_documents([{
6060
'id': 287947,
6161
'title': 'Shazam ⚡️',
6262
'genres': 'comedy'
63-
}])
63+
}], skip_creation=True)
6464
delete_all_documents_1: |-
6565
client.index('movies').delete_all_documents()
6666
delete_one_document_1: |-

meilisearch/index.py

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ def add_documents(
482482
primary_key: Optional[str] = None,
483483
*,
484484
serializer: Optional[Type[JSONEncoder]] = None,
485+
skip_creation: Optional[bool] = None,
485486
) -> TaskInfo:
486487
"""Add documents to the index.
487488
@@ -494,6 +495,9 @@ def add_documents(
494495
serializer (optional):
495496
A custom JSONEncode to handle serializing fields that the build in json.dumps
496497
cannot handle, for example UUID and datetime.
498+
skip_creation (optional):
499+
If True, documents that don't exist in the index are silently ignored rather
500+
than created. If False or None (default), existing behavior is preserved.
497501
498502
Returns
499503
-------
@@ -506,7 +510,7 @@ def add_documents(
506510
MeilisearchApiError
507511
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
508512
"""
509-
url = self._build_url(primary_key)
513+
url = self._build_url(primary_key, skip_creation=skip_creation)
510514
add_document_task = self.http.post(url, documents, serializer=serializer)
511515
return TaskInfo(**add_document_task)
512516

@@ -517,6 +521,7 @@ def add_documents_in_batches(
517521
primary_key: Optional[str] = None,
518522
*,
519523
serializer: Optional[Type[JSONEncoder]] = None,
524+
skip_creation: Optional[bool] = None,
520525
) -> List[TaskInfo]:
521526
"""Add documents to the index in batches.
522527
@@ -531,6 +536,9 @@ def add_documents_in_batches(
531536
serializer (optional):
532537
A custom JSONEncode to handle serializing fields that the build in json.dumps
533538
cannot handle, for example UUID and datetime.
539+
skip_creation (optional):
540+
If True, documents that don't exist in the index are silently ignored rather
541+
than created. If False or None (default), existing behavior is preserved.
534542
535543
Returns
536544
-------
@@ -548,7 +556,9 @@ def add_documents_in_batches(
548556
tasks: List[TaskInfo] = []
549557

550558
for document_batch in self._batch(documents, batch_size):
551-
task = self.add_documents(document_batch, primary_key, serializer=serializer)
559+
task = self.add_documents(
560+
document_batch, primary_key, serializer=serializer, skip_creation=skip_creation
561+
)
552562
tasks.append(task)
553563

554564
return tasks
@@ -559,6 +569,7 @@ def add_documents_json(
559569
primary_key: Optional[str] = None,
560570
*,
561571
serializer: Optional[Type[JSONEncoder]] = None,
572+
skip_creation: Optional[bool] = None,
562573
) -> TaskInfo:
563574
"""Add documents to the index from a byte-encoded JSON string.
564575
@@ -571,6 +582,9 @@ def add_documents_json(
571582
serializer (optional):
572583
A custom JSONEncode to handle serializing fields that the build in json.dumps
573584
cannot handle, for example UUID and datetime.
585+
skip_creation (optional):
586+
If True, documents that don't exist in the index are silently ignored rather
587+
than created. If False or None (default), existing behavior is preserved.
574588
575589
Returns
576590
-------
@@ -584,14 +598,19 @@ def add_documents_json(
584598
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
585599
"""
586600
return self.add_documents_raw(
587-
str_documents, primary_key, "application/json", serializer=serializer
601+
str_documents,
602+
primary_key,
603+
"application/json",
604+
serializer=serializer,
605+
skip_creation=skip_creation,
588606
)
589607

590608
def add_documents_csv(
591609
self,
592610
str_documents: bytes,
593611
primary_key: Optional[str] = None,
594612
csv_delimiter: Optional[str] = None,
613+
skip_creation: Optional[bool] = None,
595614
) -> TaskInfo:
596615
"""Add documents to the index from a byte-encoded CSV string.
597616
@@ -603,6 +622,9 @@ def add_documents_csv(
603622
The primary-key used in index. Ignored if already set up.
604623
csv_delimiter:
605624
One ASCII character used to customize the delimiter for CSV. Comma used by default.
625+
skip_creation (optional):
626+
If True, documents that don't exist in the index are silently ignored rather
627+
than created. If False or None (default), existing behavior is preserved.
606628
607629
Returns
608630
-------
@@ -615,12 +637,15 @@ def add_documents_csv(
615637
MeilisearchApiError
616638
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
617639
"""
618-
return self.add_documents_raw(str_documents, primary_key, "text/csv", csv_delimiter)
640+
return self.add_documents_raw(
641+
str_documents, primary_key, "text/csv", csv_delimiter, skip_creation=skip_creation
642+
)
619643

620644
def add_documents_ndjson(
621645
self,
622646
str_documents: bytes,
623647
primary_key: Optional[str] = None,
648+
skip_creation: Optional[bool] = None,
624649
) -> TaskInfo:
625650
"""Add documents to the index from a byte-encoded NDJSON string.
626651
@@ -630,6 +655,9 @@ def add_documents_ndjson(
630655
Byte-encoded NDJSON string.
631656
primary_key (optional):
632657
The primary-key used in index. Ignored if already set up.
658+
skip_creation (optional):
659+
If True, documents that don't exist in the index are silently ignored rather
660+
than created. If False or None (default), existing behavior is preserved.
633661
634662
Returns
635663
-------
@@ -642,7 +670,9 @@ def add_documents_ndjson(
642670
MeilisearchApiError
643671
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
644672
"""
645-
return self.add_documents_raw(str_documents, primary_key, "application/x-ndjson")
673+
return self.add_documents_raw(
674+
str_documents, primary_key, "application/x-ndjson", skip_creation=skip_creation
675+
)
646676

647677
def add_documents_raw(
648678
self,
@@ -652,6 +682,7 @@ def add_documents_raw(
652682
csv_delimiter: Optional[str] = None,
653683
*,
654684
serializer: Optional[Type[JSONEncoder]] = None,
685+
skip_creation: Optional[bool] = None,
655686
) -> TaskInfo:
656687
"""Add documents to the index from a byte-encoded string.
657688
@@ -669,6 +700,9 @@ def add_documents_raw(
669700
serializer (optional):
670701
A custom JSONEncode to handle serializing fields that the build in json.dumps
671702
cannot handle, for example UUID and datetime.
703+
skip_creation (optional):
704+
If True, documents that don't exist in the index are silently ignored rather
705+
than created. If False or None (default), existing behavior is preserved.
672706
673707
Returns
674708
-------
@@ -681,7 +715,9 @@ def add_documents_raw(
681715
MeilisearchApiError
682716
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
683717
"""
684-
url = self._build_url(primary_key=primary_key, csv_delimiter=csv_delimiter)
718+
url = self._build_url(
719+
primary_key=primary_key, csv_delimiter=csv_delimiter, skip_creation=skip_creation
720+
)
685721
response = self.http.post(url, str_documents, content_type, serializer=serializer)
686722
return TaskInfo(**response)
687723

@@ -691,6 +727,7 @@ def update_documents(
691727
primary_key: Optional[str] = None,
692728
*,
693729
serializer: Optional[Type[JSONEncoder]] = None,
730+
skip_creation: Optional[bool] = None,
694731
) -> TaskInfo:
695732
"""Update documents in the index.
696733
@@ -703,6 +740,9 @@ def update_documents(
703740
serializer (optional):
704741
A custom JSONEncode to handle serializing fields that the build in json.dumps
705742
cannot handle, for example UUID and datetime.
743+
skip_creation (optional):
744+
If True, documents that don't exist in the index are silently ignored rather
745+
than created. If False or None (default), existing behavior is preserved.
706746
707747
Returns
708748
-------
@@ -715,14 +755,15 @@ def update_documents(
715755
MeilisearchApiError
716756
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
717757
"""
718-
url = self._build_url(primary_key)
758+
url = self._build_url(primary_key, skip_creation=skip_creation)
719759
response = self.http.put(url, documents, serializer=serializer)
720760
return TaskInfo(**response)
721761

722762
def update_documents_ndjson(
723763
self,
724764
str_documents: str,
725765
primary_key: Optional[str] = None,
766+
skip_creation: Optional[bool] = None,
726767
) -> TaskInfo:
727768
"""Update documents as a ndjson string in the index.
728769
@@ -732,6 +773,9 @@ def update_documents_ndjson(
732773
String of document from a NDJSON file.
733774
primary_key (optional):
734775
The primary-key used in index. Ignored if already set up
776+
skip_creation (optional):
777+
If True, documents that don't exist in the index are silently ignored rather
778+
than created. If False or None (default), existing behavior is preserved.
735779
736780
Returns
737781
-------
@@ -744,14 +788,17 @@ def update_documents_ndjson(
744788
MeilisearchApiError
745789
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
746790
"""
747-
return self.update_documents_raw(str_documents, primary_key, "application/x-ndjson")
791+
return self.update_documents_raw(
792+
str_documents, primary_key, "application/x-ndjson", skip_creation=skip_creation
793+
)
748794

749795
def update_documents_json(
750796
self,
751797
str_documents: str,
752798
primary_key: Optional[str] = None,
753799
*,
754800
serializer: Optional[Type[JSONEncoder]] = None,
801+
skip_creation: Optional[bool] = None,
755802
) -> TaskInfo:
756803
"""Update documents as a json string in the index.
757804
@@ -764,6 +811,9 @@ def update_documents_json(
764811
serializer (optional):
765812
A custom JSONEncode to handle serializing fields that the build in json.dumps
766813
cannot handle, for example UUID and datetime.
814+
skip_creation (optional):
815+
If True, documents that don't exist in the index are silently ignored rather
816+
than created. If False or None (default), existing behavior is preserved.
767817
768818
Returns
769819
-------
@@ -777,14 +827,19 @@ def update_documents_json(
777827
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
778828
"""
779829
return self.update_documents_raw(
780-
str_documents, primary_key, "application/json", serializer=serializer
830+
str_documents,
831+
primary_key,
832+
"application/json",
833+
serializer=serializer,
834+
skip_creation=skip_creation,
781835
)
782836

783837
def update_documents_csv(
784838
self,
785839
str_documents: str,
786840
primary_key: Optional[str] = None,
787841
csv_delimiter: Optional[str] = None,
842+
skip_creation: Optional[bool] = None,
788843
) -> TaskInfo:
789844
"""Update documents as a csv string in the index.
790845
@@ -796,6 +851,9 @@ def update_documents_csv(
796851
The primary-key used in index. Ignored if already set up.
797852
csv_delimiter:
798853
One ASCII character used to customize the delimiter for CSV. Comma used by default.
854+
skip_creation (optional):
855+
If True, documents that don't exist in the index are silently ignored rather
856+
than created. If False or None (default), existing behavior is preserved.
799857
800858
Returns
801859
-------
@@ -808,7 +866,9 @@ def update_documents_csv(
808866
MeilisearchApiError
809867
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
810868
"""
811-
return self.update_documents_raw(str_documents, primary_key, "text/csv", csv_delimiter)
869+
return self.update_documents_raw(
870+
str_documents, primary_key, "text/csv", csv_delimiter, skip_creation=skip_creation
871+
)
812872

813873
def update_documents_raw(
814874
self,
@@ -818,6 +878,7 @@ def update_documents_raw(
818878
csv_delimiter: Optional[str] = None,
819879
*,
820880
serializer: Optional[Type[JSONEncoder]] = None,
881+
skip_creation: Optional[bool] = None,
821882
) -> TaskInfo:
822883
"""Update documents as a string in the index.
823884
@@ -835,6 +896,9 @@ def update_documents_raw(
835896
serializer (optional):
836897
A custom JSONEncode to handle serializing fields that the build in json.dumps
837898
cannot handle, for example UUID and datetime.
899+
skip_creation (optional):
900+
If True, documents that don't exist in the index are silently ignored rather
901+
than created. If False or None (default), existing behavior is preserved.
838902
839903
Returns
840904
-------
@@ -847,7 +911,9 @@ def update_documents_raw(
847911
MeilisearchApiError
848912
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
849913
"""
850-
url = self._build_url(primary_key=primary_key, csv_delimiter=csv_delimiter)
914+
url = self._build_url(
915+
primary_key=primary_key, csv_delimiter=csv_delimiter, skip_creation=skip_creation
916+
)
851917
response = self.http.put(url, str_documents, content_type, serializer=serializer)
852918
return TaskInfo(**response)
853919

@@ -857,6 +923,7 @@ def update_documents_in_batches(
857923
batch_size: int = 1000,
858924
primary_key: Optional[str] = None,
859925
serializer: Optional[Type[JSONEncoder]] = None,
926+
skip_creation: Optional[bool] = None,
860927
) -> List[TaskInfo]:
861928
"""Update documents to the index in batches.
862929
@@ -871,6 +938,9 @@ def update_documents_in_batches(
871938
serializer (optional):
872939
A custom JSONEncode to handle serializing fields that the build in json.dumps
873940
cannot handle, for example UUID and datetime.
941+
skip_creation (optional):
942+
If True, documents that don't exist in the index are silently ignored rather
943+
than created. If False or None (default), existing behavior is preserved.
874944
875945
Returns
876946
-------
@@ -888,7 +958,9 @@ def update_documents_in_batches(
888958
tasks = []
889959

890960
for document_batch in self._batch(documents, batch_size):
891-
update_task = self.update_documents(document_batch, primary_key, serializer=serializer)
961+
update_task = self.update_documents(
962+
document_batch, primary_key, serializer=serializer, skip_creation=skip_creation
963+
)
892964
tasks.append(update_task)
893965

894966
return tasks
@@ -2359,13 +2431,16 @@ def _build_url(
23592431
self,
23602432
primary_key: Optional[str] = None,
23612433
csv_delimiter: Optional[str] = None,
2434+
skip_creation: Optional[bool] = None,
23622435
) -> str:
23632436
parameters = {}
23642437
if primary_key:
23652438
parameters["primaryKey"] = primary_key
23662439
if csv_delimiter:
23672440
parameters["csvDelimiter"] = csv_delimiter
2368-
if primary_key is None and csv_delimiter is None:
2441+
if skip_creation is True:
2442+
parameters["skipCreation"] = "true"
2443+
if not parameters:
23692444
return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}"
23702445
return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{parse.urlencode(parameters)}"
23712446

0 commit comments

Comments
 (0)