protected async Task <long> CountAsync(ElasticSearchOptions <T> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (EnableCache && options.UseCache)
            {
                var cachedValue = await Cache.GetAsync <long>(GetScopedCacheKey("count-" + options.CacheKey)).AnyContext();

                if (cachedValue.HasValue)
                {
                    return(cachedValue.Value);
                }
            }

            var countDescriptor = new CountDescriptor <T>().Query(f => f.Filtered(s => s.Filter(f2 => options.GetElasticSearchFilter(_supportsSoftDeletes))));

            countDescriptor.Indices(options.Indices.Any()
                ? options.Indices.ToArray()
                : GetIndices());

            countDescriptor.IgnoreUnavailable();
            countDescriptor.Type(typeof(T));
#if DEBUG
            _elasticClient.EnableTrace();
            var sw = Stopwatch.StartNew();
#endif
            var results = await _elasticClient.CountAsync <T>(countDescriptor).AnyContext();

#if DEBUG
            sw.Stop();
            _elasticClient.DisableTrace();
            Logger.Trace().Message($"CountAsync: {sw.ElapsedMilliseconds}ms, Serialization Took {results.ConnectionStatus.Metrics.SerializationTime}ms, Deserialization Took {results.ConnectionStatus.Metrics.DeserializationTime}ms").Write();
#endif

            if (!results.IsValid)
            {
                throw new ApplicationException($"ElasticSearch error code \"{results.ConnectionStatus.HttpStatusCode}\".", results.ConnectionStatus.OriginalException);
            }

            if (EnableCache && options.UseCache)
            {
                await Cache.SetAsync(GetScopedCacheKey("count-" + options.CacheKey), results.Count, options.GetCacheExpirationDate()).AnyContext();
            }

            return(results.Count);
        }
Пример #2
0
        public async Task <IActionResult> Index()
        {
            var products = await _ecssrDbContext.Products.CountAsync();

            if (products > 0)
            {
                ViewBag.DbInstalled = true;
            }

            var totalIndexed = await _elasticClient.CountAsync <Product>();

            ViewBag.TotalIndexed = totalIndexed.Count;

            return(View());
        }
Пример #3
0
 private static IEnumerable <Func <object> > DocumentCommandsAsync(IElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.BulkAsync(
             new BulkRequest("test_index")
         {
             Operations = new List <IBulkOperation>
             {
                 new BulkCreateOperation <Post>(new Post {
                     Id = 1, Title = "BulkCreateOperation"
                 })
             }
         }),
         () => elastic.CreateAsync(new CreateRequest <Post>(new Post {
             Id = 2, Title = "CreateRequest"
         }, "test_index")),
         () => elastic.CreateDocumentAsync(new Post {
             Id = 3, Title = "CreateDocument"
         }),
         () => elastic.CountAsync <Post>(),
         () => elastic.SearchAsync <Post>(s => s.MatchAll()),
         () => elastic.DeleteByQueryAsync(new DeleteByQueryRequest("test_index")
         {
             Size = 0
         })
     });
 }
Пример #4
0
        public async Task <IActionResult> GetAllDocuments()
        {
            try
            {
                var queryResponse = await _elasticClient
                                    .CountAsync <object>(s => s.Index(IndexName));

                var recordsCount = queryResponse.Count;

                var allRecords = await _elasticClient
                                 .SearchAsync <object>(s => s.Index(IndexName).Size(Convert.ToInt32(recordsCount)).MatchAll());

                if (queryResponse.IsValid)
                {
                    var responseObject = new Entities.SearchResponse
                    {
                        RecordCount = recordsCount,
                        Records     = allRecords.Documents
                    };

                    return(Ok(responseObject));
                }
                else
                {
                    return(BadRequest(queryResponse.ServerError.Error));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public virtual async Task <CountResponse> CountAsync <T, TKey>(string indexName,
                                                                       Func <QueryContainerDescriptor <T>, QueryContainer> query)
            where T : class
        {
            var response = await _esClient.CountAsync <T>(c => c.Index(indexName).Query(query));

            return(response);
        }
Пример #6
0
        public async Task <SearchViewModel> FindAsync(string query, int page = 1, int pageSize = 10)
        {
            var SearchViewModel = new SearchViewModel();

            query = string.IsNullOrWhiteSpace(query) ? "" : query;

            var count = await _elasticClient.CountAsync <GlobalSearchModel>(c => c
                                                                            .Index(indexGlobalSearch).Query(q => q.QueryString(d => d.Query('*' + query + '*'
                                                                                                                                            ))));


            var pageNameResponse = await _elasticClient.SearchAsync <GlobalSearchModel>(
                s => s.Index(indexGlobalSearch).Query(q => q.Bool(b => b
                                                                  .Should(mu => mu
                                                                          .Match(m => m
                                                                                 .Field(f => f.EnTitle)
                                                                                 .Query(query)
                                                                                 )
                                                                          )) || q.Bool(b => b
                                                                                       .Should(mu => mu
                                                                                               .Match(m => m
                                                                                                      .Field(f => f.ArTitle)
                                                                                                      .Query(query)
                                                                                                      )
                                                                                               ))).From((page - 1) * pageSize)
                .Size(pageSize));

            var pageNameResponseList      = pageNameResponse.Documents.ToList();
            var pageNameResponseListCount = pageSize - pageNameResponseList.Count;


            var allResponse = await _elasticClient.SearchAsync <GlobalSearchModel>(
                s => s.Index(indexGlobalSearch).Query(q => q.QueryString(d => d.Query('*' + query + '*')))
                .From((page - 1) * pageNameResponseListCount)
                .Size(pageNameResponseListCount));

            pageNameResponseList.ForEach(d => d.Order = 1);
            var allResponseList = allResponse.Documents.ToList();

            allResponseList.RemoveAll(x => pageNameResponseList.Select(d => d.Id).Contains(x.Id));
            allResponseList.ForEach(d => d.Order = 2);
            var data = pageNameResponseList.Union(allResponseList).OrderBy(d => d.Order).ToList();


            if (!allResponse.IsValid || !pageNameResponse.IsValid)
            {
                // We could handle errors here by checking response.OriginalException
                //or response.ServerError properties
                _logger.LogError("Failed to search documents");
                return(new SearchViewModel {
                });
            }

            SearchViewModel.GlobalSearchModels = data;
            SearchViewModel.Count = count.Count;
            return(SearchViewModel);
        }
Пример #7
0
        /// <summary>
        /// Fetches total count base on practiceId
        /// </summary>
        /// <param name="practiceId">PracticeId which can be null.</param>
        /// <returns>Total count for either all records or based on practiceId</returns>
        protected virtual async Task <long> GetTotalCount(long?practiceId)
        {
            var countResult = await _client.CountAsync <T>(GetTotalCountQuery(practiceId));

            if (!countResult.IsValid)
            {
                throw new Exception($"Unable to execute count search due to errors in query: {JsonConvert.SerializeObject(countResult.ServerError)}");
            }
            return(countResult.Count);
        }
Пример #8
0
        public async Task <IActionResult> DashboardReport()
        {
            var totalIndexed = await _elasticClient.CountAsync <Product>();

            var totalProducts = _productService.GetProductList(pageIndex: 0, pageSize: 1).TotalCount;
            var totalPictures = await _productService.GetProductPicturesCount();

            var result = new
            {
                totalIndexed  = totalIndexed.Count,
                totalProducts = totalProducts,
                totalPictures = totalPictures
            };

            return(Ok(result));
        }
Пример #9
0
        public async Task <int> SearchCommentsCountByUserId(int userId, int state)
        {
            var mustFilters = new List <Func <QueryContainerDescriptor <CommentVModel>, QueryContainer> >();

            if (userId > 0)
            {
                mustFilters.Add(f => f.Term(t => t.Field(fd => fd.UserId).Value(userId)));
            }
            if (state > -1)
            {
                mustFilters.Add(f => f.Term(t => t.Field(fd => fd.State).Value(state)));
            }
            mustFilters.Add(f => f.Term(t => t.Field(fd => fd.IsDelete).Value(false)));

            var response = await EsClient.CountAsync <CommentVModel>(s => s.Query(q => q.Bool(b => b.Filter(mustFilters))));

            return((int)response.Count);
        }
Пример #10
0
        async Task InitElasticAsync(CancellationToken cancellationToken = default)
        {
            await _elastic.InitializeAsync(cancellationToken);

            if (!SanityChecks)
            {
                return;
            }

            var count = await _elastic.CountAsync <DbUser>(cancellationToken);

            if (count < 0)
            {
                throw new ApplicationException("Elasticsearch sanity check failed.");
            }

            _logger.LogWarning("Elasticsearch sanity check succeeded.");
        }
Пример #11
0
        public async Task <int> GetDocumentsCount(FolderIdentity folderId, CancellationToken cancellationToken)
        {
            if (folderId == null)
            {
                throw new ArgumentNullException(nameof(folderId));
            }

            var response = await elasticClient.CountAsync <DocumentInfoModel>(
                cd => cd
                .Index(options.IndexName)
                .Query(qcd => qcd
                       .Term("folderId", folderId.ToString())),
                cancellationToken);

            CheckResponse(response, "Error during getting count documents in folder");

            return((int)response.Count);
        }
        public async Task <long> CountAsync(ICommandOptions options = null)
        {
            var response = await _client.CountAsync <T>(c => c.Query(q => q.MatchAll()).Index(String.Join(",", GetIndexesByQuery(null))).Type(ElasticType.Name)).AnyContext();

            _logger.Trace(() => response.GetRequest());

            if (!response.IsValid)
            {
                if (response.ApiCall.HttpStatusCode.GetValueOrDefault() == 404)
                {
                    return(0);
                }

                string message = response.GetErrorMessage();
                _logger.Error().Exception(response.OriginalException).Message(message).Property("request", response.GetRequest()).Write();
                throw new ApplicationException(message, response.OriginalException);
            }

            return(response.Count);
        }
Пример #13
0
        /// <summary>
        /// Get the total number of terms available in the version of a dictionary matching a specific audience and language.
        /// </summary>
        /// <param name="dictionary">The specific dictionary to retrieve from.</param>
        /// <param name="audience">The target audience.</param>
        /// <param name="language">Language (English - en; Spanish - es).</param>
        /// <returns>The number of terms available.</returns>
        public async Task <long> GetCount(string dictionary, AudienceType audience, string language)
        {
            // Set up the CountRequest to send to elasticsearch.
            Indices      index   = Indices.Index(new string[] { this._apiOptions.AliasName });
            Types        types   = Types.Type(new string[] { "terms" });
            CountRequest request = new CountRequest(index, types)
            {
                Query = new TermQuery {
                    Field = "language", Value = language.ToString()
                } &&
                new TermQuery {
                    Field = "audience", Value = audience.ToString()
                } &&
                new TermQuery {
                    Field = "dictionary", Value = dictionary.ToString()
                }
            };

            ICountResponse response = null;

            try
            {
                response = await _elasticClient.CountAsync <GlossaryTerm>(request);
            }
            catch (Exception ex)
            {
                String msg = $"Could not get a count for dictionary '{dictionary}', audience '{audience}', language '{language}'";
                _logger.LogError($"Error getting count on index: '{this._apiOptions.AliasName}'.");
                _logger.LogError(msg, ex);
                throw new APIErrorException(500, msg);
            }

            if (!response.IsValid)
            {
                String msg = $"Invalid response when searching for dictionary '{dictionary}', audience '{audience}', language '{language}'";
                _logger.LogError(msg);
                throw new APIErrorException(500, "errors occured");
            }

            return(response.Count);
        }
        public virtual async Task <long> CountAsync(ICommandOptions options = null)
        {
            var response = await _client.CountAsync <T>(c => c.Query(q => q.MatchAll()).Index(String.Join(",", GetIndexesByQuery(null))).Type(ElasticType.Name)).AnyContext();

            if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace))
            {
                _logger.LogTrace(response.GetRequest());
            }

            if (!response.IsValid)
            {
                if (response.ApiCall.HttpStatusCode.GetValueOrDefault() == 404)
                {
                    return(0);
                }

                string message = response.GetErrorMessage();
                _logger.LogError(response.OriginalException, message);
                throw new ApplicationException(message, response.OriginalException);
            }

            return(response.Count);
        }
        public virtual async Task <long> CountAsync(ICommandOptions options = null)
        {
            options = ConfigureOptions(options);

            var response = await _client.CountAsync <T>(c => c.Query(q => q.MatchAll()).Index(String.Join(",", GetIndexesByQuery(null))).Type(ElasticType.Name)).AnyContext();

            if (response.IsValid)
            {
                _logger.LogTraceRequest(response);
            }
            else
            {
                if (response.ApiCall.HttpStatusCode.GetValueOrDefault() == 404)
                {
                    return(0);
                }

                _logger.LogErrorRequest(response, "Error getting document count");
                throw new ApplicationException(response.GetErrorMessage(), response.OriginalException);
            }

            return(response.Count);
        }
Пример #16
0
        public async Task <EntityCount> Count()
        {
            try
            {
                var countResponse = await _client.CountAsync <T>(s => s.Index(_defaultIndex).Type(_defaultTypeName));

                if (!countResponse.IsValid)
                {
                    throw new DataException <T>(
                              "Count (all)",
                              Mapper.Map <Models.Result>(Nest.Result.Error),
                              $"{countResponse.OriginalException.Message}{Environment.NewLine}{countResponse.ServerError}{Environment.NewLine}{countResponse.DebugInformation}",
                              countResponse.OriginalException
                              );
                }
                return(new EntityCount(countResponse.Count));
            }
            catch (Exception ex)
            {
                var thing = ex.Message;
                return(null);
            }
        }
Пример #17
0
 /// <summary>
 /// The count API allows to easily execute a query and get the number of matches for that query.
 /// It can be executed across one or more indices and across one or more types.
 /// <para>This overload returns a dynamic response and defaults to all types and indices</para>
 /// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html
 /// </summary>
 /// <param name="client"></param>
 /// <param name="countSelector">An optional descriptor to describe the count operation</param>
 public static Task <ICountResponse> CountAsync(this IElasticClient client,
                                                Func <CountDescriptor <dynamic>, CountDescriptor <dynamic> > countSelector = null)
 {
     countSelector = countSelector ?? (s => s);
     return(client.CountAsync <dynamic>(c => countSelector(c.AllIndices().AllTypes())));
 }
        public async Task <long> GetCount(string query, int newsTypeId, string lang)
        {
            CountResponse response;

            if (lang == "en")
            {
                if (newsTypeId == 0)
                {
                    response = await _elasticClient.CountAsync <PageNews>(
                        s => s.Index(index).Query(q => q.QueryString(d => d.Query('*' + query + '*')) &&
                                                  q.Bool(b => b
                                                         .Must(m => m
                                                               .Exists(e => e
                                                                       .Field(f => f.EnShortDescription)
                                                                       )
                                                               ))
                                                  &&
                                                  q.Bool(b => b
                                                         .Must(m => m
                                                               .Exists(e => e
                                                                       .Field(f => f.EnTitle)
                                                                       )
                                                               ))));
                }
                else
                {
                    response = await _elasticClient.CountAsync <PageNews>(
                        s => s.Index(index).Query(q => q.QueryString(d => d.Query('*' + query + '*'))
                                                  &&
                                                  q.Nested(n => n
                                                           .Path(p => p.NewsTypesForNews)
                                                           .Query(qq => qq.Match(m => m.Field(f => f.NewsTypesForNews.First().NewsTypeId).Query("" + newsTypeId))))

                                                  &&
                                                  q.Bool(b => b
                                                         .Must(m => m
                                                               .Exists(e => e
                                                                       .Field(f => f.EnShortDescription)
                                                                       )
                                                               ))
                                                  &&
                                                  q.Bool(b => b
                                                         .Must(m => m
                                                               .Exists(e => e
                                                                       .Field(f => f.EnTitle)
                                                                       )
                                                               ))
                                                  ));
                }
            }
            else
            {
                if (newsTypeId == 0)
                {
                    response = await _elasticClient.CountAsync <PageNews>(
                        s => s.Index(index).Query(q => q.QueryString(d => d.Query('*' + query + '*'))));
                }
                else
                {
                    response = await _elasticClient.CountAsync <PageNews>(
                        s => s.Index(index).Query(q => q.QueryString(d => d.Query('*' + query + '*'))
                                                  &&
                                                  q.Nested(n => n
                                                           .Path(p => p.NewsTypesForNews)
                                                           .Query(qq => qq.Match(m => m.Field(f => f.NewsTypesForNews.First().NewsTypeId).Query("" + newsTypeId))))
                                                  ));
                }
            }



            if (!response.IsValid)
            {
                // We could handle errors here by checking response.OriginalException
                //or response.ServerError properties
                _logger.LogError("Failed to search documents");
                return(0);
            }
            return(response.Count);
        }
Пример #19
0
 public static async Task <long> GetDocumentsCount <T>(this IElasticClient client) where T : ElasticDocument
 => (await client.CountAsync <T>()).Count;
Пример #20
0
        public async Task <int> GetCount()
        {
            var result = await _elasticClient.CountAsync <Typography>(selector : x => x.AllIndices());

            return((int)result.Count);
        }
Пример #21
0
        public async Task <long> CountDocumentsAsync(string indexName)
        {
            var countResponse = await _elasticClient.CountAsync <object>(new CountRequest(indexName));

            return(countResponse.Count);
        }
Пример #22
0
        /// <inheritdoc cref="ISearchRepository.CountAsync" />
        public async Task <long> CountAsync(CancellationToken cancellationToken = default)
        {
            var result = await _elasticClient.CountAsync <Content>(null, cancellationToken);

            return(result.Count);
        }
Пример #23
0
        public override async Task <long> DocumentCountAsync()
        {
            var countResponse = await Client.CountAsync <TSearchDocument>(new CountRequest(IndexName));

            return(countResponse.Count);
        }
Пример #24
0
 public Task <int> CountAsync(CancellationToken cancellationToken = default)
 => _client.CountAsync <DbUser>(cancellationToken);