Example #1
0
        public async Task AddOrUpdateAsync(Document document, CancellationToken cancellationToken = default)
        {
            ValidateElasticSearchEnabled();

            var client = _clientProvider.GetClient();

            var existsResponse = await client.DocumentExistsAsync(DocumentPath <EsDocument> .Id(document.Id),
                                                                  x => x.Index(_options.IndexName), cancellationToken);

            HandleError(existsResponse);

            var esDocument = new EsDocument
            {
                Id           = document.Id,
                ProjectId    = document.ProjectId,
                Name         = document.Name,
                FileName     = document.FileName,
                Content      = document.Content,
                LanguageCode = ConvertLanguageCode(document.LanguageCode),
                Version      = ConvertVersion(document.Version)
            };

            if (!existsResponse.Exists)
            {
                HandleError(await client.IndexAsync(esDocument,
                                                    x => x.Id(document.Id).Index(_options.IndexName), cancellationToken));
            }
            else
            {
                HandleError(await client.UpdateAsync(DocumentPath <EsDocument> .Id(document.Id),
                                                     x => x.Doc(esDocument).Index(_options.IndexName), cancellationToken));
            }
        }
        public virtual async Task AddOrUpdateAsync(Document document, CancellationToken cancellationToken = default)
        {
            var client = _clientProvider.GetClient();

            var esDocument = new EsDocument
            {
                Id           = NormalizeField(document.Id),
                ProjectId    = NormalizeField(document.ProjectId),
                Name         = document.Name,
                FileName     = document.FileName,
                Content      = document.Content,
                LanguageCode = NormalizeField(document.LanguageCode),
                Version      = NormalizeField(document.Version)
            };

            HandleError(await client.IndexAsync(esDocument, x => x.Index(_options.IndexName), cancellationToken));
        }