@@ -599,6 +599,100 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
599599 return responses, nil
600600}
601601
602+ /*
603+ ReplaceAllObjectsWithTransformation is similar to the `replaceAllObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must have been passed to the client instantiation method.
604+ See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation details.
605+
606+ @param indexName string - the index name to replace objects into.
607+ @param objects []map[string]any - List of objects to replace.
608+ @param opts ...ReplaceAllObjectsOption - Optional parameters for the request.
609+ @return *ReplaceAllObjectsResponse - The response of the replace all objects operation.
610+ @return error - Error if any.
611+ */
612+ func (c *APIClient) ReplaceAllObjectsWithTransformation(indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsWithTransformationResponse, error) {
613+ if c.ingestionTransporter == nil {
614+ return nil, reportError(" `region` must be provided at client instantiation before calling this method." )
615+ }
616+
617+ tmpIndexName := fmt.Sprintf("%s_tmp_%d", indexName, time.Now().UnixNano())
618+
619+ conf := config{
620+ headerParams: map[string]string{} ,
621+ scopes: []ScopeType{ SCOPE_TYPE_SETTINGS, SCOPE_TYPE_RULES, SCOPE_TYPE_SYNONYMS} ,
622+ }
623+
624+ for _, opt := range opts {
625+ opt.apply(&conf)
626+ }
627+
628+ opts = append(opts, WithWaitForTasks(true))
629+
630+ copyResp, err := c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope(conf.scopes))), toRequestOptions(opts)...)
631+ if err != nil {
632+ return nil, err
633+ }
634+
635+ watchResp, err := c.ingestionTransporter.ChunkedPush(tmpIndexName, objects, ingestion.Action(ACTION_ADD_OBJECT), &indexName, toIngestionChunkedBatchOptions(replaceAllObjectsToChunkBactchOptions(opts))...)
636+ if err != nil {
637+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
638+
639+ return nil, err //nolint:wrapcheck
640+ }
641+
642+ _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
643+ if err != nil {
644+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
645+
646+ return nil, err
647+ }
648+
649+ copyResp, err = c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope(conf.scopes))), toRequestOptions(opts)...)
650+ if err != nil {
651+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
652+
653+ return nil, err
654+ }
655+
656+ _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
657+ if err != nil {
658+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
659+
660+ return nil, err
661+ }
662+
663+ moveResp, err := c.OperationIndex(c.NewApiOperationIndexRequest(tmpIndexName, NewOperationIndexParams(OPERATION_TYPE_MOVE, indexName)), toRequestOptions(opts)...)
664+ if err != nil {
665+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
666+
667+ return nil, err
668+ }
669+
670+ _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
671+ if err != nil {
672+ _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))
673+
674+ return nil, err
675+ }
676+
677+ var searchWatchResp []WatchResponse
678+
679+ rawResp, err := json.Marshal(watchResp)
680+ if err != nil {
681+ return nil, fmt.Errorf(" unable to convert the ingestion WatchResponse to search WatchResponse: %w" , err)
682+ }
683+
684+ err = json.Unmarshal(rawResp, &searchWatchResp)
685+ if err != nil {
686+ return nil, fmt.Errorf(" unable to convert the ingestion WatchResponse to search WatchResponse: %w" , err)
687+ }
688+
689+ return &ReplaceAllObjectsWithTransformationResponse{
690+ CopyOperationResponse: *copyResp,
691+ WatchResponses: searchWatchResp,
692+ MoveOperationResponse: *moveResp,
693+ } , nil
694+ }
695+
602696/*
603697ReplaceAllObjects replaces all objects (records) in the given `indexName` with the given `objects`. A temporary index is created during this process in order to backup your data.
604698See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation details.
@@ -708,7 +802,7 @@ func (c *APIClient) SaveObjectsWithTransformation(indexName string, objects []ma
708802 return nil, reportError(" `region` must be provided at client instantiation before calling this method." )
709803 }
710804
711- return c.ingestionTransporter.ChunkedPush(indexName, objects, ingestion.Action(ACTION_ADD_OBJECT), nil, toIngestionRequestOptions(toRequestOptions( opts) )...) //nolint:wrapcheck
805+ return c.ingestionTransporter.ChunkedPush(indexName, objects, ingestion.Action(ACTION_ADD_OBJECT), nil, toIngestionChunkedBatchOptions( opts)...) //nolint:wrapcheck
712806}
713807
714808/*
@@ -725,22 +819,22 @@ func (c *APIClient) PartialUpdateObjectsWithTransformation(indexName string, obj
725819 return nil, reportError(" `region` must be provided at client instantiation before calling this method." )
726820 }
727821
728- conf := config{
729- headerParams: map[string]string{} ,
730- createIfNotExists: true,
731- }
822+ conf := config{
823+ headerParams: map[string]string{} ,
824+ createIfNotExists: true,
825+ }
732826
733- for _, opt := range opts {
734- opt.apply(&conf)
735- }
827+ for _, opt := range opts {
828+ opt.apply(&conf)
829+ }
736830
737- var action Action
831+ var action Action
738832
739- if conf.createIfNotExists {
740- action = ACTION_PARTIAL_UPDATE_OBJECT
741- } else {
742- action = ACTION_PARTIAL_UPDATE_OBJECT_NO_CREATE
743- }
833+ if conf.createIfNotExists {
834+ action = ACTION_PARTIAL_UPDATE_OBJECT
835+ } else {
836+ action = ACTION_PARTIAL_UPDATE_OBJECT_NO_CREATE
837+ }
744838
745- return c.ingestionTransporter.ChunkedPush(indexName, objects, ingestion.Action(action), nil, toIngestionRequestOptions(toRequestOptions (opts))...) //nolint:wrapcheck
839+ return c.ingestionTransporter.ChunkedPush(indexName, objects, ingestion.Action(action), nil, toIngestionChunkedBatchOptions(partialUpdateObjectsToChunkedBatchOptions (opts))...) //nolint:wrapcheck
746840}
0 commit comments