public static ElasticResult <bool?> ElasticSearchCreateIndices() { try { var client = GetElasticClient(); var isIndexExist = client.IndexExists(Indices.All, i => i.Index(EsIndexName)).Exists; if (isIndexExist) { return(ElasticResult <bool?> .SuccessResult(true)); } var response = client.CreateIndex(EsIndexName, i => i.Mappings( m => m .Map <ElasticUser>(map => map.AutoMap()) .Map <ElasticChat>(map => map.AutoMap()) .Map <ElasticChatUser>(map => map.AutoMap()) .Map <ElasticMessage>(map => map.AutoMap()))); return(response.ApiCall.Success ? ElasticResult <bool?> .SuccessResult(true) : ElasticResult <bool?> .FailResult(response.ApiCall.ServerError.Error.ToString())); } catch { return(ElasticResult <bool?> .FailResult(ServerErrorMessage)); } }
public ElasticResult <T[]> GetEntitiesFromElasticResponse <T>(ElasticResponse <T> response) where T : class { // If request bad executed. return(!response.Success ? ElasticResult <T[]> .FailResult(response.Message) : ElasticResult <T[]> .SuccessResult( response.Response.Hits.Select(h => h.Source).Where(s => s != null).ToArray())); }
public ElasticResult <T> Add <T>(string esType, T @object) where T : class, IGuidedEntity { var response = _elasticRepository.ExecuteCreateOrUpdateRequest(@object, esType); return(response.Success ? ElasticResult <T> .SuccessResult(@object) : ElasticResult <T> .FailResult(response.Message)); }
public ElasticResult <ElasticUser> Update(ElasticUser user) { var response = _elasticRepository.ExecuteCreateOrUpdateRequest(user, EsType); return(response.Success ? ElasticResult <ElasticUser> .SuccessResult(user) : ElasticResult <ElasticUser> .FailResult(response.Message)); }
public ElasticResult <ElasticChat> Update(ElasticChat chat) { var response = _elasticRepository.ExecuteCreateOrUpdateRequest(chat, EsType); return(response.Success ? ElasticResult <ElasticChat> .SuccessResult(chat) : ElasticResult <ElasticChat> .FailResult(response.Message)); }
public ElasticResult <bool> Remove <T>(string esType, string guid) where T : class { var documentPath = new DocumentPath <T>(guid).Index(_elasticRepository.EsIndex).Type(esType); var response = _elasticRepository.ExecuteDeleteRequest(documentPath); return(response.Success ? ElasticResult <bool> .SuccessResult(true) : ElasticResult <bool> .FailResult(response.Message)); }
public ElasticResult <T[]> GetEntitiesFromElasticResponseWithScroll <T>(ElasticResponse <T>[] responses) where T : class { var value = new List <T>(); foreach (var response in responses) { value.AddRange(GetEntitiesFromElasticResponse(response).Value.Select(s => s)); } return(ElasticResult <T[]> .SuccessResult(value.ToArray())); }
// Check Chat Is Unique private ElasticResult <ElasticChat> CheckChat(ElasticChat chat) { var searchDescriptor = new SearchDescriptor <ElasticChat>().Query( q => q.Term(t => t.Field(f => f.Guid).Value(chat.Guid))).Index(_elasticRepository.EsIndex).Type(EsType); var response = _elasticRepository.ExecuteSearchRequest(searchDescriptor); // If request well executed. And user is unique. if (response.Success && !response.Response.Hits.Any()) { return(ElasticResult <ElasticChat> .SuccessResult(chat)); } return(response.Success ? ElasticResult <ElasticChat> .FailResult("Server Error") : ElasticResult <ElasticChat> .FailResult(response.Message)); }
public ElasticResult <T> GetEntityIfOnlyOneEntityInElasticResponse <T>(ElasticResponse <T> response) where T : class { // If request bad executed. if (!response.Success) { return(ElasticResult <T> .FailResult(response.Message)); } var hits = response.Response.Hits; var hitsArray = hits as IHit <T>[] ?? hits.ToArray(); // If found not 1 entity if (response.Success && hitsArray.Count() != 1) { return(ElasticResult <T> .FailResult(null)); } return(ElasticResult <T> .SuccessResult(hitsArray.ElementAt(0).Source)); }
public ElasticResult <T[]> GetAllByGuids <T>(string esType, params string[] guids) where T : class { if (guids.Length == 0) { return(ElasticResult <T[]> .SuccessResult(new T[] {})); } var multiGetDescriptor = new MultiGetDescriptor().GetMany <T>(guids).Index(_elasticRepository.EsIndex).Type(esType); var response = _elasticRepository.ExecuteMultiGetRequest(multiGetDescriptor); if (!response.Success) { return(ElasticResult <T[]> .FailResult(response.Message)); } var multiGetResponse = response.Response; var hits = multiGetResponse.GetMany <T>(guids); return(ElasticResult <T[]> .SuccessResult(hits.Select(hit => hit.Source).Where(hit => hit != null).ToArray())); }
private ElasticResult <ElasticChatUser> CheckChatUser(ElasticChatUser chatUser) { var searchDescriptor = new SearchDescriptor <ElasticChatUser>().Query( q => q.Bool( b => b.Must( m => m.Term(t => t.Field(f => f.ChatGuid).Value(chatUser.ChatGuid)), m => m.Term(t => t.Field(f => f.UserGuid).Value(chatUser.UserGuid))))) .Index(_elasticRepository.EsIndex) .Type(EsType); var response = _elasticRepository.ExecuteSearchRequest(searchDescriptor); // If request well executed. And user is unique. if (response.Success && !response.Response.Hits.Any()) { return(ElasticResult <ElasticChatUser> .SuccessResult(chatUser)); } return(response.Success ? ElasticResult <ElasticChatUser> .FailResult("User in this chat already!") : ElasticResult <ElasticChatUser> .FailResult(response.Message)); }