示例#1
0
		public static QueryContainer ToContainer(PlainQuery query, QueryContainer queryContainer = null)
		{
			if (query == null) return null;
			var c = queryContainer ?? new QueryContainer();
			IQueryContainer fc = c;
			query.WrapInContainer(c);
			return c;
		}
		private void DoValidSemiStrictBoolQueryStatic(QueryContainer query)
		{
			Assert.DoesNotThrow(() =>
			{
				var s = new SearchDescriptor<ElasticsearchProject>()
					.From(0)
					.Take(10)
					.Query(query);

				this.JsonNotEquals(s, System.Reflection.MethodInfo.GetCurrentMethod(), "MatchAll");
			});
		}
示例#3
0
		public static PlainQuery operator &(PlainQuery leftQuery, PlainQuery rightQuery)
		{
			var lc = new QueryContainer();
			leftQuery.WrapInContainer(lc);
			var rc = new QueryContainer();
			rightQuery.WrapInContainer(rc);
			var query = ((lc && rc) as IQueryContainer).Bool;
			return new BoolQuery()
			{
				Must = query.Must,
				MustNot = query.MustNot,
				Should = query.Should
			};
		}
示例#4
0
        /// <summary>
        /// Searches the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="searchType">Type of the search.</param>
        /// <param name="entities">The entities.</param>
        /// <param name="fieldCriteria">The field criteria.</param>
        /// <param name="size">The size.</param>
        /// <param name="from">From.</param>
        /// <param name="totalResultsAvailable">The total results available.</param>
        /// <returns></returns>
        public override List <IndexModelBase> Search(string query, SearchType searchType, List <int> entities, SearchFieldCriteria fieldCriteria, int?size, int?from, out long totalResultsAvailable)
        {
            List <IndexModelBase> documents = new List <IndexModelBase>();

            totalResultsAvailable = 0;

            if (_client != null)
            {
                ISearchResponse <dynamic> results       = null;
                List <SearchResultModel>  searchResults = new List <SearchResultModel>();

                QueryContainer queryContainer = new QueryContainer();

                // add and field constraints
                var searchDescriptor = new SearchDescriptor <dynamic>().AllIndices();

                if (entities == null || entities.Count == 0)
                {
                    searchDescriptor = searchDescriptor.AllTypes();
                }
                else
                {
                    var entityTypes = new List <string>();
                    foreach (var entityId in entities)
                    {
                        // get entities search model name
                        var entityType = new EntityTypeService(new RockContext()).Get(entityId);
                        entityTypes.Add(entityType.IndexModelType.Name.ToLower());

                        // check if this is a person model, if so we need to add two model types one for person and the other for businesses
                        // wish there was a cleaner way to do this
                        if (entityType.Guid == SystemGuid.EntityType.PERSON.AsGuid())
                        {
                            entityTypes.Add("businessindex");
                        }
                    }

                    searchDescriptor = searchDescriptor.Type(string.Join(",", entityTypes));     // todo: consider adding indexmodeltype to the entity cache
                }

                QueryContainer matchQuery = null;
                if (fieldCriteria != null && fieldCriteria.FieldValues?.Count > 0)
                {
                    foreach (var match in fieldCriteria.FieldValues)
                    {
                        if (fieldCriteria.SearchType == CriteriaSearchType.Or)
                        {
                            matchQuery |= new MatchQuery {
                                Field = match.Field, Query = match.Value, Boost = match.Boost
                            };
                        }
                        else
                        {
                            matchQuery &= new MatchQuery {
                                Field = match.Field, Query = match.Value
                            };
                        }
                    }
                }

                switch (searchType)
                {
                case SearchType.ExactMatch:
                {
                    if (!string.IsNullOrWhiteSpace(query))
                    {
                        queryContainer &= new QueryStringQuery {
                            Query = query, AnalyzeWildcard = true
                        };
                    }

                    // special logic to support emails
                    if (query.Contains("@"))
                    {
                        queryContainer |= new QueryStringQuery {
                            Query = "email:" + query, Analyzer = "whitespace"
                        };                                                                                                    // analyzer = whitespace to keep the email from being parsed into 3 variables because the @ will act as a delimitor by default
                    }

                    // special logic to support phone search
                    if (query.IsDigitsOnly())
                    {
                        queryContainer |= new QueryStringQuery {
                            Query = "phone:*" + query + "*", AnalyzeWildcard = true
                        };
                    }

                    // add a search for all the words as one single search term
                    queryContainer |= new QueryStringQuery {
                        Query = query, AnalyzeWildcard = true, PhraseSlop = 0
                    };

                    if (matchQuery != null)
                    {
                        queryContainer &= matchQuery;
                    }

                    if (size.HasValue)
                    {
                        searchDescriptor.Size(size.Value);
                    }

                    if (from.HasValue)
                    {
                        searchDescriptor.From(from.Value);
                    }

                    searchDescriptor.Query(q => queryContainer);

                    results = _client.Search <dynamic>(searchDescriptor);
                    break;
                }

                case SearchType.Fuzzy:
                {
                    results = _client.Search <dynamic>(d =>
                                                       d.AllIndices().AllTypes()
                                                       .Query(q =>
                                                              q.Fuzzy(f => f.Value(query)
                                                                      .Rewrite(RewriteMultiTerm.TopTermsN))
                                                              )
                                                       );
                    break;
                }

                case SearchType.Wildcard:
                {
                    bool enablePhraseSearch = true;

                    if (!string.IsNullOrWhiteSpace(query))
                    {
                        QueryContainer wildcardQuery = null;

                        // break each search term into a separate query and add the * to the end of each
                        var queryTerms = query.Split(' ').Select(p => p.Trim()).ToList();

                        // special logic to support emails
                        if (queryTerms.Count == 1 && query.Contains("@"))
                        {
                            wildcardQuery |= new QueryStringQuery {
                                Query = "email:*" + query + "*", Analyzer = "whitespace"
                            };
                            enablePhraseSearch = false;
                        }
                        else
                        {
                            foreach (var queryTerm in queryTerms)
                            {
                                if (!string.IsNullOrWhiteSpace(queryTerm))
                                {
                                    wildcardQuery &= new QueryStringQuery {
                                        Query = queryTerm + "*", Analyzer = "whitespace", Rewrite = RewriteMultiTerm.ScoringBoolean
                                    };                                                                                                                                             // without the rewrite all results come back with the score of 1; analyzer of whitespaces says don't fancy parse things like check-in to 'check' and 'in'
                                }
                            }

                            // add special logic to help boost last names
                            if (queryTerms.Count > 1)
                            {
                                QueryContainer nameQuery = null;
                                nameQuery &= new QueryStringQuery {
                                    Query = "lastName:" + queryTerms.Last() + "*", Analyzer = "whitespace", Boost = 30
                                };
                                nameQuery &= new QueryStringQuery {
                                    Query = "firstName:" + queryTerms.First() + "*", Analyzer = "whitespace"
                                };
                                wildcardQuery |= nameQuery;
                            }

                            // special logic to support phone search
                            if (query.IsDigitsOnly())
                            {
                                wildcardQuery |= new QueryStringQuery {
                                    Query = "phoneNumbers:*" + query, Analyzer = "whitespace"
                                };
                            }
                        }

                        queryContainer &= wildcardQuery;
                    }

                    // add a search for all the words as one single search term
                    if (enablePhraseSearch)
                    {
                        queryContainer |= new QueryStringQuery {
                            Query = query, AnalyzeWildcard = true, PhraseSlop = 0
                        };
                    }

                    if (matchQuery != null)
                    {
                        queryContainer &= matchQuery;
                    }

                    if (size.HasValue)
                    {
                        searchDescriptor.Size(size.Value);
                    }

                    if (from.HasValue)
                    {
                        searchDescriptor.From(from.Value);
                    }

                    searchDescriptor.Query(q => queryContainer);

                    var indexBoost = GlobalAttributesCache.Value("UniversalSearchIndexBoost");

                    if (indexBoost.IsNotNullOrWhitespace())
                    {
                        var boostItems = indexBoost.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var boostItem in boostItems)
                        {
                            var boostParms = boostItem.Split(new char[] { '^' });

                            if (boostParms.Length == 2)
                            {
                                int boost = 1;
                                Int32.TryParse(boostParms[1], out boost);
                                searchDescriptor.IndicesBoost(b => b.Add(boostParms[0], boost));
                            }
                        }
                    }

                    results = _client.Search <dynamic>(searchDescriptor);
                    break;
                }
                }

                totalResultsAvailable = results.Total;

                // normallize the results to rock search results
                if (results != null)
                {
                    foreach (var hit in results.Hits)
                    {
                        IndexModelBase document = new IndexModelBase();

                        try
                        {
                            if (hit.Source != null)
                            {
                                Type indexModelType = Type.GetType($"{ ((string)((JObject)hit.Source)["indexModelType"])}, { ((string)((JObject)hit.Source)["indexModelAssembly"])}");

                                if (indexModelType != null)
                                {
                                    document = (IndexModelBase)((JObject)hit.Source).ToObject(indexModelType);   // return the source document as the derived type
                                }
                                else
                                {
                                    document = ((JObject)hit.Source).ToObject <IndexModelBase>(); // return the source document as the base type
                                }
                            }

                            if (hit.Explanation != null)
                            {
                                document["Explain"] = hit.Explanation.ToJson();
                            }

                            document.Score = hit.Score;

                            documents.Add(document);
                        }
                        catch { } // ignore if the result if an exception resulted (most likely cause is getting a result from a non-rock index)
                    }
                }
            }

            return(documents);
        }
 public Selector <T> Where(Expression <Func <T, Guid> > selector, Guid value)
 {
     queryContainer = queryContainer && +Wrap(selector, (a, w) => w.Match(r => r.Field(a).Query(value.ToString())));
     return(this);
 }
        public Func <QueryContainerDescriptor <es_t_bp_item>, QueryContainer> GetFilterQuery(TotalStatisSearchModel model)
        {
            Func <QueryContainerDescriptor <es_t_bp_item>, QueryContainer> filterQuery = q =>
            {
                string         dtFormatStr = "yyyy-MM-dd'T'HH:mm:ss";
                string         startDtStr  = model.StartDt.HasValue ? model.StartDt.Value.ToString(dtFormatStr) : string.Empty;
                string         endDtStr    = model.EndDt.HasValue ? model.EndDt.Value.AddDays(1).ToString(dtFormatStr) : string.Empty;
                QueryContainer initQuery   = q.Exists(qe => qe.Field(qef => qef.SYSPRIMARYKEY));
                if (model.DtType == "ENTRUSTDATE")
                {
                    if (!startDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.ENTRUSTDATE).GreaterThanOrEquals(DateMath.FromString(startDtStr)));
                    }
                    if (!endDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.ENTRUSTDATE).LessThan(DateMath.FromString(endDtStr)));
                    }
                }
                else if (model.DtType == "CHECKDATE")
                {
                    if (!startDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.CHECKDATE).GreaterThanOrEquals(DateMath.FromString(startDtStr)));
                    }
                    if (!endDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.CHECKDATE).LessThan(DateMath.FromString(endDtStr)));
                    }
                }
                else if (model.DtType == "APPROVEDATE")
                {
                    if (!startDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.APPROVEDATE).GreaterThanOrEquals(DateMath.FromString(startDtStr)));
                    }
                    if (!endDtStr.IsNullOrEmpty())
                    {
                        initQuery = initQuery && +q.DateRange(d => d.Field(f => f.APPROVEDATE).LessThan(DateMath.FromString(endDtStr)));
                    }
                }
                if (!model.CheckInstID.IsNullOrEmpty())
                {
                    initQuery = initQuery && +q.Term(s => s.Field(ss => ss.CUSTOMID).Value(model.CheckInstID));
                }
                if (!model.SearchType.IsNullOrEmpty())
                {
                    if (model.SearchType == "modify")
                    {
                        //HAVELOG == 1;
                        initQuery = initQuery && +q.Term(s => s.Field(ss => ss.HAVELOG).Value("1"));
                    }
                    if (model.SearchType == "unquali")
                    {
                        initQuery = initQuery && +q.Term(s => s.Field(ss => ss.CONCLUSIONCODE).Value("N"));
                    }
                    if (model.SearchType == "acs")
                    {
                        initQuery = initQuery && +q.Term(s => s.Field(ss => ss.HAVEACS).Value("1"));
                    }
                }
                return(initQuery);
            };

            return(filterQuery);
        }
示例#7
0
        private QueryContainer GetQuery(ApprenticeshipSearchRequestParameters parameters, QueryContainerDescriptor <ApprenticeshipSearchResult> q)
        {
            if (!string.IsNullOrEmpty(parameters.VacancyReference))
            {
                return(q.Bool(fq =>
                              fq.Filter(f =>
                                        f.Term(t =>
                                               t.VacancyReference, parameters.VacancyReference))));
            }

            QueryContainer query = null;

            query &= GetKeywordQuery(parameters, q);

            if (parameters.FrameworkLarsCodes.Any() || parameters.StandardLarsCodes.Any())
            {
                var queryClause = q.Terms(apprenticeship => apprenticeship.Field(f => f.FrameworkLarsCode).Terms(parameters.FrameworkLarsCodes)) ||
                                  q.Terms(apprenticeship => apprenticeship.Field(f => f.StandardLarsCode).Terms(parameters.StandardLarsCodes));

                query &= queryClause;
            }

            if (!string.IsNullOrWhiteSpace(parameters.CategoryCode))
            {
                var categoryCodes = new List <string>
                {
                    parameters.CategoryCode
                };

                var queryCategory = q.Terms(f => f.Field(g => g.CategoryCode).Terms(categoryCodes.Distinct()));

                query &= queryCategory;
            }

            if (parameters.ExcludeVacancyIds != null && parameters.ExcludeVacancyIds.Any())
            {
                var queryExcludeVacancyIds = !q.Ids(i => i.Values(parameters.ExcludeVacancyIds.Select(x => x.ToString(CultureInfo.InvariantCulture))));
                query &= queryExcludeVacancyIds;
            }

            if (parameters.VacancyLocationType != VacancyLocationType.Unknown)
            {
                var queryVacancyLocation = q.Match(m => m.Field(f => f.VacancyLocationType).Query(parameters.VacancyLocationType.ToString()));

                query &= queryVacancyLocation;
            }

            if (parameters.FromDate.HasValue)
            {
                var queryClause = q.DateRange(range =>
                                              range.Field(apprenticeship => apprenticeship.PostedDate)
                                              .GreaterThanOrEquals(parameters.FromDate));

                query &= queryClause;
            }

            if (parameters.Ukprn.HasValue)
            {
                var queryClause = q
                                  .Match(m => m.Field(f => f.Ukprn)
                                         .Query(parameters.Ukprn.ToString()));
                query &= queryClause;
            }

            if (!string.IsNullOrWhiteSpace(parameters.ApprenticeshipLevel) && parameters.ApprenticeshipLevel != "All")
            {
                var queryClause = q
                                  .Match(m => m.Field(f => f.ApprenticeshipLevel)
                                         .Query(parameters.ApprenticeshipLevel));
                query &= queryClause;
            }

            if (parameters.DisabilityConfidentOnly)
            {
                // Nest package won't allow a boolean directly and has to be a string
                // Elastic will throw if its not lower case
                // As we specifically only add this when this statement is true, explicit value is passed.
                var queryDisabilityConfidentOnly = q
                                                   .Match(m => m.Field(f => f.IsDisabilityConfident)
                                                          .Query("true"));

                query &= queryDisabilityConfidentOnly;
            }

            if (parameters.CanFilterByGeoDistance)
            {
                var geoQueryClause = q.Bool(qf => qf.Filter(f => f
                                                            .GeoDistance(vs => vs
                                                                         .Field(field => field.Location)
                                                                         .Location(parameters.Latitude.Value, parameters.Longitude.Value)
                                                                         .Distance(parameters.SearchRadius.Value, DistanceUnit.Miles))));

                query &= geoQueryClause;
            }

            return(query);
        }
示例#8
0
        public static ValidateQueryResponse ValidateSpecificQueryRaw <T>(Nest.ElasticClient client, QueryContainer qc)
            where T : class
        {
            var res = client.Indices
                      .ValidateQuery <T>(v => v
                                         .Query(q => qc)
                                         );

            return(res);
        }
示例#9
0
 public static SearchDescriptor <T> FilterOn <T>(this SearchDescriptor <T> searchDescriptor, QueryContainer container) where T : class
 {
     return(searchDescriptor.Query(q => container));
 }
示例#10
0
        /// <summary>
        /// 通过条件删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="indexName"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static async Task DeleteByQueryAsync_ <T>(this IElasticClient client, string indexName, QueryContainer where) where T : class, IElasticSearchIndex
        {
            var query = new DeleteByQueryRequest <T>(indexName)
            {
                Query = where
            };

            var response = await client.DeleteByQueryAsync(query);

            response.ThrowIfException();
        }
示例#11
0
        public static AnalysisCalculation.VazbyFiremNaUradyStat UradyObchodujiciSFirmami_s_vazbouNaPolitiky(Relation.AktualnostType aktualnost, bool showProgress = false)
        {
            HlidacStatu.Lib.Data.AnalysisCalculation.VazbyFiremNaPolitiky vazbyNaPolitiky = null;
            List <Sponzoring> sponzorujiciFirmy = null;

            QueryContainer qc = null;

            switch (aktualnost)
            {
            case HlidacStatu.Lib.Data.Relation.AktualnostType.Aktualni:
                vazbyNaPolitiky = StaticData.FirmySVazbamiNaPolitiky_aktualni_Cache.Get();
                qc = new QueryContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>().Term(t => t.Field(f => f.SVazbouNaPolitikyAktualni).Value(true));
                sponzorujiciFirmy = StaticData.SponzorujiciFirmy_Nedavne.Get();
                break;

            case HlidacStatu.Lib.Data.Relation.AktualnostType.Nedavny:
                vazbyNaPolitiky = StaticData.FirmySVazbamiNaPolitiky_nedavne_Cache.Get();
                qc = new QueryContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>().Term(t => t.Field(f => f.SVazbouNaPolitikyNedavne).Value(true));
                sponzorujiciFirmy = StaticData.SponzorujiciFirmy_Nedavne.Get();
                break;

            case HlidacStatu.Lib.Data.Relation.AktualnostType.Neaktualni:
            case HlidacStatu.Lib.Data.Relation.AktualnostType.Libovolny:
                vazbyNaPolitiky = StaticData.FirmySVazbamiNaPolitiky_vsechny_Cache.Get();
                qc = new QueryContainerDescriptor <HlidacStatu.Lib.Data.Smlouva>().Term(t => t.Field(f => f.SVazbouNaPolitiky).Value(true));
                sponzorujiciFirmy = StaticData.SponzorujiciFirmy_Vsechny.Get();
                break;
            }


            Func <int, int, Nest.ISearchResponse <Lib.Data.Smlouva> > searchFunc = null;

            searchFunc = (size, page) =>
            {
                return(Lib.ES.Manager.GetESClient().Search <Lib.Data.Smlouva>(a => a
                                                                              .TrackTotalHits(page * size == 0)
                                                                              .Size(size)
                                                                              .From(page * size)
                                                                              .Source(m => m.Excludes(e => e.Field(o => o.Prilohy)))
                                                                              .Query(q => qc)
                                                                              .Scroll("1m")
                                                                              ));
            };


            //TODO predelat z projeti vsech smluv na hledani pres vsechna ICO  v RS, vybrani statnich firem,
            //a dohlednai jejich statistiky vuci jednotlivym ostatnim firmam v RS
            Dictionary <string, Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > > > uradyStatni = new Dictionary <string, Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > > >();
            Dictionary <string, Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > > > uradySoukr  = new Dictionary <string, Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > > >();
            object lockObj = new object();

            Lib.Searching.Tools.DoActionForQuery <Lib.Data.Smlouva>(Lib.ES.Manager.GetESClient(), searchFunc,
                                                                    (hit, param) =>
            {
                Lib.Data.Smlouva s = hit.Source;
                List <string> icos = new List <string>();
                try
                {
                    var objednatelIco = s.Platce.ico;
                    if (!string.IsNullOrEmpty(objednatelIco))
                    {
                        Firma ff = Firmy.Get(objednatelIco);
                        if (!ff.Valid || !ff.PatrimStatu())
                        {
                            goto end;
                        }

                        //vsichni prijemci smlouvy statniho subjektu
                        icos.AddRange(s.Prijemce.Select(m => m.ico).Where(m => !string.IsNullOrEmpty(m)).Distinct());

                        lock (lockObj)
                        {
                            foreach (var ico in icos)
                            {
                                if (vazbyNaPolitiky.SoukromeFirmy.ContainsKey(ico) || sponzorujiciFirmy.Any(m => m.IcoDarce == ico))
                                {
                                    if (!uradySoukr.ContainsKey(objednatelIco))
                                    {
                                        uradySoukr.Add(objednatelIco, new Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > >());
                                        uradySoukr[objednatelIco].Ico = objednatelIco;
                                    }
                                    uradySoukr[objednatelIco].Add(1, s.CalculatedPriceWithVATinCZK);
                                    if (!uradySoukr[objednatelIco].Detail.Any(m => m.Item == ico))
                                    {
                                        uradySoukr[objednatelIco].Detail.Add(new Analysis.BasicData <string>()
                                        {
                                            Item = ico, CelkemCena = s.CalculatedPriceWithVATinCZK, Pocet = 1
                                        });
                                    }
                                    else
                                    {
                                        var item = uradySoukr[objednatelIco].Detail.First(m => m.Item == ico);
                                        item.Add(1, s.CalculatedPriceWithVATinCZK);
                                    }
                                }
                                else if (vazbyNaPolitiky.StatniFirmy.ContainsKey(ico))
                                {
                                    if (!uradyStatni.ContainsKey(objednatelIco))
                                    {
                                        uradyStatni.Add(objednatelIco, new Analysis.BasicDataForSubject <List <Analysis.BasicData <string> > >());
                                        uradyStatni[objednatelIco].Ico = objednatelIco;
                                    }
                                    uradyStatni[objednatelIco].Add(1, s.CalculatedPriceWithVATinCZK);
                                    if (!uradyStatni[objednatelIco].Detail.Any(m => m.Item == ico))
                                    {
                                        uradyStatni[objednatelIco].Detail.Add(new Analysis.BasicData <string>()
                                        {
                                            Item = ico, CelkemCena = s.CalculatedPriceWithVATinCZK, Pocet = 1
                                        });
                                    }
                                    else
                                    {
                                        var item = uradyStatni[objednatelIco].Detail.First(m => m.Item == ico);
                                        item.Add(1, s.CalculatedPriceWithVATinCZK);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    HlidacStatu.Util.Consts.Logger.Error("ERROR UradyObchodujiciSFirmami_s_vazbouNaPolitiky", e);
                }

                end:
                return(new Devmasters.Batch.ActionOutputData()
                {
                    CancelRunning = false, Log = null
                });
            }, null,
                                                                    showProgress ? Devmasters.Batch.Manager.DefaultOutputWriter : (Action <string>)null,
                                                                    showProgress ? new Devmasters.Batch.ActionProgressWriter().Write : (Action <ActionProgressData>)null
                                                                    , true
                                                                    , prefix: "UradyObchodujiciSFirmami_s_vazbouNaPolitiky " + aktualnost.ToNiceDisplayName()
                                                                    );


            AnalysisCalculation.VazbyFiremNaUradyStat ret = new VazbyFiremNaUradyStat();
            ret.SoukromeFirmy = uradySoukr
                                .Where(m => m.Value.Pocet > 0)
                                .Select(kv => kv.Value)
                                .OrderByDescending(o => o.Pocet);

            return(ret);
        }
示例#12
0
        private static QueryContainer BuildQueryContainerForText(string value, QueryDescriptor <T> queryDescriptor, QueryExpression <T> expressionAndValues, QueryContainer queryContainer)
        {
            var stringValue       = Convert.ToString(value);
            var useFullTextSearch = false;

            if (expressionAndValues.MetaData != null &&
                expressionAndValues.MetaData.UseFullTextSearch != null)
            {
                useFullTextSearch = Convert.ToBoolean(expressionAndValues.MetaData.UseFullTextSearch);
            }

            if (queryContainer == null)
            {
                if (useFullTextSearch)
                {
                    queryContainer =
                        queryDescriptor.Match(m => m.OnField(expressionAndValues.ObjectPath)
                                              .Query(Convert.ToString(stringValue)));
                }
                else
                {
                    queryContainer =
                        queryDescriptor.MatchPhrase(m => m.OnField(expressionAndValues.ObjectPath)
                                                    .Query(Convert.ToString(stringValue)));
                }
            }
            else
            {
                if (useFullTextSearch)
                {
                    queryContainer = queryDescriptor ||
                                     queryDescriptor.MatchPhrase(m => m.OnField(expressionAndValues.ObjectPath)
                                                                 .Query(Convert.ToString(stringValue)));
                }
                else
                {
                    queryContainer = queryDescriptor ||
                                     queryDescriptor.Match(m => m.OnField(expressionAndValues.ObjectPath)
                                                           .Query(Convert.ToString(stringValue)));
                }
            }
            return(queryContainer);
        }
示例#13
0
        public Dictionary <string, object[]> TraCuuLog(string term, string site_id, long date_start, long date_end, int page, out long total_recs, int page_size = 50)
        {
            List <QueryContainer> must_key   = new List <QueryContainer>();
            List <QueryContainer> must_ade   = new List <QueryContainer>();
            List <QueryContainer> should_ade = new List <QueryContainer>();

            must_key.Add(new TermQuery()
            {
                Field = "pro.type",
                Value = "key"
            });
            must_ade.Add(new TermQuery()
            {
                Field = "pro.type",
                Value = "ade"
            });

            if (!string.IsNullOrEmpty(term))
            {
                must_key.Add(new QueryStringQuery
                {
                    Fields = "n",
                    Query  = term
                });
            }

            if (!string.IsNullOrEmpty(site_id))
            {
                must_key.Add(new TermQuery()
                {
                    Field = "pro.dm",
                    Value = site_id
                });
            }
            if (date_start > 0)
            {
                must_key.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
                must_ade.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
            }

            if (date_end > 0)
            {
                must_key.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
                must_ade.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
            }
            var qc = new QueryContainer(
                new BoolQuery()
            {
                Must = must_key
            }
                );

            var re = client.Search <dynamic>(x => x.Query(q => qc).Sort(so => so.Descending("ext.sm.c").Descending("sum.co.s")).From((page - 1) * page_size).Size(page_size));

            total_recs = re.Total;
            var hs = new HashSet <string>();

            foreach (var item in re.Documents)
            {
                var n = Convert.ToString(((Dictionary <string, object>)item)["n"]).ToLower();

                if (hs.Add(n))
                {
                    should_ade.Add(new QueryStringQuery
                    {
                        Fields = "prox.xkj",
                        Query  = string.Format("\"{0}\"", ((Dictionary <string, object>)item)["n"])
                    });
                }
            }
            var qc_ade = new QueryContainer(
                new BoolQuery()
            {
                Must = must_ade, Should = should_ade, MinimumShouldMatch = 1
            }
                );
            var re_ade = client.Search <dynamic>(s => s.Query(q => qc_ade).Size(999999));

            var dic_tu_khoa_tim_kiem = new Dictionary <string, object[]>();

            foreach (var item in re.Documents)
            {
                JObject json_item = JObject.Parse(JsonConvert.SerializeObject(item));
                if (json_item != null)
                {
                    var name          = json_item["n"].Value <string>().ToLower();
                    var count_request = json_item["ext"]["sm"]["c"].Value <long>();
                    var sum_display   = json_item["sum"]["co"]["s"].Value <long>();
                    if (!dic_tu_khoa_tim_kiem.ContainsKey(name))
                    {
                        dic_tu_khoa_tim_kiem.Add(name, new object[3] {
                            count_request, sum_display, 0
                        });
                    }
                }
            }

            foreach (var item in re_ade.Documents)
            {
                JObject json_item = JObject.Parse(JsonConvert.SerializeObject(item));
                if (json_item != null)
                {
                    var count = json_item["ext"]["s"]["c"].Value <long>();
                    var xkj   = json_item["prox"]["xkj"].Values <string>().ToList();
                    foreach (var kj in xkj)
                    {
                        if (dic_tu_khoa_tim_kiem.ContainsKey(kj.ToLower()))
                        {
                            var ob = dic_tu_khoa_tim_kiem[kj.ToLower()];
                            ob[2] = count;
                        }
                    }
                }
            }
            return(dic_tu_khoa_tim_kiem);
        }
示例#14
0
        public Dictionary <string, double> SumTraCuuLog(string term, long date_start, long date_end)
        {
            Dictionary <string, double> data_sum = new Dictionary <string, double>();
            List <QueryContainer>       must_key = new List <QueryContainer>();
            List <QueryContainer>       must_ade = new List <QueryContainer>();

            must_key.Add(new TermQuery()
            {
                Field = "pro.type",
                Value = "key"
            });
            must_ade.Add(new TermQuery()
            {
                Field = "pro.type",
                Value = "ade"
            });

            if (!string.IsNullOrEmpty(term))
            {
                must_key.Add(new QueryStringQuery
                {
                    Fields = "n",
                    Query  = term
                });

                must_ade.Add(new QueryStringQuery
                {
                    Fields = "prox.xkj",
                    Query  = term
                });
            }

            if (date_start > 0)
            {
                must_key.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
            }

            if (date_end > 0)
            {
                must_key.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
            }
            var qc = new QueryContainer(
                new BoolQuery()
            {
                Must = must_key
            }
                );
            var qc_ade = new QueryContainer(
                new BoolQuery()
            {
                Must = must_ade
            }
                );

            var res_key = client.Search <dynamic>(x => x.Size(0).Aggregations(o => o.Filter("filter_by_key",
                                                                                            c => c.Filter(u => u.Bool(n => n.Must(qc)))
                                                                                            .Aggregations(ch => ch
                                                                                                          .Sum("tong_so_qc", t => t.Field("sum.co.s"))
                                                                                                          .Sum("luot_goi_qc", l => l.Field("ext.sm.c"))))));

            var res_ade = client.Search <dynamic>(x => x.Size(0).Aggregations(o => o.Filter("filter_by_ade",
                                                                                            c => c.Filter(u => u.Bool(n => n.Must(qc_ade)))
                                                                                            .Aggregations(ch => ch
                                                                                                          .Sum("ext.s.c", e => e.Field("ext.s.c"))))));

            // sum khi pro.type = ade, prox.word = "tu khoa"
            var tong_qc_hien_thi = res_ade.Aggregations.Filter("filter_by_ade").Sum("ext.s.c").Value;

            // sum khi pro.type = key, n = "tu_khoa"
            var total_qc = res_key.Aggregations.Filter("filter_by_key").Sum("tong_so_qc").Value;

            var sum_luot_goi_qc = res_key.Aggregations.Filter("filter_by_key").Sum("luot_goi_qc").Value;

            data_sum = new Dictionary <string, double>
            {
                { "tong_qc_hien_thi", Convert.ToDouble(tong_qc_hien_thi) },
                { "tong_qc", Convert.ToDouble(total_qc) },
                { "luot_goi_qc", Convert.ToDouble(sum_luot_goi_qc) },
            };

            return(data_sum);
        }
示例#15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="fields"></param>
        /// <param name="date_start"></param>
        /// <param name="date_end"></param>
        /// <returns></returns>
        public Dictionary <string, Dictionary <string, Dictionary <string, double> > > TraCuuTuKhoa(List <TuKhoa> lst, List <string> fields, long date_start, long date_end)
        {
            Dictionary <string, Dictionary <string, Dictionary <string, double> > > response = new Dictionary <string, Dictionary <string, Dictionary <string, double> > >();
            Dictionary <string, double> data_sum = new Dictionary <string, double>();
            List <QueryContainer>       must     = new List <QueryContainer>();
            List <QueryContainer>       should   = new List <QueryContainer>();

            fields = new List <string> {
                "ext.c.c", "ext.s.c"
            };

            lst.Add(new TuKhoa
            {
                id_chien_dich = "804182626448441344",
                tu_khoa       = "kế toán",
                kieu_doi_sanh = KieuDoiSanh.KHOP_CHINH_XAC
            });
            lst.Add(new TuKhoa
            {
                id_chien_dich = "804182626448441344",
                tu_khoa       = "Nhân Viên",
                kieu_doi_sanh = KieuDoiSanh.KHOP_CHINH_XAC
            });

            lst.Add(new TuKhoa
            {
                id_chien_dich = "799212750336163840",
                tu_khoa       = "nhân viên",
                kieu_doi_sanh = KieuDoiSanh.KHOP_CHINH_XAC
            });

            lst.Add(new TuKhoa
            {
                id_chien_dich = "803887768076812288",
                tu_khoa       = "Nhân Viên Kinh Doanh",
                kieu_doi_sanh = KieuDoiSanh.KHOP_CHINH_XAC
            });

            foreach (var item in lst)
            {
                if (!string.IsNullOrEmpty(item.tu_khoa))
                {
                    switch (item.kieu_doi_sanh)
                    {
                    case KieuDoiSanh.DOI_SANH_RONG:
                        should.Add(new QueryStringQuery
                        {
                            Fields = "prox.xkj",
                            Query  = item.tu_khoa
                        });
                        break;

                    case KieuDoiSanh.KHOP_CHINH_XAC:
                        should.Add(new TermQuery
                        {
                            Field = "prox.xkj.keyword",
                            Value = item.tu_khoa
                        });
                        break;

                    case KieuDoiSanh.KHOP_CUM_TU:
                        item.tu_khoa = $"\"{item.tu_khoa}\"";
                        should.Add(new QueryStringQuery
                        {
                            Fields = "prox.xkj",
                            Query  = item.tu_khoa
                        });
                        break;
                    }
                }
            }

            date_start = 0;
            if (date_start > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
            }

            date_end = 0;
            if (date_end > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
            }
            var qc = new QueryContainer(
                new BoolQuery {
                Should = should, Must = must
            }
                );

            Dictionary <string, AggregationContainer> aggs = new Dictionary <string, AggregationContainer>();

            foreach (var field in fields)
            {
                aggs.Add("agg_avg_" + field, new AggregationContainer
                {
                    Average = new AverageAggregation("avg_" + field, field)
                });
                aggs.Add("agg_sum_" + field, new AggregationContainer
                {
                    Sum = new SumAggregation("sum_" + field, field)
                });
            }

            //var res = client.Search<dynamic>(x => x.Size(0).Aggregations(o => o.Filter("f_ads", c => c.Filter(u => u.Bool(n => n.Must(qc)) && u.Term(t => t.Field("ai").Value("xmads"))).Aggregations(aggs)) && o.Terms("states", st => st.Field("pro.cam").Aggregations(aggs))));
            //var res = client.Search<dynamic>(x => x.Size(0).Aggregations(o =>
            //    o.Terms("data", y => y.Field("ai")) && o.Filter("ads",
            //        w => w.Filter(f => f.Bool(m => m.Must(qc)) && f.Term("ai", "xmads")).Aggregations(aggs))));

            return(response);
        }
示例#16
0
        public Dictionary <string, double> TraCuuTuKhoaV2(string tu_khoa, List <string> fields, KieuDoiSanh kieu_doi_sanh, long date_start, long date_end)
        {
            Dictionary <string, double> data_sum = new Dictionary <string, double>();

            List <QueryContainer> must = new List <QueryContainer>();

            if (!string.IsNullOrEmpty(tu_khoa))
            {
                switch (kieu_doi_sanh)
                {
                case KieuDoiSanh.DOI_SANH_RONG:
                    must.Add(new QueryStringQuery
                    {
                        Fields = "prox.xkj",
                        Query  = tu_khoa
                    });
                    break;

                case KieuDoiSanh.KHOP_CHINH_XAC:
                    must.Add(new TermQuery
                    {
                        Field = "prox.xkj.keyword",
                        Value = tu_khoa
                    });
                    break;

                case KieuDoiSanh.KHOP_CUM_TU:
                    tu_khoa = $"\"{tu_khoa}\"";
                    must.Add(new QueryStringQuery
                    {
                        Fields = "prox.xkj",
                        Query  = tu_khoa
                    });
                    break;
                }
            }

            if (date_start > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
            }

            if (date_end > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
            }
            var qc = new QueryContainer(
                new BoolQuery()
            {
                Must = must
            }
                );

            Dictionary <string, AggregationContainer> aggs = new Dictionary <string, AggregationContainer>();

            foreach (var field in fields)
            {
                aggs.Add("agg_avg_" + field, new AggregationContainer
                {
                    Average = new AverageAggregation("avg_" + field, field)
                });
                aggs.Add("agg_sum_" + field, new AggregationContainer
                {
                    Sum = new SumAggregation("sum_" + field, field)
                });
            }
            var res_joboko = client.Search <dynamic>(x => x.Size(0)
                                                     .Aggregations(o => o.Filter("f_joboko",
                                                                                 c => c.Filter(u => u.Bool(n => n.Must(qc)) &&
                                                                                               u.Term(cc => cc.Field("ai")
                                                                                                      .Value("joboko")))
                                                                                 .Aggregations(aggs))
                                                                   )
                                                     );
            var res_xmads = client.Search <dynamic>(x => x.Size(0)
                                                    .Aggregations(o => o.Filter("f_xmads",
                                                                                c => c.Filter(u => u.Bool(n => n.Must(qc)) &&
                                                                                              u.Term(oo => oo.Field("ai")
                                                                                                     .Value("xmads")))
                                                                                .Aggregations(aggs))));
            var filterAgg_joboko = res_joboko.Aggregations.Filter("f_joboko");
            var filterAgg_xmads  = res_xmads.Aggregations.Filter("f_xmads");

            foreach (var field in fields)
            {
                data_sum.Add("sum_joboko_" + field, Convert.ToDouble(filterAgg_joboko.Sum("agg_sum_" + field)?.Value));
                data_sum.Add("avg_joboko_" + field, Convert.ToDouble(filterAgg_joboko.Average("agg_avg_" + field)?.Value));
                data_sum.Add("sum_xmads_" + field, Convert.ToDouble(filterAgg_xmads.Sum("agg_sum_" + field)?.Value));
                data_sum.Add("avg_xmads_" + field, Convert.ToDouble(filterAgg_xmads.Average("agg_avg_" + field)?.Value));
            }

            return(data_sum);
        }
示例#17
0
        public Dictionary <string, double> TinhTongLog(string ai, string term, long date_start, long date_end)
        {
            Dictionary <string, double> data_sum = new Dictionary <string, double>();
            List <QueryContainer>       must     = new List <QueryContainer>
            {
                new TermQuery()
                {
                    Field = "ai", Value = ai
                }
            };

            if (!string.IsNullOrEmpty(term))
            {
                term = string.Format("\"{0}\"", term);
                must.Add(new QueryStringQuery
                {
                    Fields = "prox.xkj",
                    Query  = term
                });
            }

            if (date_start > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field = "d",
                    GreaterThanOrEqualTo = date_start
                });
            }

            if (date_end > 0)
            {
                must.Add(new LongRangeQuery()
                {
                    Field             = "d",
                    LessThanOrEqualTo = date_end
                });
            }
            var qc = new QueryContainer(
                new BoolQuery()
            {
                Must = must
            }
                );

            var re = client.Search <dynamic>(x => x.Aggregations(o => o.Filter("filter", c => c.Filter(u => u.Bool(n => n.Must(qc)))
                                                                               .Aggregations(a => a.Sum("luot_click", avg => avg.Field("ext.c.c")).Sum("luot_show", sh => sh.Field("ext.s.c"))))).Size(0));

            if (re.IsValid)
            {
                var filterAgg = re.Aggregations.Filter("filter");
                if (filterAgg != null)
                {
                    data_sum = new Dictionary <string, double>
                    {
                        { "luot_click", Convert.ToDouble(filterAgg.Sum("luot_click")?.Value) },
                        { "luot_show", Convert.ToDouble(filterAgg.Sum("luot_show")?.Value) },
                    };
                }
            }
            return(data_sum);
        }
示例#18
0
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var sfq = ctx.Source.GetAppFilter();

            if (sfq == null)
            {
                return(Task.CompletedTask);
            }

            var allowedOrganizations = sfq.Organizations.Where(o => o.HasPremiumFeatures || (!o.HasPremiumFeatures && !sfq.UsesPremiumFeatures)).ToList();

            if (allowedOrganizations.Count == 0)
            {
                ctx.Filter &= Query <T> .Term(_organizationIdFieldName, "none");

                return(Task.CompletedTask);
            }

            var    index = ctx.Options.GetElasticIndex();
            bool   shouldApplyRetentionFilter = ShouldApplyRetentionFilter(index);
            string field = shouldApplyRetentionFilter ? GetDateField(index) : null;

            if (sfq.Stack != null)
            {
                var stackIdFieldName = typeof(T) == typeof(Stack) ? "id" : _stackIdFieldName;
                var organization     = allowedOrganizations.SingleOrDefault(o => o.Id == sfq.Stack.OrganizationId);
                if (organization != null)
                {
                    if (shouldApplyRetentionFilter)
                    {
                        ctx.Filter &= (Query <T> .Term(stackIdFieldName, sfq.Stack.Id) && GetRetentionFilter <T>(field, organization, _options.MaximumRetentionDays, sfq.Stack.FirstOccurrence));
                    }
                    else
                    {
                        ctx.Filter &= Query <T> .Term(stackIdFieldName, sfq.Stack.Id);
                    }
                }
                else
                {
                    ctx.Filter &= Query <T> .Term(stackIdFieldName, "none");
                }

                return(Task.CompletedTask);
            }

            QueryContainer container = null;

            if (sfq.Projects?.Count > 0)
            {
                var allowedProjects = sfq.Projects.ToDictionary(p => p, p => allowedOrganizations.SingleOrDefault(o => o.Id == p.OrganizationId)).Where(kvp => kvp.Value != null).ToList();
                if (allowedProjects.Count > 0)
                {
                    foreach (var project in allowedProjects)
                    {
                        if (shouldApplyRetentionFilter)
                        {
                            container |= (Query <T> .Term(_projectIdFieldName, project.Key.Id) && GetRetentionFilter <T>(field, project.Value, _options.MaximumRetentionDays, project.Key.CreatedUtc.SafeSubtract(TimeSpan.FromDays(3))));
                        }
                        else
                        {
                            container |= Query <T> .Term(_projectIdFieldName, project.Key.Id);
                        }
                    }

                    ctx.Filter &= container;
                    return(Task.CompletedTask);
                }

                ctx.Filter &= (Query <T> .Term(_projectIdFieldName, "none"));
                return(Task.CompletedTask);
            }

            foreach (var organization in allowedOrganizations)
            {
                if (shouldApplyRetentionFilter)
                {
                    container |= (Query <T> .Term(_organizationIdFieldName, organization.Id) && GetRetentionFilter <T>(field, organization, _options.MaximumRetentionDays));
                }
                else
                {
                    container |= Query <T> .Term(_organizationIdFieldName, organization.Id);
                }
            }

            ctx.Filter &= container;
            return(Task.CompletedTask);
        }
示例#19
0
        public Selector <T> In <TValue>(Expression <Func <T, object> > selector, TValue[] values)
        {
            queryContainer = queryContainer && +Wrap(selector, (w, r) => r.Terms(a => a.Field(w).Terms(values)));

            return(this);
        }
示例#20
0
 public override void Convert(dynamic jsonItem, ref QueryContainer query)
 {
     query.Server = jsonItem.Value;
 }
示例#21
0
 public Selector <T> Lt(Expression <Func <T, object> > selector, double?value)
 {
     queryContainer = queryContainer && +Wrap(selector, (w, r) => r.Range(a => a.Field(w).LessThan(value)));
     return(this);
 }
示例#22
0
 public static DeleteByQueryDescriptor <T> FilterOn <T>(this DeleteByQueryDescriptor <T> deleteDescriptor, QueryContainer container) where T : class
 {
     return(deleteDescriptor.Query(q => container));
 }
示例#23
0
 public Selector <T> Le(Expression <Func <T, object> > selector, DateTime?value)
 {
     queryContainer = queryContainer && +Wrap(selector, (w, r) => r.DateRange(a => a.Field(w).LessThanOrEquals(value)));
     return(this);
 }
示例#24
0
        public static void HowToFilterByDistance(QueryContainer qc)
        {
            qc = qc && new GeoBoundingBoxQuery()
            {
                Field       = "name",
                BoundingBox = new BoundingBox()
                {
                    TopLeft     = new GeoLocation(212, 32),
                    BottomRight = new GeoLocation(43, 56)
                }
            };
            qc = qc && new GeoDistanceQuery()
            {
                Field    = "Field Name",
                Location = new GeoLocation(32, 43),
            };
            qc = qc && new GeoShapeCircleQuery()
            {
                Field = "name",
                Shape = new CircleGeoShape()
                {
                    Coordinates = new GeoCoordinate(324, 535)
                },
                Relation = GeoShapeRelation.Within
            };
            qc = qc && new GeoShapePolygonQuery()
            {
                Field = "xx",
                Shape = new PolygonGeoShape()
                {
                    Coordinates = new List <IEnumerable <GeoCoordinate> >()
                    {
                        new List <GeoCoordinate>()
                        {
                        }
                    }
                },
                Relation = GeoShapeRelation.Within
            };
            qc = qc && new GeoDistanceQuery()
            {
                Field    = "Location",
                Location = new GeoLocation(32, 43),
                Distance = Distance.Kilometers(1)
            };
            qc &= new GeoShapeEnvelopeQuery()
            {
                Field = "Location",
                Shape = new EnvelopeGeoShape(new List <GeoCoordinate>()
                {
                }),
                Relation = GeoShapeRelation.Intersects,
            };
            qc &= new GeoShapePointQuery()
            {
                Field    = "Location",
                Shape    = new PointGeoShape(new GeoCoordinate(32, 32)),
                Relation = GeoShapeRelation.Intersects
            };
            qc &= new GeoShapeMultiPolygonQuery()
            {
                Field = "location",
                Shape = new MultiPolygonGeoShape(new List <List <List <GeoCoordinate> > >()
                {
                })
                {
                },
                Relation = GeoShapeRelation.Intersects
            };
            //使用场景:一个销售区域支持多个配送闭环范围,查询当前位置在不在配送范围内
            var model = new
            {
                nested_sales_area = new object[]
                {
                    new
                    {
                        cordinates = new List <GeoCoordinate>()
                        {
                        }
                    },
                    new
                    {
                        cordinates = new List <GeoCoordinate>()
                        {
                        }
                    },
                }
            };
            var nested_query = new QueryContainer();

            nested_query &= new GeoShapePointQuery()
            {
                Field    = "nested_sales_area.cordinates",
                Shape    = new PointGeoShape(new GeoCoordinate(32, 32)),
                Relation = GeoShapeRelation.Intersects
            };
            qc &= new NestedQuery()
            {
                Path  = "nested_sales_area",
                Query = nested_query
            };
        }
示例#25
0
            private static ISearchResponse <Lib.Data.Smlouva> _coreSearch(QueryContainer query, int page, int pageSize,
                                                                          OrderResult order,
                                                                          AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation = null,
                                                                          bool?platnyZaznam     = null, bool includeNeplatne    = false, bool logError = true,
                                                                          bool withHighlighting = false, bool exactNumOfResults = false)
            {
                page = page - 1;
                if (page < 0)
                {
                    page = 0;
                }

                if (page * pageSize >= MaxResultWindow) //elastic limit
                {
                    page = 0; pageSize = 0;             //return nothing
                }

                AggregationContainerDescriptor <Lib.Data.Smlouva> baseAggrDesc = null;

                baseAggrDesc = anyAggregation == null ?
                               null //new AggregationContainerDescriptor<HlidacStatu.Lib.Data.Smlouva>().Sum("sumKc", m => m.Field(f => f.CalculatedPriceWithVATinCZK))
                        : anyAggregation;

                Func <AggregationContainerDescriptor <Lib.Data.Smlouva>, AggregationContainerDescriptor <Lib.Data.Smlouva> > aggrFunc
                    = (aggr) => { return(baseAggrDesc); };

                ISearchResponse <Lib.Data.Smlouva> res = null;

                try
                {
                    var client = Lib.ES.Manager.GetESClient();
                    if (platnyZaznam.HasValue && platnyZaznam == false)
                    {
                        client = Lib.ES.Manager.GetESClient_Sneplatne();
                    }
                    Indices indexes = client.ConnectionSettings.DefaultIndex;
                    if (includeNeplatne)
                    {
                        indexes = ES.Manager.defaultIndexName_SAll;
                    }

                    res = client
                          .Search <Lib.Data.Smlouva>(s => s
                                                     .Index(indexes)
                                                     .Size(pageSize)
                                                     .From(page * pageSize)
                                                     .Query(q => query)
                                                     .Source(m => m.Excludes(e => e.Field(o => o.Prilohy)))
                                                     .Sort(ss => GetSort(order))
                                                     .Aggregations(aggrFunc)
                                                     .Highlight(h => Lib.Searching.Tools.GetHighlight <Data.Smlouva>(withHighlighting))
                                                     .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                                     );
                    if (withHighlighting && res.Shards != null && res.Shards.Failed > 0) //if some error, do it again without highlighting
                    {
                        res = client
                              .Search <Lib.Data.Smlouva>(s => s
                                                         .Index(indexes)
                                                         .Size(pageSize)
                                                         .From(page * pageSize)
                                                         .Query(q => query)
                                                         .Source(m => m.Excludes(e => e.Field(o => o.Prilohy)))
                                                         .Sort(ss => GetSort(order))
                                                         .Aggregations(aggrFunc)
                                                         .Highlight(h => Lib.Searching.Tools.GetHighlight <Data.Smlouva>(false))
                                                         .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                                         );
                    }
                }
                catch (Exception e)
                {
                    if (res != null && res.ServerError != null)
                    {
                        Lib.ES.Manager.LogQueryError <Lib.Data.Smlouva>(res);
                    }
                    else
                    {
                        HlidacStatu.Util.Consts.Logger.Error("", e);
                    }
                    throw;
                }

                if (res.IsValid == false && logError)
                {
                    Lib.ES.Manager.LogQueryError <Lib.Data.Smlouva>(res);
                }

                return(res);
            }
示例#26
0
 public static bool ValidateQuery <T>(Nest.ElasticClient client, QueryContainer qc)
     where T : class
 {
     return(ValidateSpecificQueryRaw <T>(client, qc)?.Valid ?? false);
 }
 private void AssertBoolQuery(QueryContainer q, Action <IBoolQuery> assert) =>
 assert(((IQueryContainer)q).Bool);
示例#28
0
 public Selector <T> Where <TProperty>(Expression <Func <T, TProperty> > selector, TProperty value)
 {
     queryContainer = queryContainer && +Wrap(selector, (w, r) => r.Term(w, value));
     return(this);
 }
示例#29
0
        private QueryContainer BuildQuery(SearchParamModel model)
        {
            var temp = new ProductListV2();
            var qc   = new QueryContainer();
            {
                var traderlist = new List <string>();
                if (!ValidateHelper.IsPlumpString(model.province))
                {
                    throw new Exception("缺少区域信息");
                }
                if (ValidateHelper.IsPlumpString(model.trader))
                {
                    if (traderlist.Contains(model.trader))
                    {
                        traderlist.Clear();
                        traderlist.Add(model.trader);
                    }
                    else
                    {
                        traderlist.Clear();
                    }
                }
                if (!ValidateHelper.IsPlumpList(traderlist))
                {
                    traderlist = new List <string>()
                    {
                        "构造一个不可能存在的值"
                    };
                }
                qc = qc && new TermsQuery()
                {
                    Field = nameof(temp.TraderId), Terms = traderlist
                };
            }
            var idlist = new string[] { };

            if (!new string[] { "2", "4" }.Contains(model.CustomerType))
            {
                qc = qc && (!new TermsQuery()
                {
                    Field = nameof(temp.UKey), Terms = idlist
                });
            }
            else
            {
                qc = qc && (!new TermsQuery()
                {
                    Field = nameof(temp.UKey), Terms = idlist
                });
            }
            if (ValidateHelper.IsPlumpString(model.brand))
            {
                var brand_sp = ConvertHelper.GetString(model.brand).Split(',').Where(x => ValidateHelper.IsPlumpString(x)).ToArray();
                qc = qc && new TermsQuery()
                {
                    Field = nameof(temp.BrandId), Terms = brand_sp
                };
            }
            if (ValidateHelper.IsPlumpString(model.catalog))
            {
                qc = qc && (new TermQuery()
                {
                    Field = nameof(temp.PlatformCatalogId), Value = model.catalog
                } ||
                            new TermsQuery()
                {
                    Field = nameof(temp.PlatformCatalogIdList), Terms = new object[] { model.catalog }
                } ||
                            new TermsQuery()
                {
                    Field = nameof(temp.ShowCatalogIdList), Terms = new object[] { model.catalog }
                });
            }
            if (model.min_price >= 0)
            {
                qc = qc && new NumericRangeQuery()
                {
                    Field = nameof(temp.SalesPrice), GreaterThanOrEqualTo = (double)model.min_price
                };
            }
            if (model.max_price >= 0)
            {
                qc = qc && new NumericRangeQuery()
                {
                    Field = nameof(temp.SalesPrice), LessThanOrEqualTo = (double)model.max_price
                };
            }

            new GeoDistanceQuery()
            {
            };
            qc = qc && new GeoDistanceRangeQuery()
            {
                Field             = "Location",
                Location          = new GeoLocation(32, 43),
                LessThanOrEqualTo = Distance.Kilometers(1)
            };

            try
            {
                if (!ValidateHelper.IsPlumpString(model.attr))
                {
                    model.attr = "[]";
                }
                var attr_list = model.attr.JsonToEntity <List <AttrParam> >();

                /*
                 * if (ValidateHelper.IsPlumpList(attr_list))
                 * {
                 *  var attr_query = new QueryContainer();
                 *  foreach (var attr in attr_list)
                 *  {
                 *      attr_query = attr_query || new TermQuery() { Field = $"{nameof(template.ProductAttributes)}.{attr.UID}", Value = attr.value };
                 *  }
                 *  qc = qc && new NestedQuery() { Path = nameof(template.ProductAttributes), Query = attr_query };
                 * }
                 */
                if (ValidateHelper.IsPlumpList(attr_list))
                {
                    //qc = qc && new TermsQuery() { Field = nameof(temp.ProductAttributes), Terms = attr_list.Select(attr => $"{attr.UID}@$@{attr.value}") };
                    foreach (var attr_key in attr_list.Select(x => x.UID).Distinct())
                    {
                        qc = qc && new TermsQuery()
                        {
                            Field = nameof(temp.ProductAttributes), Terms = attr_list.Where(x => x.UID == attr_key).Select(attr => $"{attr.UID}@$@{attr.value}")
                        };
                    }
                }
            }
            catch { }
            if (model.isGroup)
            {
                qc = qc && new TermQuery()
                {
                    Field = nameof(temp.IsGroup), Value = 1
                };
            }
            if (ValidateHelper.IsPlumpString(model.qs))
            {
                qc = qc && (new MatchQuery()
                {
                    Field = nameof(temp.ShopName), Query = model.qs, Operator = Operator.Or, MinimumShouldMatch = "100%"
                } ||
                            new MatchQuery()
                {
                    Field = nameof(temp.SeachTitle), Query = model.qs, Operator = Operator.Or, MinimumShouldMatch = "100%"
                });
            }

            qc = qc && new TermQuery()
            {
                Field = nameof(temp.PAvailability), Value = 1
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.UpAvailability), Value = 1
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.PIsRemove), Value = 0
            };
            qc = qc && new TermQuery()
            {
                Field = nameof(temp.UpIsRemove), Value = 0
            };
            qc = qc && new NumericRangeQuery()
            {
                Field = nameof(temp.SalesPrice), GreaterThan = 0
            };

            return(qc);
        }
示例#30
0
        public static List <Picture> GetPicturesOfComputer(
            string computerId, string text, string[] tags, DateTime startDate, DateTime endDate, int start = 0, int rows = 10)
        {
            QueryContainer textContainer;
            QueryContainer tagsContainer;

            if (string.IsNullOrEmpty(text))
            {
                textContainer = new MatchAllQuery();
            }
            else
            {
                textContainer = new MatchQuery()
                {
                    Field = "Text", Query = text
                }
            };

            if ((tags == null) || (tags.Length > 0))
            {
                tagsContainer = new MatchAllQuery();
            }
            else
            {
                tagsContainer = new QueryContainer(new MatchQuery()
                {
                    Field = "Tags", Query = tags[0]
                });

                for (int i = 1; i < tags.Length; i++)
                {
                    tagsContainer = tagsContainer && new MatchQuery()
                    {
                        Field = "Tags", Query = tags[i]
                    };
                }
            }


            QueryContainer query = new FilteredQuery()
            {
                Filter = new FilterContainer(
                    new RangeFilter()
                {
                    Field = "Date",
                    GreaterThanOrEqualTo = startDate.ToString("yyyy-MM-ddTHH:mm:ss"),
                    LowerThanOrEqualTo   = endDate.ToString("yyyy-MM-ddTHH:mm:ss")
                }),
                Query = new QueryContainer(
                    new MatchQuery()
                {
                    Field = "ComputerId",
                    Query = computerId
                })
            };

            QueryContainer finalQuery = query && textContainer && tagsContainer;

            ISearchResponse <Picture> response =
                _elasticClient.Search <Picture>(s => s
                                                .Type(PICTURE_OBJECT_TYPE)
                                                .From(start)
                                                .Size(rows)
                                                .SortAscending("Date")
                                                .Query(finalQuery));

            return(response.Documents.ToList());
        }
        public async Task <ActivitiesResult> GetActivities(ActivitiesQuery query)
        {
            var now        = DateTime.UtcNow;
            var today      = now.Date;
            var oneYearAgo = today.AddYears(-1);
            var take       = query.Take ?? 50;
            var from       = query.From ?? oneYearAgo;
            var to         = query.To ?? now;

            if (take > 500)
            {
                take = 500;
            }

            var response = await _client.SearchAsync <Activity>(s => s
                                                                .Query(q =>
            {
                var where = q
                            .Term(t => t
                                  .Field(a => a.AccountId)
                                  .Value(query.AccountId)
                                  )
                            // TODO: temporary filter to remove payments whilst AML-3120 is resolved
                            && !q
                            .Term(t => t
                                  .Field(a => a.Type)
                                  .Value(ActivityType.PaymentCreated))
                            // end of temporary filter
                            && q
                            .DateRange(r => r
                                       .Field(a => a.At)
                                       .GreaterThanOrEquals(DateMath.Anchored(from).RoundTo(TimeUnit.Day))
                                       .LessThanOrEquals(DateMath.Anchored(to).RoundTo(TimeUnit.Day))
                                       );

                if (query.Category != null)
                {
                    where &= q
                             .Terms(t => t
                                    .Field(a => a.Type)
                                    .Terms(query.Category.Value.GetActivityTypes())
                                    );
                }

                if (query.Data != null && query.Data.Any())
                {
                    where &= q.Nested(n => n
                                      .Path(p => p.Data)
                                      .Query(nq =>
                    {
                        QueryContainer nestedWhere = null;

                        foreach (var filter in query.Data)
                        {
                            nestedWhere &= nq
                                           .Term(t => t
                                                 .Field(a => a.Data[filter.Key + ".keyword"])
                                                 .Value(filter.Value)
                                                 );
                        }

                        return(nestedWhere);
                    })
                                      );
                }

                if (!string.IsNullOrWhiteSpace(query.Term))
                {
                    where &= q
                             .MultiMatch(m => m
                                         .Type(TextQueryType.CrossFields)
                                         .Fields(f => f
                                                 .Field(a => a.Description)
                                                 .Field(a => a.Data["*"])
                                                 )
                                         .Query(query.Term)
                                         .Operator(Operator.And)
                                         );
                }

                return(where);
            })
                                                                .Sort(srt => srt
                                                                      .Descending(a => a.At)
                                                                      .Descending("_uid")
                                                                      )
                                                                .Take(take)
                                                                );

            return(new ActivitiesResult
            {
                Activities = response.Documents,
                Total = response.Total
            });
        }
示例#32
0
        public PagerData <CommentEs> QueryCommentFromEs(
            string user_product_id = null, string user_uid = null, string q = null, int page = 1, int pagesize = 10)
        {
            var data     = new PagerData <CommentEs>();
            var client   = ElasticsearchClientManager.Instance.DefaultClient.CreateClient();
            var temp     = new CommentEs();
            var tag_temp = new TagEs();

            var sd = new SearchDescriptor <CommentEs>();

            sd = sd.Index(INDEX_NAME);

            #region where
            var query = new QueryContainer();
            if (ValidateHelper.IsPlumpString(user_product_id))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.UserProductUID), Value = user_product_id
                };
            }
            if (ValidateHelper.IsPlumpString(user_uid))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.UserUID), Value = user_uid
                };
            }
            if (ValidateHelper.IsPlumpString(q))
            {
                query &= new MatchQuery()
                {
                    Field = nameof(temp.Comment), Query = q, Operator = Operator.Or, MinimumShouldMatch = "100%"
                };
            }
            sd = sd.Query(_ => query);
            #endregion

            #region order
            var sort = new SortDescriptor <CommentEs>();
            sort = sort.Descending(x => x.CreateTime);
            sd   = sd.Sort(_ => sort);
            #endregion

            #region aggs
            sd = sd.Aggregations(x => x
                                 .Terms("tags", av => av.Field($"{nameof(temp.Tags)}.{nameof(tag_temp.TagName)}").Size(10))
                                 .Terms("shops", av => av.Field(f => f.TraderUID).Size(10))
                                 .Average("score", av => av.Field(f => f.Score)));
            #endregion

            #region pager
            sd = sd.QueryPage_(page, pagesize);
            #endregion

            var response = client.Search <CommentEs>(_ => sd);
            response.ThrowIfException();

            data.ItemCount = (int)response.Total;
            data.DataList  = response.Documents.ToList();

            var tags_agg  = response.Aggs.Terms("tags");
            var shops_agg = response.Aggs.Terms("shops");
            var score_agg = response.Aggs.Average("score");

            return(data);
        }
示例#33
0
 public MinAggregate(IComparerExpression <T> comparer, QueryContainer container)
     : base(comparer, container)
 {
 }