Пример #1
0
        public async Task <DocumentResponse <TEntity> > GetEntityByIdAsync <TEntity>(Id id)
            where TEntity : class
        {
            var result = new DocumentResponse <TEntity>();

            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var indexExistsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                      .ConfigureAwait(false);

            if (!indexExistsResponse.Exists)
            {
                result.IsOk         = false;
                result.ErrorMessage = $"Index {indexName} does not exist";

                return(result);
            }

            var documentPath = DocumentPath <TEntity> .Id(id);

            var getResponse = await _elasticClient.GetAsync(documentPath, descriptor => descriptor.Index(indexName))
                              .ConfigureAwait(false);

            if (!getResponse.IsValid)
            {
                result.IsOk         = false;
                result.ErrorMessage = getResponse.OriginalException.Message;
                result.Exception    = getResponse.OriginalException;
                return(result);
            }

            result.DocumentResult = getResponse;

            return(result);
        }
Пример #2
0
        private async Task <Response> DeleteEntityFromPathAsync <TEntity>(DocumentPath <TEntity> documentPath)
            where TEntity : class
        {
            var result    = new Response();
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var existsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                 .ConfigureAwait(false);

            if (!existsResponse.Exists)
            {
                throw new InvalidOperationException($"Index {indexName} does not exist");
            }

            var documentExistsResponse = await _elasticClient.DocumentExistsAsync(documentPath, d => d.Index(indexName))
                                         .ConfigureAwait(false);

            if (!documentExistsResponse.Exists)
            {
                return(result);
            }

            var deleteResponse = await _elasticClient.DeleteAsync(documentPath, d => d.Index(indexName))
                                 .ConfigureAwait(false);

            if (deleteResponse.IsValid)
            {
                return(result);
            }

            result.IsOk         = false;
            result.ErrorMessage = deleteResponse.OriginalException.Message;
            result.Exception    = deleteResponse.OriginalException;
            return(result);
        }
Пример #3
0
        public async Task GetAllFields <TEntity>() where TEntity : class

        {
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var mappingResponse = await _elasticClient.Indices.GetMappingAsync <TEntity>(m => m.Index(indexName));
        }
Пример #4
0
        public async Task <Response> AddEntitiesAsync <TEntity>(IList <TEntity> entities)
            where TEntity : class
        {
            var response = new Response();

            if (entities == null)
            {
                return(response);
            }

            if (!entities.Any())
            {
                return(response);
            }

            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var indexExistsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                      .ConfigureAwait(false);

            if (!indexExistsResponse.Exists)
            {
                var indexCreateResponse = await CreateIndexAsync <TEntity>(indexName)
                                          .ConfigureAwait(false);

                if (!indexCreateResponse.IsValid)
                {
                    response.IsOk         = false;
                    response.ErrorMessage = indexCreateResponse.OriginalException.Message;
                    response.Exception    = indexCreateResponse.OriginalException;
                    return(response);
                }
            }

            var bulkInsert = await _elasticClient.BulkAsync(b => b
                                                            .IndexMany(entities, (op,
                                                                                  item) => op.Index(indexName)
                                                                       .Id(GetEntityId(item)))
                                                            )
                             .ConfigureAwait(false);

            await _elasticClient.Indices.RefreshAsync(indexName)
            .ConfigureAwait(false);

            if (!bulkInsert.Errors)
            {
                return(response);
            }

            response.IsOk         = false;
            response.ErrorMessage = bulkInsert.OriginalException.Message;
            response.Exception    = bulkInsert.OriginalException;

            return(response);
        }
Пример #5
0
        public async Task <Response> RecreateIndexAsync <TEntity>()
            where TEntity : class
        {
            var result = new Response();

            var indexName      = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();
            var existsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                 .ConfigureAwait(false);

            if (existsResponse == null ||
                !existsResponse.Exists)
            {
                return(result);
            }

            var deleteResponse = await DeleteIndexAsync(indexName)
                                 .ContinueWith(task =>
            {
                var response = task.Result;
                if (!response.IsOk)
                {
                    result.IsOk         = false;
                    result.ErrorMessage = response.ErrorMessage;
                    result.Exception    = response.Exception;
                    return(result);
                }

                var createIndexResponse = CreateIndexAsync <TEntity>(indexName);

                if (createIndexResponse.Result.IsValid)
                {
                    return(result);
                }

                result.IsOk         = createIndexResponse.Result.Acknowledged;
                result.ErrorMessage = createIndexResponse.Result.OriginalException.Message;
                result.Exception    = createIndexResponse.Result.OriginalException;

                return(result);
            })
                                 .ConfigureAwait(false);

            if (deleteResponse.IsOk)
            {
                return(result);
            }

            result.IsOk         = false;
            result.ErrorMessage = deleteResponse.ErrorMessage;
            result.Exception    = deleteResponse.Exception;
            return(result);
        }
Пример #6
0
        private async Task CreateIndexIfNotExistsAsync <TEntity>()
            where TEntity : class
        {
            var indexName           = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();
            var indexExistsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                      .ConfigureAwait(false);

            if (!indexExistsResponse.Exists)
            {
                await CreateIndexAsync <TEntity>(indexName)
                .ConfigureAwait(false);
            }
        }
Пример #7
0
        public async Task <BulkResponse> DeleteEntitiesAsync <TEntity>(IEnumerable <TEntity> entities)
            where TEntity : class
        {
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            await CreateIndexIfNotExistsAsync <TEntity>();

            var response = await _elasticClient.BulkAsync(descriptor => descriptor.DeleteMany(entities, (op,
                                                                                                         item) => op.Index(indexName)));

            await _elasticClient.Indices.RefreshAsync(indexName);

            return(response);
        }
Пример #8
0
        public async Task <BulkResponse> AddOrUpdateEntitiesAsync <TEntity>(IEnumerable <TEntity> entities)
            where TEntity : class
        {
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            await CreateIndexIfNotExistsAsync <TEntity>();

            var response = await _elasticClient.BulkAsync(b => b.UpdateMany(entities, (op,
                                                                                       item) => op.Index(indexName)
                                                                            .DocAsUpsert()
                                                                            .Id(new Id(GetEntityId(item)))
                                                                            .Doc(item)));

            await _elasticClient.Indices.RefreshAsync(indexName)
            .ConfigureAwait(false);

            return(response);
        }
Пример #9
0
        public async Task <Response> AddEntityAsync <TEntity>(TEntity entity)
            where TEntity : class
        {
            var response  = new Response();
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var indexExistsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                      .ConfigureAwait(false);

            if (!indexExistsResponse.Exists)
            {
                var indexCreateResponse = await CreateIndexAsync <TEntity>(indexName)
                                          .ConfigureAwait(false);

                if (!indexCreateResponse.IsValid)
                {
                    response.IsOk         = false;
                    response.ErrorMessage = indexCreateResponse.OriginalException.Message;
                    response.Exception    = indexCreateResponse.OriginalException;
                    return(response);
                }
            }

            var entityAddedResult = await _elasticClient.IndexAsync(entity, i => i
                                                                    .Index(indexName)
                                                                    .Id(GetEntityId(entity)))
                                    .ConfigureAwait(false);

            await _elasticClient.Indices.RefreshAsync(indexName)
            .ConfigureAwait(false);

            if (entityAddedResult.IsValid)
            {
                return(response);
            }

            response.IsOk         = false;
            response.ErrorMessage = entityAddedResult.OriginalException.Message;
            return(response);
        }
Пример #10
0
        public async Task <ElasticSearchResponse <TEntity> > SearchAsync <TEntity>(ElasticSearchRequest <TEntity> elasticSearchRequest)
            where TEntity : class
        {
            var result = new ElasticSearchResponse <TEntity>();

            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var searchResponse = await _elasticClient.SearchAsync <TEntity>(s => CreateSearchDescriptor(elasticSearchRequest, s, indexName))
                                 .ConfigureAwait(false);

            if (!searchResponse.IsValid)
            {
                result.IsOk         = false;
                result.ErrorMessage = searchResponse.OriginalException?.Message;
                result.Exception    = searchResponse.OriginalException;
                return(result);
            }

            result.SearchResults = searchResponse;

            return(result);
        }
Пример #11
0
        public async Task <bool> CheckIfFieldTypeIsTextAsync <TEntity>(string propertyName,
                                                                       string propertyEntityName = null)
            where TEntity : class
        {
            var property = string.IsNullOrWhiteSpace(propertyEntityName)
                               ? propertyName
                               : propertyEntityName;
            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var mappingResponse = await _elasticClient.Indices.GetMappingAsync <TEntity>(m => m.Index(indexName));

            if (!mappingResponse.IsValid)
            {
                return(false);
            }

            var propertyDefinition = ElasticSearchExtensions.GetPropertyDefinition(mappingResponse, indexName, property);

            switch (propertyDefinition)
            {
            case NestedProperty nestedProperty:
            {
                var propertyProperty = nestedProperty.Properties[propertyName];

                return(string.Equals(propertyProperty.Type, FieldType.Text.ToString(), StringComparison.InvariantCultureIgnoreCase));
            }

            case null:
                return(false);

            default:
            {
                var isText = string.Equals(propertyDefinition.Type, FieldType.Text.ToString(), StringComparison.InvariantCultureIgnoreCase);
                return(isText);
            }
            }
        }
Пример #12
0
        public async Task <Response> UpdateEntityAsync <TEntity>(TEntity entity)
            where TEntity : class
        {
            var result = new Response();

            var indexName = ElasticSearchExtensions.GetIndexNameFrom <TEntity>();

            var indexExistsResponse = await _elasticClient.Indices.ExistsAsync(indexName)
                                      .ConfigureAwait(false);

            if (!indexExistsResponse.Exists)
            {
                result.IsOk         = false;
                result.ErrorMessage = $"Index {indexName} does not exist";
                return(result);
            }

            var documentPath = DocumentPath <TEntity> .Id(GetEntityId(entity));

            var updateResponse = await _elasticClient.UpdateAsync(documentPath, u => u
                                                                  .RetryOnConflict(3)
                                                                  .Index(indexName)
                                                                  .DocAsUpsert()
                                                                  .Doc(entity))
                                 .ConfigureAwait(false);

            if (updateResponse.IsValid)
            {
                return(result);
            }

            result.IsOk         = false;
            result.ErrorMessage = updateResponse.OriginalException.Message;
            result.Exception    = updateResponse.OriginalException;
            return(result);
        }