コード例 #1
0
        public static async Task UpdateSearchIndex <T>(T entityToUpdate, CrudAction action) where T : Entity, new()
        {
            if (_updateSearchService)
            {
                if (entityToUpdate is ISearcheable searchable)
                {
                    var indexObject = searchable.GetIndexObject();
                    if (indexObject != null)
                    {
                        var targetType = typeof(T).Name.ToLower();
                        try
                        {
                            StringResponse response;
                            switch (action)
                            {
                            case CrudAction.Create:
                            {
                                indexObject.CreatedAt = DateTime.Now;
                                response = await _lowlevelClient.IndexAsync <StringResponse>(targetType, targetType,
                                                                                             indexObject.Id, PostData.Serializable(indexObject));
                            }
                            break;

                            case CrudAction.Update:
                            {
                                indexObject.UpdatedAt = DateTime.Now;
                                response =
                                    await _lowlevelClient.IndexPutAsync <StringResponse>(targetType, targetType,
                                                                                         indexObject.Id, PostData.Serializable(indexObject));

                                break;
                            }

                            case CrudAction.Delete:
                            {
                                response =
                                    await _lowlevelClient.DeleteAsync <StringResponse>(targetType, targetType,
                                                                                       indexObject.Id);

                                break;
                            }
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
        }
コード例 #2
0
        public async Task <Book> AddBook(Book book)
        {
            var id = Guid.NewGuid();

            book.Id = id;

            var indexResponse = await _client.IndexAsync <StringResponse>(
                IndexName, id.ToString(), PostData.Serializable(book));

            if (indexResponse.Success)
            {
                return(book);
            }

            throw indexResponse.OriginalException;
        }
コード例 #3
0
        private async Task IndexProductAsync(ProductForIndex productForIndex, IElasticLowLevelClient client)
        {
            string insertData = JsonConvert.SerializeObject(productForIndex);

            Log.Logger.Information($"Inserting product: [{productForIndex.SearchResultData.Id}]' {productForIndex.SearchResultData.Name}'");
            StringResponse response = await client.IndexAsync <StringResponse>(
                _elasticsearchConfiguration.IndexName,
                productForIndex.SearchResultData.Id.ToString(),
                PostData.String(insertData));

            if (response.Success)
            {
                Log.Logger.Information($"Product [{productForIndex.SearchResultData.Id}]' {productForIndex.SearchResultData.Name}' successfuly indexed");
            }
            else
            {
                Log.Logger.Error($"Unable to index product (ID: {productForIndex.SearchResultData.Id}): {response.Body}");
            }
        }
コード例 #4
0
        public async Task CreateMapping(string mappingObject)
        {
            var response = await _elasticClient.IndexAsync <string>(_indexName, _typeName, mappingObject);

            ThrowIfError(response);
        }