[U] public void OrIntoBoolWithMustAndShould()
		{
			var queries = new QueryContainer[] { Query };
			CombineBothWays(
				new BoolQuery { Must = queries, Should = queries }, Query
				, q => q.Bool(b => b.Must(c => c.Query()).Should(c => c.Query())), q=> q.Query()
				, l => l.Bool.Should().NotBeNull()
				, r => r.Term.Should().NotBeNull()
				, b => b.Should.Should().NotBeEmpty().And.HaveCount(2)
			);
		}
		private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(QueryContainer leftContainer, QueryContainer rightContainer, out QueryContainer queryContainer)
		{
			queryContainer = null;
			if (leftContainer == null && rightContainer == null) return true;
			var leftWritable = (leftContainer?.IsWritable).GetValueOrDefault(false);
			var rightWritable = (rightContainer?.IsWritable).GetValueOrDefault(false);
			if (leftWritable && rightWritable) return false;
			if (!leftWritable && !rightWritable) return true;

			queryContainer = leftWritable ? leftContainer : rightContainer;
			return true;
		}
Esempio n. 3
1
		internal static QueryContainer CombineAsMust(this QueryContainer leftContainer, QueryContainer rightContainer)
		{
			var leftBool = (leftContainer as IQueryContainer)?.Bool;
			var rightBool = (rightContainer as IQueryContainer)?.Bool;
			if (leftBool == null && rightBool == null)
				return CreateMustContainer(new[] { leftContainer, rightContainer }, null, null);

			if ((leftBool?.Locked).GetValueOrDefault() || (rightBool?.Locked).GetValueOrDefault())
				return CreateMustContainer(new[] { leftContainer, rightContainer }, null, null);

			if (leftBool.HasOnlyShouldClauses() || rightBool.HasOnlyShouldClauses())
				return CreateMustContainer(new[] { leftContainer, rightContainer }, null, null);

			var mustNotClauses = OrphanMustNots(leftContainer).EagerConcat(OrphanMustNots(rightContainer));
			var filterClauses = OrphanFilters(leftContainer).EagerConcat(OrphanFilters(rightContainer));
			var mustClauses = MustClausesOrSelf(leftContainer).EagerConcat(MustClausesOrSelf(rightContainer));

			var container = CreateMustContainer(mustClauses, mustNotClauses, filterClauses);
			return container;
		}
		internal static QueryContainer CombineAsShould(this QueryContainer leftContainer, QueryContainer rightContainer)
		{
			QueryContainer c = null;
			var leftBool = leftContainer.Self()?.Bool;
			var rightBool = rightContainer.Self()?.Bool;
			if (TryFlattenShould(leftContainer, rightContainer, leftBool, rightBool, out c))
				return c;

			var lBoolQuery = leftContainer.Self().Bool;
			var rBoolQuery = rightContainer.Self().Bool;

			var lHasShouldQueries = lBoolQuery != null && lBoolQuery.Should.HasAny();
			var rHasShouldQueries = rBoolQuery != null && rBoolQuery.Should.HasAny();

			var lq = lHasShouldQueries ? lBoolQuery.Should : new[] { leftContainer };
			var rq = rHasShouldQueries ? rBoolQuery.Should : new[] { rightContainer };

			var shouldClauses = lq.EagerConcat(rq);
			return CreateShouldContainer(shouldClauses);
		}
		/// <summary>
		/// Handles cases where either side is a bool which indicates it can't be merged yet the other side is mergable.
		/// A side is considered unmergable if its locked (has important metadata) or has should clauses.
		/// Instead of always wrapping these cases in another bool we merge to unmergable side into to others must clause therefor flattening the generated graph
		/// </summary>
		private static bool TryHandleUnmergableBools(
			QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c)
		{
			c = null;
			var leftCantMergeAnd = leftBool != null && !leftBool.CanMergeAnd();
			var rightCantMergeAnd = rightBool != null && !rightBool.CanMergeAnd();
			if (!leftCantMergeAnd && !rightCantMergeAnd) return false;

			if (leftCantMergeAnd && rightCantMergeAnd)
				c = CreateMustContainer(leftContainer, rightContainer);
			//right can't merge but left can and is a bool so we add left to the must clause of right
			else if (!leftCantMergeAnd && leftBool != null && rightCantMergeAnd)
			{
				leftBool.Must = leftBool.Must.AddIfNotNull(rightContainer);
				c = leftContainer;
			}
			//right can't merge and left is not a bool, we forcefully create a wrapped must container
			else if (!leftCantMergeAnd && leftBool == null && rightCantMergeAnd)
				c = CreateMustContainer(leftContainer, rightContainer);
			//left can't merge but right can and is a bool so we add left to the must clause of right
			else if (leftCantMergeAnd && !rightCantMergeAnd && rightBool != null)
			{
				rightBool.Must = rightBool.Must.AddIfNotNull(leftContainer);
				c = rightContainer;
			}
			//left can't merge and right is not a bool, we forcefully create a wrapped must container
			else if (leftCantMergeAnd && !rightCantMergeAnd && rightBool == null)
				c = CreateMustContainer(new Containers { leftContainer, rightContainer }, null);
			return c != null;
		}
		private static IEnumerable<QueryContainer> MustClausesOrSelf(QueryContainer container)
		{
			var boolQuery = container.Self().Bool;
			if (boolQuery != null && boolQuery.Must.HasAny()) return boolQuery.Must;

			return boolQuery != null && boolQuery.Conditionless ? Enumerable.Empty<QueryContainer>() : new[] {container};
		}
		[U] public void CombindingTwoBools()
		{
			var queries = new QueryContainer[] { Query };
			ReturnsBool(
				new BoolQuery { Must = queries, Should = queries }
					|| new BoolQuery { MustNot = queries, Should = queries }
				, q => q.Bool(b=>b.Must(c=>c.Query()).Should(c=>c.Query()))
					|| q.Bool(b=>b.MustNot(c=>c.Query()).Should(c=>c.Query()))
				, b =>
			{

				b.Should.Should().NotBeEmpty().And.HaveCount(2);
				var first = (IQueryContainer)b.Should.First();
				var last = (IQueryContainer)b.Should.Last();
				first.Bool.Should().NotBeNull();
				last.Bool.Should().NotBeNull();

				var firstBool = first.Bool;
				var lastBool = last.Bool;

				firstBool.Should.Should().NotBeEmpty().And.HaveCount(1);
				firstBool.Must.Should().NotBeEmpty().And.HaveCount(1);

				lastBool.Should.Should().NotBeEmpty().And.HaveCount(1);
				lastBool.MustNot.Should().NotBeEmpty().And.HaveCount(1);
			});
		}
		protected void CombineBothWays(
			QueryContainer ois1,
			QueryContainer ois2,
			Func<QueryContainerDescriptor<Project>, QueryContainer> lambda1,
			Func<QueryContainerDescriptor<Project>, QueryContainer> lambda2,
			Action<IQueryContainer> assertLeft,
			Action<IQueryContainer> assertRight,
			Action<IBoolQuery> assertContainer = null
			)
		{
			var oisLeft = ois1 || ois2;
			Func<QueryContainerDescriptor<Project>, QueryContainer> lambdaLeft = (s) => lambda1(s) || lambda2(s);

			ReturnsBool(oisLeft, lambdaLeft, b =>
			{
				var left = (IQueryContainer)b.Should.First();
				var right = (IQueryContainer)b.Should.Last();
				assertLeft(left);
				assertRight(right);
				assertContainer?.Invoke(b);
			});

			var oisRight = ois2 || ois1;
			Func<QueryContainerDescriptor<Project>, QueryContainer> lambdaRight = (s) => lambda2(s) || lambda1(s);

			ReturnsBool(oisRight, lambdaRight, b =>
			{
				var left = (IQueryContainer)b.Should.First();
				var right = (IQueryContainer)b.Should.Last();
				assertRight(left);
				assertLeft(right);
				assertContainer?.Invoke(b);
			});
		}
		private void ReturnsBool(QueryContainer combined, Action<IBoolQuery> boolQueryAssert)
		{
			combined.Should().NotBeNull();
			IQueryContainer c = combined;
			c.Bool.Should().NotBeNull();
			boolQueryAssert(c.Bool);
		}
		private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(QueryContainer leftContainer, QueryContainer rightContainer, out QueryContainer queryContainer)
		{
			var combined = new[] {leftContainer, rightContainer};
			var any = combined.Any(bf => bf == null || bf.IsConditionless);
			queryContainer = any ? combined.FirstOrDefault(bf => bf != null && !bf.IsConditionless) : null;
			return any;
		}
		internal static QueryContainer Or(QueryContainer leftContainer, QueryContainer rightContainer)
		{
			QueryContainer queryContainer;
			return IfEitherIsEmptyReturnTheOtherOrEmpty(leftContainer, rightContainer, out queryContainer)
				? queryContainer
				: leftContainer.CombineAsShould(rightContainer);
		}
		[U] public void CombiningManyUsingForeachInitializingWithDefault()
		{
			var container = new QueryContainer(); 
			foreach(var i in Enumerable.Range(0, 100))
				container |= Query;
			LotsOfOrs(container);
		}
Esempio n. 13
0
        public static Nest.ISearchResponse <Lib.Data.Smlouva> RawSearch(Nest.QueryContainer query, int page, int pageSize, OrderResult order = OrderResult.Relevance,
                                                                        AggregationContainerDescriptor <Lib.Data.Smlouva> anyAggregation     = null, int?platnyZaznam = null, bool includeNeplatne = false
                                                                        )
        {
            var res = _coreSearch(query, page, pageSize, order, anyAggregation, platnyZaznam: platnyZaznam, includeNeplatne: includeNeplatne, logError: true);

            return(res);
        }
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			if (reader.TokenType != JsonToken.StartObject) return null;
			var container = new QueryContainer();
			serializer.Populate(reader, container);
			var agg = new FilterAggregation();
			agg.Filter = container;
			return agg;
		}
            public static Nest.ISearchResponse <VerejnaZakazka> RawSearch(Nest.QueryContainer query, int page, int pageSize, VerejnaZakazkaSearchData.VZOrderResult order = VerejnaZakazkaSearchData.VZOrderResult.Relevance,
                                                                          AggregationContainerDescriptor <VerejnaZakazka> anyAggregation = null
                                                                          )
            {
                page = page - 1;
                if (page < 0)
                {
                    page = 0;
                }


                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); };



                var     client  = Lib.ES.Manager.GetESClient();
                Indices indexes = client.ConnectionSettings.DefaultIndex;


                var res = client
                          .Search <VerejnaZakazka>(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((int)order))
                                                   .Aggregations(aggrFunc)

                                                   );

                if (res.IsValid == false)
                {
                    Lib.ES.Manager.LogQueryError <VerejnaZakazka>(res);
                }

                //Audit.Add(Audit.Operations.Search, "", "", "VerejnaZakazka", res.IsValid ? "valid" : "invalid", query., null);

                return(res);
            }
		internal static QueryContainer CombineAsShould(this QueryContainer leftContainer, QueryContainer rightContainer)
		{
			if (!leftContainer.CanMergeShould() || !leftContainer.CanMergeShould())
				return CreateShouldContainer(new List<QueryContainer> { leftContainer, rightContainer });

			var lBoolQuery = leftContainer.Self().Bool;
			var rBoolQuery = rightContainer.Self().Bool;

			var lHasShouldQueries = lBoolQuery != null && lBoolQuery.Should.HasAny();
			var rHasShouldQueries = rBoolQuery != null && rBoolQuery.Should.HasAny();

			var lq = lHasShouldQueries ? lBoolQuery.Should : new[] { leftContainer };
			var rq = rHasShouldQueries ? rBoolQuery.Should : new[] { rightContainer };

			var shouldClauses = lq.EagerConcat(rq);
			return CreateShouldContainer(shouldClauses);
		}
		private static bool TryFlattenShould(
			QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c)
		{
			c = null;
			var leftCanMerge = leftContainer.CanMergeShould();
			var rightCanMerge = rightContainer.CanMergeShould();
			if (!leftCanMerge && !rightCanMerge) c = CreateShouldContainer(new Containers { leftContainer, rightContainer });

			//left can merge but right's bool can not instead of wrapping into a new bool we inject the whole bool into left
			else if (leftCanMerge && !rightCanMerge && rightBool != null)
			{
				leftBool.Should = leftBool.Should.AddIfNotNull(rightContainer);
				c = leftContainer;
			}
			else if (rightCanMerge && !leftCanMerge && leftBool != null)
			{
				rightBool.Should = rightBool.Should.AddIfNotNull(leftContainer);
				c = rightContainer;
			}
			return c != null;

		}
		internal static QueryContainer CombineAsMust(this QueryContainer leftContainer, QueryContainer rightContainer)
		{
			QueryContainer c;
			var leftBool = leftContainer.Self()?.Bool;
			var rightBool = rightContainer.Self()?.Bool;

			//neither side is a bool, no special handling needed wrap in a bool must
			if (leftBool == null && rightBool == null)
				return CreateMustContainer(new Containers { leftContainer, rightContainer }, null);

			else if (TryHandleBoolsWithOnlyShouldClauses(leftContainer, rightContainer, leftBool, rightBool, out c)) return c;
			else if (TryHandleUnmergableBools(leftContainer, rightContainer, leftBool, rightBool, out c)) return c;

			//neither side is unmergable so neither is a bool with should clauses

			var mustNotClauses = OrphanMustNots(leftContainer).EagerConcat(OrphanMustNots(rightContainer));
			var filterClauses = OrphanFilters(leftContainer).EagerConcat(OrphanFilters(rightContainer));
			var mustClauses = OrphanMusts(leftContainer).EagerConcat(OrphanMusts(rightContainer));

			var container = CreateMustContainer(mustClauses, mustNotClauses, filterClauses);
			return container;
		}
		/// <summary>
		/// Both Sides are bools, but one of them has only should clauses so we should wrap into a new container.
		/// Unless we know one of the sides is a bool with only a must who's clauses are all bools with only shoulds.
		/// This is a piece of metadata we set at the bools creation time so we do not have to itterate the clauses on each combination
		/// In this case we can optimize the generated graph by merging and preventing stack overflows
		/// </summary>
		private static bool TryHandleBoolsWithOnlyShouldClauses(
			QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c)
		{
			c = null;
			var leftHasOnlyShoulds = leftBool.HasOnlyShouldClauses();
			var rightHasOnlyShoulds = rightBool.HasOnlyShouldClauses();
			if (!leftHasOnlyShoulds && !rightHasOnlyShoulds) return false;
			if (leftContainer.HoldsOnlyShouldMusts && rightHasOnlyShoulds)
			{
				leftBool.Must = leftBool.Must.AddIfNotNull(rightContainer);
				c = leftContainer;
			}
			else if (rightContainer.HoldsOnlyShouldMusts && leftHasOnlyShoulds)
			{
				rightBool.Must = rightBool.Must.AddIfNotNull(leftContainer);
				c = rightContainer;
			}
			else
			{
				c = CreateMustContainer(new Containers { leftContainer, rightContainer }, null);
				c.HoldsOnlyShouldMusts = rightHasOnlyShoulds && leftHasOnlyShoulds;
			}
			return true;
		}
Esempio n. 20
0
        public ElasticsearchDynamicValue[] Search(ElasticDocument objectToSearch, SearchCriteria p_criteria)
        //public IEnumerable<ElasticDocument> Search(ElasticDocument objectToSearch)
        {
            ElasticsearchDynamicValue[] response = null;
            QueryContainer queryContainer = new QueryContainer();

           /*
            var queryMatchName = new MatchQueryDescriptor<ElasticDocument>().OnField("_search.name").Query(objectToSearch.Search.Name);
            var queryMatchDescription = new MatchQueryDescriptor<ElasticDocument>().OnField("_search.description").Query(objectToSearch.Search.Description);
            var queryMatchOwner = new MatchQueryDescriptor<ElasticDocument>().OnField("_search.owner").Query(objectToSearch.Search.Owner);

            var queryTermName = new TermQueryDescriptor<ElasticDocument>().OnField("_search.name").Value(objectToSearch.Search.Name);
            var queryTermDescription = new TermQueryDescriptor<ElasticDocument>().OnField("_search.description").Value(objectToSearch.Search.Description);
            var queryTermOwner = new TermQueryDescriptor<ElasticDocument>().OnField("_search.owner").Value(objectToSearch.Search.Owner);
            */

            if (p_criteria.Condition.Equals(SHOULD) && p_criteria.QueryType.Equals(MATCH))
                queryContainer = new QueryDescriptor<ElasticDocument>().Bool(b => b.
                            Should(s => s.Match(m => m.OnField("_search.name").Query(objectToSearch.Search.Name)) ||
                                        s.Match(m => m.OnField("_search.description").Query(objectToSearch.Search.Description)) ||
                                        s.Match(m => m.OnField("_search.owner").Query(objectToSearch.Search.Owner))));

            if (p_criteria.Condition.Equals(MUST) && p_criteria.QueryType.Equals(MATCH))
                queryContainer = new QueryDescriptor<ElasticDocument>().Bool(b => b.
                            Must(s => s.Match(m => m.OnField("_search.name").Query(objectToSearch.Search.Name)) &&
                                      s.Match(m => m.OnField("_search.description").Query(objectToSearch.Search.Description)) &&
                                      s.Match(m => m.OnField("_search.owner").Query(objectToSearch.Search.Owner))));

            if (p_criteria.Condition.Equals(SHOULD) && p_criteria.QueryType.Equals(TERM))
                queryContainer = new QueryDescriptor<ElasticDocument>().Bool(b => b.
                        Should(s => s.Term(m => m.OnField("_search.name").Value(objectToSearch.Search.Name)) ||
                                    s.Term(m => m.OnField("_search.description").Value(objectToSearch.Search.Description)) ||
                                    s.Term(m => m.OnField("_search.owner").Value(objectToSearch.Search.Owner))));

            if (p_criteria.Condition.Equals(MUST) && p_criteria.QueryType.Equals(TERM))
                queryContainer = new QueryDescriptor<ElasticDocument>().Bool(b => b.
                            Must(s => s.Term(m => m.OnField("_search.name").Value(objectToSearch.Search.Name)) &&
                                      s.Term(m => m.OnField("_search.description").Value(objectToSearch.Search.Description)) &&
                                      s.Term(m => m.OnField("_search.owner").Value(objectToSearch.Search.Owner))));
            
            var searchDescriptor = new SearchDescriptor<ElasticDocument>().
                Query(queryContainer);

            var request = clientElasticNest.Serializer.Serialize(searchDescriptor);

            var results = clientElasticNest.Raw.Search(request);

            int total_hits = (int)results.Response[HITS][TOTAL_HITS];
            ElasticsearchDynamicValue hits = results.Response[HITS][HITS];

            if (total_hits > 0)
                response = new ElasticsearchDynamicValue[total_hits];
            else
                response = new ElasticsearchDynamicValue[0];

            for (int i = 0; i < total_hits; i++)
            {
                response[i] = hits[i];
            }

            return response;
            
        }
		protected void ReturnsBool(QueryContainer combined, Func<QueryContainerDescriptor<Project>, QueryContainer> selector, Action<IBoolQuery> boolQueryAssert)
		{
			ReturnsBool(combined, boolQueryAssert);
			ReturnsBool(selector.Invoke(new QueryContainerDescriptor<Project>()), boolQueryAssert);
		}
		[U] public void OrIntoNamedBool()
		{
			var queries = new QueryContainer[] { Query };
			CombineBothWays(
				new BoolQuery { Should = queries, Name = "name" }, Query
				, q => q.Bool(b => b.Should(c => c.Query()).Name("name")), q=> q.Query()
				, l => {
					l.Bool.Should().NotBeNull();
					l.Bool.Should.Should().NotBeNullOrEmpty();
					l.Bool.Name.Should().Be("name");
				}
				, r => r.Term.Should().NotBeNull()
				, b => b.Should.Should().NotBeEmpty().And.HaveCount(2)
			);
		}
Esempio n. 23
0
 /// <summary>
 /// Filter search
 /// </summary>
 public SearchDescriptor <T> PostFilter(QueryContainer filter) => Assign(a => {
     filter.ThrowIfNull(nameof(filter));
     a.PostFilter = filter;
 });
		private IQueryContainer AndAssignManyBoolQueries(QueryContainer q)
		{
			var container = new QueryContainer();
			System.Action act = () =>
			{
				for (int i = 0; i < Iterations; i++) container &= q;
			};
			act.ShouldNotThrow();
			return container;
		}
        /// <summary>
        /// Both Sides are bools, but one of them has only should clauses so we should wrap into a new container.
        /// Unless we know one of the sides is a bool with only a must who's clauses are all bools with only shoulds.
        /// This is a piece of metadata we set at the bools creation time so we do not have to itterate the clauses on each combination
        /// In this case we can optimize the generated graph by merging and preventing stack overflows
        /// </summary>
        private static bool TryHandleBoolsWithOnlyShouldClauses(
            QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c
            )
        {
            c = null;
            var leftHasOnlyShoulds  = leftBool.HasOnlyShouldClauses();
            var rightHasOnlyShoulds = rightBool.HasOnlyShouldClauses();

            if (!leftHasOnlyShoulds && !rightHasOnlyShoulds)
            {
                return(false);
            }

            if (leftContainer.HoldsOnlyShouldMusts && rightHasOnlyShoulds)
            {
                leftBool.Must = leftBool.Must.AddIfNotNull(rightContainer);
                c             = leftContainer;
            }
            else if (rightContainer.HoldsOnlyShouldMusts && leftHasOnlyShoulds)
            {
                rightBool.Must = rightBool.Must.AddIfNotNull(leftContainer);
                c = rightContainer;
            }
            else
            {
                c = CreateMustContainer(new Containers {
                    leftContainer, rightContainer
                });
                c.HoldsOnlyShouldMusts = rightHasOnlyShoulds && leftHasOnlyShoulds;
            }
            return(true);
        }
 public static bool IsConditionless(this QueryContainer q) => q == null || q.IsConditionless;
Esempio n. 27
0
        private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(QueryContainer leftContainer, QueryContainer rightContainer, out QueryContainer queryContainer)
        {
            var combined = new[] { leftContainer, rightContainer };
            var anyEmpty = combined.Any(q => q == null || !q.IsWritable);

            queryContainer = anyEmpty ? combined.FirstOrDefault(q => q != null && q.IsWritable) : null;
            return(anyEmpty);
        }
        internal static QueryContainer CombineAsMust(this QueryContainer leftContainer, QueryContainer rightContainer)
        {
            var leftBool  = (leftContainer as IQueryContainer)?.Bool;
            var rightBool = (rightContainer as IQueryContainer)?.Bool;

            if (leftBool == null && rightBool == null)
            {
                return(CreateMustContainer(new[] { leftContainer, rightContainer }, null, null));
            }

            if ((leftBool?.Locked).GetValueOrDefault() || (rightBool?.Locked).GetValueOrDefault())
            {
                return(CreateMustContainer(new[] { leftContainer, rightContainer }, null, null));
            }

            if (leftBool.HasOnlyShouldClauses() || rightBool.HasOnlyShouldClauses())
            {
                return(CreateMustContainer(new[] { leftContainer, rightContainer }, null, null));
            }

            var mustNotClauses = OrphanMustNots(leftContainer).EagerConcat(OrphanMustNots(rightContainer));
            var filterClauses  = OrphanFilters(leftContainer).EagerConcat(OrphanFilters(rightContainer));
            var mustClauses    = MustClausesOrSelf(leftContainer).EagerConcat(MustClausesOrSelf(rightContainer));

            var container = CreateMustContainer(mustClauses, mustNotClauses, filterClauses);

            return(container);
        }
Esempio n. 29
0
		public static QueryContainer CreateReturnQuery(Action<QueryContainer, BoolBaseQueryDescriptor> modify = null)
		{
			var returnQuery = new QueryContainer();
			var returnBoolQuery = new BoolBaseQueryDescriptor() { };
			((IQueryContainer)returnQuery).Bool = returnBoolQuery;
			if (modify != null)
			{
				modify(returnQuery, returnBoolQuery);
			}
			return returnQuery;
		}
 private static IQueryContainer Self(this QueryContainer q) => q;
        internal static QueryContainer CombineAsShould(this QueryContainer leftContainer, QueryContainer rightContainer)
        {
            if ((!leftContainer.CanMergeShould() || !rightContainer.CanMergeShould()))
            {
                return(CreateShouldContainer(new List <QueryContainer> {
                    leftContainer, rightContainer
                }));
            }

            var lBoolQuery = leftContainer.Self().Bool;
            var rBoolQuery = rightContainer.Self().Bool;

            var lHasShouldQueries = lBoolQuery != null && lBoolQuery.Should.HasAny();
            var rHasShouldQueries = rBoolQuery != null && rBoolQuery.Should.HasAny();

            var lq = lHasShouldQueries ? lBoolQuery.Should : new[] { leftContainer };
            var rq = rHasShouldQueries ? rBoolQuery.Should : new[] { rightContainer };

            var shouldClauses = lq.EagerConcat(rq);

            return(CreateShouldContainer(shouldClauses));
        }
Esempio n. 32
0
 public void Add(string name, QueryContainer filter) => BackingDictionary.Add(name, filter);
        private static bool TryFlattenShould(
            QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c
            )
        {
            c = null;
            var leftCanMerge  = leftContainer.CanMergeShould();
            var rightCanMerge = rightContainer.CanMergeShould();

            if (!leftCanMerge && !rightCanMerge)
            {
                c = CreateShouldContainer(new Containers {
                    leftContainer, rightContainer
                });
            }

            //left can merge but right's bool can not instead of wrapping into a new bool we inject the whole bool into left
            else if (leftCanMerge && !rightCanMerge && rightBool != null)
            {
                leftBool.Should = leftBool.Should.AddIfNotNull(rightContainer);
                c = leftContainer;
            }
            else if (rightCanMerge && !leftCanMerge && leftBool != null)
            {
                rightBool.Should = rightBool.Should.AddIfNotNull(leftContainer);
                c = rightContainer;
            }
            return(c != null);
        }
		internal static QueryContainer CreateShouldContainer(IList<IQueryContainer> shouldClauses)
		{
			IQueryContainer q = new QueryContainer();
			q.Bool = new BoolBaseQueryDescriptor();
			q.Bool.Should = shouldClauses.NullIfEmpty();
			return q as QueryContainer;
		}
        internal static QueryContainer CombineAsMust(this QueryContainer leftContainer, QueryContainer rightContainer)
        {
            QueryContainer c;
            var            leftBool  = leftContainer.Self()?.Bool;
            var            rightBool = rightContainer.Self()?.Bool;

            //neither side is a bool, no special handling needed wrap in a bool must
            if (leftBool == null && rightBool == null)
            {
                return(CreateMustContainer(new Containers {
                    leftContainer, rightContainer
                }));
            }

            else if (TryHandleBoolsWithOnlyShouldClauses(leftContainer, rightContainer, leftBool, rightBool, out c))
            {
                return(c);
            }
            else if (TryHandleUnmergableBools(leftContainer, rightContainer, leftBool, rightBool, out c))
            {
                return(c);
            }

            //neither side is unmergable so neither is a bool with should clauses

            var mustNotClauses = OrphanMustNots(leftContainer).EagerConcat(OrphanMustNots(rightContainer));
            var filterClauses  = OrphanFilters(leftContainer).EagerConcat(OrphanFilters(rightContainer));
            var mustClauses    = OrphanMusts(leftContainer).EagerConcat(OrphanMusts(rightContainer));

            var container = CreateMustContainer(mustClauses, mustNotClauses, filterClauses);

            return(container);
        }
        [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")]         // there for clarity
        private static bool TryHandleUnmergableBools(
            QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c
            )
        {
            c = null;
            var leftCantMergeAnd  = leftBool != null && !leftBool.CanMergeAnd();
            var rightCantMergeAnd = rightBool != null && !rightBool.CanMergeAnd();

            if (!leftCantMergeAnd && !rightCantMergeAnd)
            {
                return(false);
            }

            if (leftCantMergeAnd && rightCantMergeAnd)
            {
                c = CreateMustContainer(leftContainer, rightContainer);
            }
            //right can't merge but left can and is a bool so we add left to the must clause of right
            else if (!leftCantMergeAnd && leftBool != null && rightCantMergeAnd)
            {
                leftBool.Must = leftBool.Must.AddIfNotNull(rightContainer);
                c             = leftContainer;
            }
            //right can't merge and left is not a bool, we forcefully create a wrapped must container
            else if (!leftCantMergeAnd && leftBool == null && rightCantMergeAnd)
            {
                c = CreateMustContainer(leftContainer, rightContainer);
            }
            //left can't merge but right can and is a bool so we add left to the must clause of right
            else if (leftCantMergeAnd && !rightCantMergeAnd && rightBool != null)
            {
                rightBool.Must = rightBool.Must.AddIfNotNull(leftContainer);
                c = rightContainer;
            }
            //left can't merge and right is not a bool, we forcefully create a wrapped must container
            else if (leftCantMergeAnd && !rightCantMergeAnd && rightBool == null)
            {
                c = CreateMustContainer(new Containers {
                    leftContainer, rightContainer
                });
            }
            return(c != null);
        }
		private void AssertBoolQuery(QueryContainer q, Action<IBoolQuery> assert) =>
			assert(((IQueryContainer)q).Bool);
 private static QueryContainer CreateMustContainer(QueryContainer left, QueryContainer right)
 {
     return(CreateMustContainer(new Containers {
         left, right
     }, null));
 }
		public void CombindingManyBoolFiltersUsingOrsShouldFlatten()
		{
			var container = new QueryContainer();
			foreach (var i in Enumerable.Range(0, 100))
				container |= +Query;
			var c = container as IQueryContainer;
		
			c.Bool.Should.Should().NotBeEmpty().And.HaveCount(100);
		}
Esempio n. 40
0
 internal static bool NotWritable(this QueryContainer q) => q == null || !q.IsWritable;
        public IScoreFunction Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            QueryContainer filter   = null;
            double?        weight   = null;
            IScoreFunction function = null;

            var count = 0;

            while (reader.ReadIsInObject(ref count))
            {
                var propertyName = reader.ReadPropertyNameSegmentRaw();
                if (AutomataDictionary.TryGetValue(propertyName, out var value))
                {
                    switch (value)
                    {
                    case 0:
                        var formatter = formatterResolver.GetFormatter <QueryContainer>();
                        filter = formatter.Deserialize(ref reader, formatterResolver);
                        break;

                    case 1:
                        weight = reader.ReadDouble();
                        break;

                    case 2:
                        var            innerCount     = 0;
                        MultiValueMode?multiValueMode = null;
                        IDecayFunction decayFunction  = null;
                        while (reader.ReadIsInObject(ref innerCount))
                        {
                            var functionPropertyName = reader.ReadPropertyName();
                            if (functionPropertyName == "multi_value_mode")
                            {
                                multiValueMode = formatterResolver.GetFormatter <MultiValueMode>()
                                                 .Deserialize(ref reader, formatterResolver);
                            }
                            else
                            {
                                var name = propertyName.Utf8String();
                                decayFunction       = ReadDecayFunction(ref reader, name, formatterResolver);
                                decayFunction.Field = functionPropertyName;
                            }
                        }

                        if (decayFunction != null)
                        {
                            decayFunction.MultiValueMode = multiValueMode;
                            function = decayFunction;
                        }
                        break;

                    case 3:
                        var randomScoreFormatter = formatterResolver.GetFormatter <RandomScoreFunction>();
                        function = randomScoreFormatter.Deserialize(ref reader, formatterResolver);
                        break;

                    case 4:
                        var fieldValueFormatter = formatterResolver.GetFormatter <FieldValueFactorFunction>();
                        function = fieldValueFormatter.Deserialize(ref reader, formatterResolver);
                        break;

                    case 5:
                        var scriptFormatter = formatterResolver.GetFormatter <ScriptScoreFunction>();
                        function = scriptFormatter.Deserialize(ref reader, formatterResolver);
                        break;
                    }
                }
            }

            if (function == null)
            {
                if (weight.HasValue)
                {
                    function = new WeightFunction();
                }
                else
                {
                    return(null);
                }
            }

            function.Weight = weight;
            function.Filter = filter;
            return(function);
        }
		protected void ReturnsSingleQuery(QueryContainer combined, Func<QueryContainerDescriptor<Project>, QueryContainer> selector, Action<IQueryContainer> containerAssert)
		{
			ReturnsSingleQuery(combined, containerAssert);
			ReturnsSingleQuery(selector.Invoke(new QueryContainerDescriptor<Project>()), containerAssert);
		}
Esempio n. 43
0
 /// <summary>
 /// A query to optionally limit the documents to use for the reindex operation.
 /// </summary>
 public ReindexDescriptor <T> Query(QueryContainer query) => Assign(a => a.Query = query);
 private static QueryContainer CreateMustContainer(QueryContainer left, QueryContainer right) =>
 CreateMustContainer(new Containers {
     left, right
 });
Esempio n. 45
0
 private static QueryContainer CreateMustContainer(List <QueryContainer> mustClauses, QueryContainer reuse) =>
 new QueryContainer(new BoolQuery()
 {
     Must = mustClauses.ToListOrNullIfEmpty()
 });
Esempio n. 46
0
 internal static bool IsNullOrConditionless(this QueryContainer c) => c == null || c.IsConditionless;
		protected void ReturnsNull(QueryContainer combined, Func<QueryContainerDescriptor<Project>, QueryContainer> selector)
		{
			combined.Should().BeNull(); selector.Invoke(new QueryContainerDescriptor<Project>()).Should().BeNull();
		}
Esempio n. 48
0
        public ActionResult Index()
        {
            var model = new HomeViewModel();

            // DateTime Range
            var queryRange = new Nest.DateRangeQuery();

            queryRange.Field                = new Nest.Field();
            queryRange.Field.Name           = "datum";
            queryRange.GreaterThanOrEqualTo = DateTime.Now.AddDays(-DateTime.Now.Day);
            queryRange.LessThanOrEqualTo    = DateTime.Now.AddDays(0);
            var rangeContainer = new Nest.QueryContainer(queryRange);

            // Name TagCloud
            var nameTagCloudModel = new TagCloudViewModel();

            nameTagCloudModel.Title = "Meist verwendete Namen";
            nameTagCloudModel.Tags  = new Dictionary <string, float>();
            var listOfQueriesTagCloud = new List <Nest.QueryContainer>();

            listOfQueriesTagCloud.Add(rangeContainer);

            var res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesTagCloud.ToArray())
                                                                                      )
                                                                                )
                                                                         .Aggregations(a =>
                                                                                       a.Terms("tagcloud", ta => ta.Field("name"))
                                                                                       )
                                                                         );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                nameTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.NameTagCloud = nameTagCloudModel;

            // DocType TagCloud
            var docTagCloudModel = new TagCloudViewModel();

            docTagCloudModel.Title = "Meist verwendete DocTypes";
            docTagCloudModel.Tags  = new Dictionary <string, float>();

            foreach (var docType in DocTypeRepository.Index())
            {
                var listOfQueriesDocType = new List <Nest.QueryContainer>();
                listOfQueriesDocType.Add(rangeContainer);

                var queryDocType = new Nest.MatchQuery();
                queryDocType.Field      = new Nest.Field();
                queryDocType.Field.Name = "type";
                queryDocType.Query      = docType.Id.ToString();
                var docTypesContainer = new Nest.QueryContainer(queryDocType);
                listOfQueriesDocType.Add(docTypesContainer);

                res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesDocType.ToArray())
                                                                                      )
                                                                                )
                                                                         );
                docTagCloudModel.Tags.Add(docType.Name, res.Total);
            }
            model.DocTypeTagCloud = docTagCloudModel;

            // Ort TagCloud
            var ortTagCloudModel = new TagCloudViewModel();

            ortTagCloudModel.Title = "Meist verwendete Orte";
            ortTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("ort"))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                ortTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.OrtTagCloud = ortTagCloudModel;

            // Kategorie TagCloud
            var kategorieTagCloudModel = new TagCloudViewModel();

            kategorieTagCloudModel.Title = "Meist verwendete Kategorien";
            kategorieTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("kategorie")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         ))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                kategorieTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.KategorieTagCloud = kategorieTagCloudModel;

            // Handel TagCloud
            var handelTagCloudModel = new TagCloudViewModel();

            handelTagCloudModel.Title = "Meist verwendete Handel";
            handelTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("handel")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         )
                                                                                           )
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                handelTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.HandelTagCloud = handelTagCloudModel;

            // Preis Summe

            var listOfQueriesAusgaben = new List <Nest.QueryContainer>();

            listOfQueriesAusgaben.Add(rangeContainer);

            var queryAusgabe = new Nest.MatchQuery();

            queryAusgabe.Field      = new Nest.Field();
            queryAusgabe.Field.Name = "type";
            queryAusgabe.Query      = "e0c4f438-4509-449b-a491-3fa3093b4129";
            var docTypeContainer = new Nest.QueryContainer(queryAusgabe);

            listOfQueriesAusgaben.Add(docTypeContainer);


            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesAusgaben.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("preis"))
                                                                                   )
                                                                     );
            model.SummeAusgaben = res.Aggs.Sum("summe").Value.Value;

            // Einnahmen Summe
            var listOfQueriesEinnahmen = new List <Nest.QueryContainer>();

            listOfQueriesEinnahmen.Add(rangeContainer);
            var queryEinnahme = new Nest.MatchQuery();

            queryEinnahme.Field      = new Nest.Field();
            queryEinnahme.Field.Name = "type";
            queryEinnahme.Query      = "05ce209c-e837-4fc4-b2a4-6b54bf73be46";
            docTypeContainer         = new Nest.QueryContainer(queryEinnahme);
            listOfQueriesEinnahmen.Add(docTypeContainer);

            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesEinnahmen.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("betrag"))
                                                                                   )
                                                                     );
            model.SummeEinnahmen = res.Aggs.Sum("summe").Value.Value;

            return(View(model));
        }
Esempio n. 49
0
 /// <summary>
 /// Filter search
 /// </summary>
 public PercolateCountDescriptor <TDocument> Filter(QueryContainer filter)
 {
     filter.ThrowIfNull(nameof(filter));
     Self.Filter = filter;
     return(this);
 }
        internal static QueryContainer CombineAsShould(this QueryContainer leftContainer, QueryContainer rightContainer)
        {
            QueryContainer c         = null;
            var            leftBool  = leftContainer.Self()?.Bool;
            var            rightBool = rightContainer.Self()?.Bool;

            if (TryFlattenShould(leftContainer, rightContainer, leftBool, rightBool, out c))
            {
                return(c);
            }

            var lBoolQuery = leftContainer.Self().Bool;
            var rBoolQuery = rightContainer.Self().Bool;

            var lHasShouldQueries = lBoolQuery != null && lBoolQuery.Should.HasAny();
            var rHasShouldQueries = rBoolQuery != null && rBoolQuery.Should.HasAny();

            var lq = lHasShouldQueries ? lBoolQuery.Should : new[] { leftContainer };
            var rq = rHasShouldQueries ? rBoolQuery.Should : new[] { rightContainer };

            var shouldClauses = lq.EagerConcat(rq);

            return(CreateShouldContainer(shouldClauses));
        }
		private void ReturnsSingleQuery(QueryContainer combined, Action<IQueryContainer> containerAssert)
		{
			combined.Should().NotBeNull();
			IQueryContainer c = combined;
			containerAssert(c);
		}
Esempio n. 52
0
        public IQueryContainer BuildQuery(System.Collections.Specialized.NameValueCollection nvc)
        {
            IQueryContainer query = new QueryContainer();

            if (string.IsNullOrEmpty(nvc["q"])) {
                query.MatchAllQuery = new MatchAllQuery();
            } else {
                query.QueryString = new QueryStringQuery();
                query.QueryString.Query = nvc["q"];
            }

            return query;
        }
 internal static QueryContainer Or(QueryContainer leftContainer, QueryContainer rightContainer) =>
 IfEitherIsEmptyReturnTheOtherOrEmpty(leftContainer, rightContainer, out var queryContainer)
                         ? queryContainer
                         : leftContainer.CombineAsShould(rightContainer);
 internal static IQueryContainer Self(this QueryContainer q) => q;
		internal static QueryContainer CreateMustContainer(IList<IQueryContainer> mustClauses, IEnumerable<IQueryContainer> mustNotClauses)
		{
			IQueryContainer q = new QueryContainer();
			q.Bool = new BoolBaseQueryDescriptor();
			q.Bool.Must = mustClauses.NullIfEmpty();
			q.Bool.MustNot = mustNotClauses.NullIfEmpty();
			return q as QueryContainer;
		}
        private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(QueryContainer leftContainer, QueryContainer rightContainer,
                                                                 out QueryContainer queryContainer
                                                                 )
        {
            queryContainer = null;
            if (leftContainer == null && rightContainer == null)
            {
                return(true);
            }

            var leftWritable  = leftContainer?.IsWritable ?? false;
            var rightWritable = rightContainer?.IsWritable ?? false;

            if (leftWritable && rightWritable)
            {
                return(false);
            }
            if (!leftWritable && !rightWritable)
            {
                return(true);
            }

            queryContainer = leftWritable ? leftContainer : rightContainer;
            return(true);
        }
Esempio n. 57
0
 public SearchDescriptor <T> Query(QueryContainer query)
 {
     return(this.Query((IQueryContainer)query));
 }
Esempio n. 58
0
        private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(QueryContainer leftContainer, QueryContainer rightContainer,
                                                                 out QueryContainer queryContainer)
        {
            var combined = new[] { leftContainer, rightContainer };

            queryContainer = !combined.Any(bf => bf == null || bf.IsConditionless)
                                ? null
                                : combined.FirstOrDefault(bf => bf != null && !bf.IsConditionless) ?? CreateEmptyContainer();
            return(queryContainer != null);
        }