Beispiel #1
0
        public static OfferedLabourerService findByOfferedServiceId(int id)
        {
            _connectionToEs = new ConnectionToES();
            var client = _connectionToEs.EsClient();


            Nest.ISearchResponse <OfferedLabourerService> response = client.Search <OfferedLabourerService>(s => s
                                                                                                            .Index("moll_ols")
                                                                                                            .Type("OLS")
                                                                                                            .Size(1)
                                                                                                            .Query(q => q
                                                                                                                   .Match(m => m
                                                                                                                          .Field(f => f.fld_offeredserviceid)
                                                                                                                          .Query(id.ToString())
                                                                                                                          )
                                                                                                                   )
                                                                                                            );

            var datasend = (from hits in response.Hits
                            select hits.Source).ToList();

            OfferedLabourerService package = (OfferedLabourerService)response.Documents.ElementAt(0);

            package.fld_cost = package.fld_cost / 100;

            return(package);
        }
Beispiel #2
0
        public static List <OfferedLabourerService> mostBought()
        {
            _connectionToEs = new ConnectionToES();
            var client = _connectionToEs.EsClient();

            Nest.ISearchResponse <OfferedLabourerService> response = client.Search <OfferedLabourerService>(s => s
                                                                                                            .Index("moll_ols")
                                                                                                            .Type("OLS")
                                                                                                            .Size(20)
                                                                                                            .Query(q => new BoolQuery {
            })
                                                                                                            .Sort(so => so
                                                                                                                  .Field(f => f
                                                                                                                         .Field(ff => ff.fld_timesbought)
                                                                                                                         .Order(SortOrder.Ascending)
                                                                                                                         )
                                                                                                                  )
                                                                                                            .Size(6)
                                                                                                            );

            var datasend = (from hits in response.Hits
                            select hits.Source).ToList();

            List <OfferedLabourerService> packages = response.Documents.ToList <OfferedLabourerService>();

            foreach (OfferedLabourerService package in packages)
            {
                package.fld_cost = package.fld_cost / 100;
            }

            return(response.Documents.ToList <OfferedLabourerService>());
        }
        public static List <tbl_userdata> AllUsers()
        {
            _connectionToEs = new ConnectionToES();
            var client = _connectionToEs.EsClient();



            BoolQuery boolQuery1 = new BoolQuery
            {
            };

            Nest.ISearchResponse <tbl_userdata> response = client.Search <tbl_userdata>(s => s
                                                                                        .Index("moll_users")
                                                                                        .Type("User")
                                                                                        .Query(q => q
                                                                                               .MatchAll()

                                                                                               )
                                                                                        .From(0)
                                                                                        .Size(1000)
                                                                                        );

            var json = client.RequestResponseSerializer.SerializeToString(boolQuery1);


            var datasend = (from hits in response.Hits
                            select hits.Source).ToList();

            List <tbl_userdata> packages = response.Documents.ToList <tbl_userdata>();


            return(response.Documents.ToList <tbl_userdata>());
        }
        public static decimal SearchPolozkyGetSum(Nest.ISearchResponse <BankovniPolozka> res)
        {
            decimal sum = 0;

            if (res.Aggregations != null && res.Aggregations.ContainsKey("sumKc"))
            {
                sum = (decimal)((Nest.ValueAggregate)res.Aggregations["sumKc"]).Value;
            }
            return(sum);
        }
 public ElasticSearchResponse(Nest.ISearchResponse <T> response)
 {
     this.Shards              = response.Shards;
     this.HitsMetaData        = response.HitsMetaData;
     this.Facets              = response.Facets;
     this.Aggregations        = response.Aggregations;
     this.Suggest             = response.Suggest;
     this.ElapsedMilliseconds = response.ElapsedMilliseconds;
     this.TimedOut            = response.TimedOut;
     this.ScrollId            = response.ScrollId;
 }
        public static Nest.ISearchResponse <BankovniPolozka> SearchPolozkyRaw(string queryString,
                                                                              BankovniUcet bu = null, int size = 500,
                                                                              AggregationContainerDescriptor <BankovniPolozka> anyAggregation = null)
        {
            AggregationContainerDescriptor <BankovniPolozka> baseAggrDesc = null;

            baseAggrDesc = anyAggregation == null ?
                           new AggregationContainerDescriptor <BankovniPolozka>().Sum("sumKc", m => m.Field(f => f.Castka))
                    : anyAggregation;

            Func <AggregationContainerDescriptor <BankovniPolozka>, AggregationContainerDescriptor <BankovniPolozka> > aggrFunc
                = (aggr) => { return(baseAggrDesc); };


            Nest.ISearchResponse <BankovniPolozka> res = null;

            if (bu != null)
            {
                res = HlidacStatu.Lib.ES.Manager.GetESClient_Ucty()
                      .Search <BankovniPolozka>(a => a
                                                .Size(500)
                                                .Aggregations(aggrFunc)
                                                .Query(qq => qq.Bool(b => b
                                                                     .Must(
                                                                         m => m.Term(t => t.Field(f => f.CisloUctu).Value(bu.Id)),
                                                                         m1 => m1.QueryString(qs => qs
                                                                                              .Query(queryString)
                                                                                              .DefaultOperator(Nest.Operator.And)
                                                                                              ))
                                                                     )
                                                       )
                                                );
            }
            else
            {
                res = HlidacStatu.Lib.ES.Manager.GetESClient_Ucty()
                      .Search <HlidacStatu.Lib.Data.TransparentniUcty.BankovniPolozka>(a => a
                                                                                       .Size(500)
                                                                                       .Aggregations(aggrFunc)
                                                                                       .Query(qq => qq.QueryString(qs => qs
                                                                                                                   .Query(queryString)
                                                                                                                   .DefaultOperator(Nest.Operator.And)
                                                                                                                   )
                                                                                              )
                                                                                       );
            }
            return(res);
        }
Beispiel #7
0
        //public static bool DeletePerson(Guid Id, ElasticClient client = null)
        //{
        //    if (client == null)
        //        client = Lib.ES.Manager.GetESClient();
        //    var res = client
        //        .Delete<Lib.Data.Person>(Id);
        //    return res.IsValid;
        //}

        public static void LogQueryError <T>(Nest.ISearchResponse <T> esReq, string text = "", System.Web.HttpContextBase httpContext = null, Exception ex = null)
            where T : class
        {
            Elasticsearch.Net.ServerError serverErr = esReq.ServerError;
            ESLogger.Error(new Devmasters.Core.Logging.LogMessage()
                           .SetMessage("ES query error: " + text
                                       + "\n\nCause:" + serverErr?.Error?.ToString()
                                       + "\n\nDetail:" + esReq.DebugInformation
                                       + "\n\n\n"
                                       )
                           .SetException(ex)
                           .SetCustomKeyValue("URL", httpContext?.Request?.RawUrl)
                           .SetCustomKeyValue("Stack-trace", System.Environment.StackTrace)
                           .SetCustomKeyValue("Referer", httpContext?.Request?.UrlReferrer?.AbsoluteUri)
                           .SetCustomKeyValue("User-agent", httpContext?.Request?.Browser?.Browser)
                           .SetCustomKeyValue("IP", httpContext?.Request?.UserHostAddress + " " + System.Web.HttpContext.Current?.Request?.UserHostName)
                           );
        }
Beispiel #8
0
        private void ResponseCheck(Nest.ISearchResponse <TEntity> res)
        {
            if (res == null)
            {
                throw new NullReferenceException("ResponseCheck response is null");
            }

            if (!res.ApiCall.Success)
            {
                throw new Exception($"ResponseCheck ApiCall Success {res.ApiCall.HttpMethod} {res.ApiCall.HttpStatusCode} {res.ServerError.Error.Reason}", res.ApiCall.OriginalException);
            }

            if (!res.IsValid)
            {
                throw new Exception($"ResponseCheck IsValid {res.ApiCall.HttpMethod} {res.ApiCall.HttpStatusCode} {res.ServerError.Error.Reason}", res.ApiCall.OriginalException);
            }

            TryQueryedClear();
        }
Beispiel #9
0
        //Get by LabourerId
        public static List <OfferedLabourerService> getByLabourer(int labourerid)
        {
            _connectionToEs = new ConnectionToES();
            var client = _connectionToEs.EsClient();


            Nest.ISearchResponse <OfferedLabourerService> response = client.Search <OfferedLabourerService>(s => s
                                                                                                            .Index("moll_ols")
                                                                                                            .Type("OLS")
                                                                                                            .Query(q => q
                                                                                                                   .Match(m => m
                                                                                                                          .Field(f => f.fld_labourerid)
                                                                                                                          .Query(labourerid.ToString())
                                                                                                                          )
                                                                                                                   )
                                                                                                            );

            var datasend = (from hits in response.Hits
                            select hits.Source).ToList();

            List <OfferedLabourerService> packages = response.Documents.ToList <OfferedLabourerService>();

            return(packages);
        }
Beispiel #10
0
        private Dictionary <string, object> GetDict(Nest.ISearchResponse <TEntity> res)
        {
            if (res.Aggregations == null)
            {
                throw new Exception($"ToDict Aggregations {res.ApiCall.HttpMethod} {res.ApiCall.HttpStatusCode}", res.ApiCall.OriginalException);
            }
            Dictionary <string, object> dict = new Dictionary <string, object>();

            int n = res.Aggregations.Count;

            foreach (var m in res.Aggregations)
            {
                dict.Add("Meta", m.Value.Meta);
                switch (m.Value.GetType().Name)
                {
                case "StatsAggregate":
                    var val2 = m.Value as StatsAggregate;
                    dict.Add("Average", val2.Average);
                    dict.Add("Count", val2.Count);
                    dict.Add("Max", val2.Max);
                    dict.Add("Min", val2.Min);
                    dict.Add("Sum", val2.Sum);
                    break;

                case "ValueAggregate":
                    var val = m.Value as ValueAggregate;
                    dict.Add("Value", val.Value);
                    break;

                case "TopHitsAggregate":
                    var val4 = m.Value as TopHitsAggregate;
                    dict.Add("MaxScore", val4.MaxScore);
                    dict.Add("Total", val4.Total);
                    break;

                case "BucketAggregate":
                    var val6 = m.Value as BucketAggregate;
                    dict.Add("AfterKey", val6.AfterKey);
                    dict.Add("BgCount", val6.BgCount);
                    dict.Add("DocCount", val6.DocCount);
                    dict.Add("DocCountErrorUpperBound", val6.DocCountErrorUpperBound);
                    dict.Add("Items", val6.Items);
                    foreach (var m2 in val6.Items)
                    {
                        var val66 = m2 as KeyedBucket <object>;
                        if (val66 != null)
                        {
                            dict.Add($"Items-{val66.Key}", val66.Values?.Count());
                            int i = 0;
                            foreach (var m4 in val66.Values)
                            {
                                switch (m4.GetType().Name)
                                {
                                case "ValueAggregate":
                                    var val6666 = m4 as ValueAggregate;
                                    dict.Add($"Items-{val66.Key}-{i++}", val6666.Value);
                                    break;
                                }
                            }
                        }
                    }
                    dict.Add("SumOtherDocCount", val6.SumOtherDocCount);
                    break;
                }
            }
            return(dict);
        }
Beispiel #11
0
 private List <TEntity> GetList(Nest.ISearchResponse <TEntity> res)
 {
     return(res.Documents.ToList());
 }
            public static VerejnaZakazkaSearchData _Search(
                VerejnaZakazkaSearchData search,
                AggregationContainerDescriptor <VerejnaZakazka> anyAggregation, ElasticClient client)
            {
                if (string.IsNullOrEmpty(search.Q) && (search.CPV == null || search.CPV.Length == 0))
                {
                    return(null);
                }

                if (client == null)
                {
                    client = HlidacStatu.Lib.ES.Manager.GetESClient_VZ();
                }

                AggregationContainerDescriptor <VerejnaZakazka> baseAggrDesc = null;

                baseAggrDesc = anyAggregation == null ?
                               null //new AggregationContainerDescriptor<VerejnaZakazka>().Sum("sumKc", m => m.Field(f => f.Castka))
                        : anyAggregation;

                Func <AggregationContainerDescriptor <VerejnaZakazka>, AggregationContainerDescriptor <VerejnaZakazka> > aggrFunc
                    = (aggr) => { return(baseAggrDesc); };

                string queryString = search.Q;

                Nest.ISearchResponse <VerejnaZakazka> res = null;
                if (search.CPV != null && search.CPV.Length > 0)
                {
                    string cpvs = search.CPV.Select(c => c + "*").Aggregate((f, s) => f + " OR " + s);
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        queryString = queryString + " AND (cPV:(" + cpvs + "))";
                    }
                    else
                    {
                        queryString = "cPV:(" + cpvs + ")";
                    }
                }

                int page = search.Page - 1;

                if (page < 0)
                {
                    page = 0;
                }
                try
                {
                    res = client
                          .Search <HlidacStatu.Lib.Data.VZ.VerejnaZakazka>(a => a
                                                                           .Size(search.PageSize)
                                                                           .From(search.PageSize * page)
                                                                           .Aggregations(aggrFunc)
                                                                           .Query(qq => qq.QueryString(qs => qs
                                                                                                       .Query(queryString)
                                                                                                       .DefaultOperator(Nest.Operator.And)
                                                                                                       )
                                                                                  )
                                                                           .Sort(ss => GetSort(Convert.ToInt32(search.Order)))
                                                                           .Aggregations(aggrFunc)
                                                                           );
                }
                catch (Exception e)
                {
                    Audit.Add(Audit.Operations.Search, "", "", "VerejnaZakazka", "error", search.Q, null);
                    if (res != null && res.ServerError != null)
                    {
                        HlidacStatu.Lib.ES.Manager.LogQueryError <VerejnaZakazka>(res, "Exception, Orig query:"
                                                                                  + search.OrigQuery + "   query:"
                                                                                  + search.Q
                                                                                  + "\n\n res:" + search.Result.ToString()
                                                                                  , ex: e);
                    }
                    else
                    {
                        HlidacStatu.Util.Consts.Logger.Error("", e);
                    }
                    throw;
                }
                Audit.Add(Audit.Operations.Search, "", "", "VerejnaZakazka", res.IsValid ? "valid" : "invalid", search.Q, null);


                search.IsValid        = res.IsValid;
                search.ElasticResults = res;
                search.Total          = res.Total;


                return(search);
            }
            public static ES.VerejnaZakazkaSearchData _Search(
                ES.VerejnaZakazkaSearchData search,
                AggregationContainerDescriptor <VerejnaZakazka> anyAggregation, ElasticClient client)
            {
                if (string.IsNullOrEmpty(search.Q) && (search.CPV == null || search.CPV.Length == 0))
                {
                    return(null);
                }

                if (client == null)
                {
                    client = HlidacStatu.Lib.ES.Manager.GetESClient_VZ();
                }

                AggregationContainerDescriptor <VerejnaZakazka> baseAggrDesc = null;

                baseAggrDesc = anyAggregation == null ?
                               null //new AggregationContainerDescriptor<VerejnaZakazka>().Sum("sumKc", m => m.Field(f => f.Castka))
                        : anyAggregation;

                Func <AggregationContainerDescriptor <VerejnaZakazka>, AggregationContainerDescriptor <VerejnaZakazka> > aggrFunc
                    = (aggr) => { return(baseAggrDesc); };

                string queryString = search.Q;

                Nest.ISearchResponse <VerejnaZakazka> res = null;
                if (search.CPV != null && search.CPV.Length > 0)
                {
                    string cpvs = search.CPV.Select(c => c + "*").Aggregate((f, s) => f + " OR " + s);
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        queryString = queryString + " AND (cPV:(" + cpvs + "))";
                    }
                    else
                    {
                        queryString = "cPV:(" + cpvs + ")";
                    }
                }

                int page = search.Page - 1;

                if (page < 0)
                {
                    page = 0;
                }
                res = client
                      .Search <HlidacStatu.Lib.Data.VZ.VerejnaZakazka>(a => a
                                                                       .Size(search.PageSize)
                                                                       .From(search.PageSize * page)
                                                                       .Aggregations(aggrFunc)
                                                                       .Query(qq => qq.QueryString(qs => qs
                                                                                                   .Query(queryString)
                                                                                                   .DefaultOperator(Nest.Operator.And)
                                                                                                   )
                                                                              )
                                                                       .Sort(ss => GetSort(Convert.ToInt32(search.Order)))
                                                                       .Aggregations(aggrFunc)
                                                                       );


                search.IsValid        = res.IsValid;
                search.ElasticResults = res;
                search.Total          = res.Total;


                return(search);
            }
Beispiel #14
0
 public static IListResultModel <T2> GetResultListModel <T, T2>(this Nest.ISearchResponse <T> searchResponse) where T : class where T2 : class, IModel <T, T2>, new()
 => new ListResultModel <T, T2>(searchResponse);
        protected async Task <SearchResultsModel> HandleResponse(SearchRequestModel request, Nest.ISearchResponse <T> response, SearchResultType resultType)
        {
            if (response.IsValid && response.Documents.Count > 0)
            {
                var sources    = response.HitsMetadata.Hits.Select(h => h.Source);
                var highlights = response.HitsMetadata.Hits.Select(h => h.Highlight);
                var results    = new List <BaseObject>();
                var pipe       = _pipeline.GetPipe();
                foreach (var r in sources)
                {
                    r.ViewerId = request.viewerId;
                    var final = await pipe.Pipe(new ActivityDeliveryContext { item = new Announce {
                                                                                  obj = r
                                                                              }, IsReading = true });

                    if (final != null)
                    {
                        results.Add(final);
                    }
                }
                return(new SearchResultsModel()
                {
                    Results = results.Skip(request.pagination.start).Take(request.pagination.count).Select(p => new SearchResultModel()
                    {
                        Type = resultType,
                        Value = p,
                    }).ToList(),
                    TotalResults = results.Count()
                });
            }
            return(new SearchResultsModel());
        }