Exemplo n.º 1
0
        public void Heartbeat_WithNoExistingServer_DoesNothing()
        {
            // GIVEN
            const string serverId = "server-001";

            // WHEN
            _elasticConnection.Heartbeat(serverId);

            // THEN
            var getServerResponse = _elasticClient.Count <Model.Server>(descr => descr.Query(q => q.MatchAll())).ThrowIfInvalid();

            getServerResponse.Count.Should().Be(0);
        }
Exemplo n.º 2
0
 private static IEnumerable <Func <object> > DocumentCommands(IElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.Bulk(
             new BulkRequest("test_index")
         {
             Operations = new List <IBulkOperation>
             {
                 new BulkCreateOperation <Post>(new Post {
                     Id = 1, Title = "BulkCreateOperation"
                 })
             }
         }),
         () => elastic.Create(new CreateRequest <Post>(new Post {
             Id = 2, Title = "CreateRequest"
         }, "test_index")),
         () => elastic.CreateDocument(new Post {
             Id = 3, Title = "CreateDocument"
         }),
         () => elastic.Count <Post>(),
         () => elastic.Search <Post>(s => s.MatchAll()),
         () => elastic.DeleteByQuery(new DeleteByQueryRequest("test_index")
         {
             Size = 0
         })
     });
 }
Exemplo n.º 3
0
        public T ExecuteScalar <T>(QueryModel queryModel)
        {
            var queryAggregator = _elasticGeneratorQueryModelVisitor.GenerateElasticQuery <T>(queryModel);

            foreach (var resultOperator in queryModel.ResultOperators)
            {
                if (resultOperator is CountResultOperator)
                {
                    var result = _elasticClient.Count <object>(descriptor =>
                    {
                        descriptor.Index(_dataId);

                        if (queryAggregator.QueryContainers.Any())
                        {
                            descriptor.Query(q => q.Bool(x => x.Must(queryAggregator.QueryContainers.ToArray())));
                        }
                        return(descriptor);
                    }).Count;

                    var converter = TypeDescriptor.GetConverter(typeof(T));
                    if (converter.CanConvertFrom(typeof(string)))
                    {
                        return((T)converter.ConvertFromString(result.ToString()));
                    }
                }
            }

            return(default(T));
        }
        public CountResponse DocumentCount(IndexName index, [CallerMemberName] string callerName = "")
        {
            var timer  = Stopwatch.StartNew();
            var result = _client.Count(new CountRequest(index));

            ValidateResponse(result);
            SendLog(result.ApiCall, null, timer.ElapsedMilliseconds, $"Elasticsearch.Count.{callerName}");
            return(result);
        }
        protected long Count(ElasticSearchOptions <T> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            long?result;

            if (EnableCache && options.UseCache)
            {
                result = Cache.Get <long?>(GetScopedCacheKey("count-" + options.CacheKey));
                if (result.HasValue)
                {
                    return(result.Value);
                }
            }

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

            countDescriptor.Indices(options.Indices);
            countDescriptor.IgnoreUnavailable();

            countDescriptor.Type(typeof(T));

            _elasticClient.EnableTrace();
            var results = _elasticClient.Count <T>(countDescriptor);

            _elasticClient.DisableTrace();

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

            result = results.Count;

            if (EnableCache && options.UseCache)
            {
                Cache.Set(GetScopedCacheKey("count-" + options.CacheKey), result, options.GetCacheExpirationDate());
            }

            return(result.Value);
        }
Exemplo n.º 6
0
        public long CountOfDocumentsForIndex(string indexName)
        {
            var response = _client.Count <TUmbracoDocument>(c => c.Index(indexName).Type(DocumentTypeName));

            if (response.IsValid)
            {
                return(response.Count);
            }
            return(-1);
        }
Exemplo n.º 7
0
        public CountResponse Count <T>(Func <CountDescriptor <T>, ICountRequest> selector,
                                       [CallerMemberName] string callerName = "")
            where T : class
        {
            var timer  = Stopwatch.StartNew();
            var result = _client.Count(selector);

            SendLog(result.ApiCall, null, timer.ElapsedMilliseconds, $"Search : {callerName}");

            return(new CountResponse {
                Count = result.Count
            });
        }
Exemplo n.º 8
0
        public void PrintCounts()
        {
            //var userCount = _client.Count<Models.User>(search => search
            //    .Index(Storage.UserIndex));

            //var orgCount = _client.Count<TenantModel>(search => search
            //    .Index(Storage.TenantIndex));

            //var appCount = _client.Count<ServiceModel>(search => search
            //    .Index(Storage.ServiceIndex));

            //var jointCount = _client.Count<ServiceModel>(search => search
            //    .Index(Indices.Parse($"{Storage.UserIndex}, {Storage.TenantIndex}, {Storage.ServiceIndex}")));

            var claimCount = _client.Count <ClaimModel>(search => search
                                                        .Index(Storage.ClaimsAlias));

            //Console.WriteLine($"Users: {userCount.Count}");
            //Console.WriteLine($"Tenant: {orgCount.Count}");
            //Console.WriteLine($"Service: {appCount.Count}");
            //Console.WriteLine($"Total: {jointCount.Count}");

            Console.WriteLine($"Users: {claimCount.Count}");
        }
 private static int GetDatabaseSize(IElasticClient client)
 {
     return Convert.ToInt32(client.Count(c => c.Index(EsIndex).Type(EsType)).Count);
 }
Exemplo n.º 10
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 ICountResponse Count(this IElasticClient client,
                                    Func <CountDescriptor <dynamic>, CountDescriptor <dynamic> > countSelector = null)
 {
     countSelector = countSelector ?? (s => s);
     return(client.Count <dynamic>(c => countSelector(c.AllIndices().AllTypes())));
 }
Exemplo n.º 11
0
 public long EventCount()
 {
     _client.Refresh(r => r.Force(false));
     return(_client.Count <PersistentEvent>(c => c.Query(q => q.MatchAll())).Count);
 }
Exemplo n.º 12
0
        public long GetCountOfDocuments()
        {
            var searchResponse = _elasticClient.Count <Document>();

            return(searchResponse.Count);
        }
 public long Count()
 {
     return(_elasticClient.Count <T>(c => c.Query(q => q.MatchAll())).Count);
 }