Ejemplo n.º 1
0
        /// <inheritdoc />
        public async Task <MultiResponse> CopyIndexAsync <T>(ISearchIndex sourceIndex, ISearchIndex destinationIndex,
                                                             RequestOptions requestOptions = null, CancellationToken ct = default) where T : class
        {
            if (sourceIndex.Config.AppId.Equals(destinationIndex.Config.AppId))
            {
                throw new AlgoliaException("Source and Destination indices should not be on the same application.");
            }

            // TODO: improve this section
            try
            {
                IndexSettings destinationSettings =
                    await destinationIndex.GetSettingsAsync(ct : ct).ConfigureAwait(false);

                if (destinationSettings != null)
                {
                    throw new AlgoliaException(
                              "Destination index already exists. Please delete it before copying index across applications.");
                }
            }
            catch (AlgoliaApiException ex)
            {
                // We want to catch an non existing index exception (404) and continue
                // Otherwise, we want to throw if it's another Http exception
                if (ex.HttpErrorCode != 404)
                {
                    throw;
                }
            }

            MultiResponse ret = new MultiResponse {
                Responses = new List <IAlgoliaWaitableResponse>()
            };

            // Save settings
            IndexSettings sourceSettings = await sourceIndex.GetSettingsAsync(ct : ct).ConfigureAwait(false);

            SetSettingsResponse destinationSettingsResp = await destinationIndex
                                                          .SetSettingsAsync(sourceSettings, requestOptions, ct)
                                                          .ConfigureAwait(false);

            ret.Responses.Add(destinationSettingsResp);

            // Save synonyms
            SynonymsIterator    sourceSynonyms             = new SynonymsIterator(sourceIndex);
            SaveSynonymResponse destinationSynonymResponse = await destinationIndex
                                                             .SaveSynonymsAsync(sourceSynonyms, requestOptions : requestOptions, ct : ct)
                                                             .ConfigureAwait(false);

            ret.Responses.Add(destinationSynonymResponse);

            // Save rules
            RulesIterator sourceRules             = new RulesIterator(sourceIndex);
            BatchResponse destinationRuleResponse = await destinationIndex
                                                    .SaveRulesAsync(sourceRules, requestOptions : requestOptions, ct : ct)
                                                    .ConfigureAwait(false);

            ret.Responses.Add(destinationRuleResponse);

            // Save objects (batched)
            IndexIterator <T>     indexIterator = sourceIndex.Browse <T>(new BrowseIndexQuery());
            BatchIndexingResponse saveObject    = await destinationIndex
                                                  .SaveObjectsAsync(indexIterator, requestOptions, ct)
                                                  .ConfigureAwait(false);

            ret.Responses.Add(saveObject);

            return(ret);
        }