コード例 #1
0
        private static FilterContainer CreateEmptyContainer()
        {
            var q = new FilterContainer();

            ((IFilterContainer)q).IsConditionless = true;
            return(q);
        }
コード例 #2
0
        internal static FilterContainer CreateShouldContainer(IList <IFilterContainer> shouldClauses)
        {
            IFilterContainer q = new FilterContainer();

            q.Bool        = new BoolBaseFilterDescriptor();
            q.Bool.Should = shouldClauses.NullIfEmpty();
            return(q as FilterContainer);
        }
コード例 #3
0
        public FunctionScoreFunction <T> Filter(Func <FilterDescriptor <T>, FilterContainer> filterSelector)
        {
            filterSelector.ThrowIfNull("filterSelector");
            var filter = new FilterDescriptor <T>();
            var f      = filterSelector(filter);

            this.FilterDescriptor = f;
            return(this);
        }
コード例 #4
0
        internal static FilterContainer CreateMustContainer(IList <IFilterContainer> mustClauses, IEnumerable <IFilterContainer> mustNotClauses)
        {
            IFilterContainer q = new FilterContainer();

            q.Bool         = new BoolBaseFilterDescriptor();
            q.Bool.Must    = mustClauses.NullIfEmpty();
            q.Bool.MustNot = mustNotClauses.NullIfEmpty();
            return(q as FilterContainer);
        }
コード例 #5
0
        private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(FilterContainer leftContainer, FilterContainer rightContainer,
                                                                 out FilterContainer filterContainer)
        {
            var combined = new[] { leftContainer, rightContainer };

            filterContainer = !combined.Any(bf => bf == null || bf.IsConditionless)
                                ? null
                                : combined.FirstOrDefault(bf => bf != null && !bf.IsConditionless) ?? CreateEmptyContainer();
            return(filterContainer != null);
        }
コード例 #6
0
        /// <summary>
        /// AND's two BaseFilters
        /// </summary>
        /// <returns>A new basefilter that represents the AND of the two</returns>
        public static FilterContainer operator &(FilterContainer leftFilterDescriptor, FilterContainer rightFilterDescriptor)
        {
            var defaultFilter = new FilterContainer()
            {
                IsConditionless = true
            };

            leftFilterDescriptor  = leftFilterDescriptor ?? defaultFilter;
            rightFilterDescriptor = rightFilterDescriptor ?? defaultFilter;
            var combined = new[] { leftFilterDescriptor, rightFilterDescriptor };

            //if any of the queries is conditionless return the first one that is not
            //or return the defaultQuery
            if (combined.Any(bf => bf.IsConditionless))
            {
                return(combined.FirstOrDefault(bf => !bf.IsConditionless) ?? defaultFilter);
            }

            //return simple combination of the two if merging is not possible/necessary
            var noMergeFilter = CombineIfNoMergeIsNecessary(leftFilterDescriptor, rightFilterDescriptor, combined);

            if (noMergeFilter != null)
            {
                return(noMergeFilter);
            }

            //if the left is a strict bool try to merge right on left first
            var joinStrictLeft = StrictSingleSideAndMerge(leftFilterDescriptor, rightFilterDescriptor);

            if (joinStrictLeft != null)
            {
                return(joinStrictLeft);
            }

            // if the right side is a strict bool try to merge left on right
            var joinStrictRight = StrictSingleSideAndMerge(rightFilterDescriptor, leftFilterDescriptor);

            if (joinStrictRight != null)
            {
                return(joinStrictRight);
            }

            // if the left side is a normal bool try to merge right on left
            var joinLeft = SingleSideAndMerge(leftFilterDescriptor, rightFilterDescriptor);

            if (joinLeft != null)
            {
                return(joinLeft);
            }

            // if the right side is a normal bool try to merge lefft on right
            var joinRight = SingleSideAndMerge(rightFilterDescriptor, leftFilterDescriptor);

            return(joinRight ?? defaultFilter);
        }
コード例 #7
0
        public static FilterContainer operator !(FilterContainer filterContainer)
        {
            if (filterContainer == null || filterContainer.IsConditionless)
            {
                return(CreateEmptyContainer());
            }

            IFilterContainer f = new FilterContainer();

            f.Bool         = new BoolBaseFilterDescriptor();
            f.Bool.MustNot = new[] { filterContainer };
            return(f as FilterContainer);
        }
コード例 #8
0
        public static FilterContainer CreateReturnFilter(Action <FilterContainer, BoolBaseFilterDescriptor> modify = null)
        {
            var returnFilter     = new FilterContainer();
            var returnBoolFilter = new BoolBaseFilterDescriptor()
            {
            };

            ((IFilterContainer)returnFilter).Bool = returnBoolFilter;
            if (modify != null)
            {
                modify(returnFilter, returnBoolFilter);
            }
            return(returnFilter);
        }
コード例 #9
0
        public static FilterContainer operator !(FilterContainer lbq)
        {
            if (lbq == null || lbq.IsConditionless)
            {
                return new FilterContainer {
                           IsConditionless = true
                }
            }
            ;

            var f  = new FilterContainer();
            var fq = new BoolBaseFilterDescriptor();

            ((IBoolFilter)fq).MustNot = new[] { lbq };

            ((IFilterContainer)f).Bool = fq;
            return(f);
        }
コード例 #10
0
        /// <summary>
        /// A filter that filters out matched documents using a query.
        /// This filter is more performant then bool filter.
        /// </summary>
        public FilterContainer Not(Func <FilterDescriptor <T>, FilterContainer> selector)
        {
            var notFilter = new NotFilterDescriptor();

            var filter = new FilterDescriptor <T>()
            {
                IsConditionless = true
            };
            FilterContainer bf = filter;

            if (selector != null)
            {
                bf = selector(filter);
            }

            ((INotFilter)notFilter).Filter = bf;
            return(this.New(notFilter, f => f.Not = notFilter));
        }
コード例 #11
0
		internal static FilterContainer CreateShouldContainer(IList<IFilterContainer> shouldClauses)
		{
			IFilterContainer q = new FilterContainer();
			q.Bool = new BoolBaseFilterDescriptor();
			q.Bool.Should = shouldClauses.NullIfEmpty();
			return q as FilterContainer;
		}
コード例 #12
0
ファイル: SearchDescriptor.cs プロジェクト: GopiKand/NEST
 /// <summary>
 /// Filter search
 /// </summary>
 public SearchDescriptor <T> Filter(FilterContainer filterDescriptor)
 {
     filterDescriptor.ThrowIfNull("filter");
     Self.Filter = filterDescriptor;
     return(this);
 }
コード例 #13
0
 /// <summary>
 /// Filter search
 /// </summary>
 public PercolateCountDescriptor <T> Filter(FilterContainer filterDescriptor)
 {
     filterDescriptor.ThrowIfNull("filter");
     Self.Filter = filterDescriptor;
     return(this);
 }
コード例 #14
0
		public static FilterContainer operator !(FilterContainer filterContainer)
		{
			if (filterContainer == null || filterContainer.IsConditionless) return CreateEmptyContainer();

			IFilterContainer f = new FilterContainer();
			f.Bool = new BoolBaseFilterDescriptor();
			f.Bool.MustNot = new[] { filterContainer };
			return f as FilterContainer;
		}
コード例 #15
0
		private static FilterContainer CreateEmptyContainer()
		{
			var q = new FilterContainer();
			((IFilterContainer)q).IsConditionless = true;
			return q;
		}
コード例 #16
0
		public static FilterContainer CreateReturnFilter(Action<FilterContainer, BoolBaseFilterDescriptor> modify = null)
		{
			var returnFilter = new FilterContainer();
			var returnBoolFilter = new BoolBaseFilterDescriptor() { };
			((IFilterContainer)returnFilter).Bool = returnBoolFilter;
			if (modify != null)
			{
				modify(returnFilter, returnBoolFilter);
			}
			return returnFilter;
		}
コード例 #17
0
		private static bool IfEitherIsEmptyReturnTheOtherOrEmpty(FilterContainer leftContainer, FilterContainer rightContainer,
			out FilterContainer filterContainer)
		{
			var combined = new[] {leftContainer, rightContainer};
			filterContainer = !combined.Any(bf => bf == null || bf.IsConditionless)
				? null
				: combined.FirstOrDefault(bf => bf != null && !bf.IsConditionless) ?? CreateEmptyContainer();
			return filterContainer != null;
		}
コード例 #18
0
		public static FilterContainer operator !(FilterContainer lbq)
		{
			if (lbq == null || lbq.IsConditionless)
				return new FilterContainer { IsConditionless = true };

			var f = new FilterContainer();
			var fq = new BoolBaseFilterDescriptor();
			((IBoolFilter)fq).MustNot = new[] { lbq };

			((IFilterContainer)f).Bool = fq;
			return f;
		}
コード例 #19
0
		public static FilterContainer operator |(FilterContainer leftFilterDescriptor, FilterContainer rightFilterDescriptor)
		{
			var defaultFilter = new FilterContainer() { IsConditionless = true };
			leftFilterDescriptor = leftFilterDescriptor ?? defaultFilter;
			rightFilterDescriptor = rightFilterDescriptor ?? defaultFilter;
			var combined = new[] { leftFilterDescriptor, rightFilterDescriptor };

			if (combined.Any(bf => bf.IsConditionless))
				return combined.FirstOrDefault(bf => !bf.IsConditionless) ?? defaultFilter;

			var leftDescriptor = ((IFilterContainer)leftFilterDescriptor);
			var leftBoolFilter = leftDescriptor.Bool;
			var rightDescriptor = ((IFilterContainer)rightFilterDescriptor);
			var rightBoolFilter = rightDescriptor.Bool;
		

			var f = new FilterContainer();
			var fq = new BoolBaseFilterDescriptor();
			var bfq = (IBoolFilter)fq;
			bfq.Should = new[] { leftFilterDescriptor, rightFilterDescriptor };
			((IFilterContainer)f).Bool = fq;

			//if neither the left nor the right side represent a bool filter join them
			if (leftDescriptor.Bool == null && rightDescriptor.Bool == null)
			{
				bfq.Should = leftFilterDescriptor.MergeShouldFilters(rightFilterDescriptor);
				return f;
			}
			//if the left or right sight already is a bool filter determine join the non bool query side of the 
			//of the operation onto the other.
			if (leftDescriptor.Bool != null && rightDescriptor.Bool == null && !leftDescriptor.IsStrict)
			{
				JoinShouldOnSide(leftFilterDescriptor, rightFilterDescriptor, fq);
			}
			else if (rightDescriptor.Bool != null && leftDescriptor.Bool == null && !rightDescriptor.IsStrict)
			{
				JoinShouldOnSide(rightFilterDescriptor, leftFilterDescriptor, fq);
			}
			//both sides already represent a bool filter
			else
			{
				//both sides report that we may merge the shoulds
				if (!leftDescriptor.IsStrict && !rightDescriptor.IsStrict
					&& leftBoolFilter.CanJoinShould()
					&& rightBoolFilter.CanJoinShould())
					bfq.Should = leftFilterDescriptor.MergeShouldFilters(rightFilterDescriptor);
				else
					//create a new nested bool with two separate should bool sections
					bfq.Should = new[] { leftFilterDescriptor, rightFilterDescriptor };
			}
			return f;
		}
コード例 #20
0
		internal static FilterContainer CreateMustContainer(IList<IFilterContainer> mustClauses, IEnumerable<IFilterContainer> mustNotClauses)
		{
			IFilterContainer q = new FilterContainer();
			q.Bool = new BoolBaseFilterDescriptor();
			q.Bool.Must = mustClauses.NullIfEmpty();
			q.Bool.MustNot = mustNotClauses.NullIfEmpty();
			return q as FilterContainer;
		}
コード例 #21
0
        public ISearchResponse<Book> MultiMatchANDSearch(List<AdvancedSearchFilter> SearchCriteria, List<string> refinements = null, Dictionary<string, string> SearchFilters = null, int count = 10)
        {
            QueryContainer _query = null;

            foreach(var rec in SearchCriteria)
            {
                if ((!String.IsNullOrEmpty(rec.SearchField)) && (!String.IsNullOrEmpty(rec.SearchField)))
                {
                    var qry = new MatchQuery()
                    {
                        Field = rec.SearchField,
                        Query = rec.SearchQuery
                    };

                    if (_query == null)
                        _query = qry;
                    else
                        _query = _query && qry;
                }
            }

            SearchRequest searchRequest = new SearchRequest
            {

                From = 0,
                Size = count,
                Query = _query
            };

            if (refinements != null && refinements.Count > 0)
            {
                var _aggregations = new Dictionary<string, IAggregationContainer>();

                foreach (var field in refinements)
                {
                    _aggregations.Add(field, new AggregationContainer
                    {
                        Terms = new TermsAggregator
                        {
                            Field = field
                        }
                    });
                }

                searchRequest.Aggregations = _aggregations;

            }

            if (SearchFilters != null && SearchFilters.Count > 0)
            {
                var searchFilterConfig = new FilterContainer();

                foreach (var sf in SearchFilters)
                {
                    searchFilterConfig = Filter<Book>.Term(sf.Key, sf.Value);
                }

                searchRequest.Filter = searchFilterConfig;
            }

            return esClient.Search<Book>(searchRequest);
        }
コード例 #22
0
        public static FilterContainer operator |(FilterContainer leftFilterDescriptor, FilterContainer rightFilterDescriptor)
        {
            var defaultFilter = new FilterContainer()
            {
                IsConditionless = true
            };

            leftFilterDescriptor  = leftFilterDescriptor ?? defaultFilter;
            rightFilterDescriptor = rightFilterDescriptor ?? defaultFilter;
            var combined = new[] { leftFilterDescriptor, rightFilterDescriptor };

            if (combined.Any(bf => bf.IsConditionless))
            {
                return(combined.FirstOrDefault(bf => !bf.IsConditionless) ?? defaultFilter);
            }

            var leftDescriptor  = ((IFilterContainer)leftFilterDescriptor);
            var leftBoolFilter  = leftDescriptor.Bool;
            var rightDescriptor = ((IFilterContainer)rightFilterDescriptor);
            var rightBoolFilter = rightDescriptor.Bool;


            var f   = new FilterContainer();
            var fq  = new BoolBaseFilterDescriptor();
            var bfq = (IBoolFilter)fq;

            bfq.Should = new[] { leftFilterDescriptor, rightFilterDescriptor };
            ((IFilterContainer)f).Bool = fq;

            //if neither the left nor the right side represent a bool filter join them
            if (leftDescriptor.Bool == null && rightDescriptor.Bool == null)
            {
                bfq.Should = leftFilterDescriptor.MergeShouldFilters(rightFilterDescriptor);
                return(f);
            }
            //if the left or right sight already is a bool filter determine join the non bool query side of the
            //of the operation onto the other.
            if (leftDescriptor.Bool != null && rightDescriptor.Bool == null && !leftDescriptor.IsStrict)
            {
                JoinShouldOnSide(leftFilterDescriptor, rightFilterDescriptor, fq);
            }
            else if (rightDescriptor.Bool != null && leftDescriptor.Bool == null && !rightDescriptor.IsStrict)
            {
                JoinShouldOnSide(rightFilterDescriptor, leftFilterDescriptor, fq);
            }
            //both sides already represent a bool filter
            else
            {
                //both sides report that we may merge the shoulds
                if (!leftDescriptor.IsStrict && !rightDescriptor.IsStrict &&
                    leftBoolFilter.CanJoinShould() &&
                    rightBoolFilter.CanJoinShould())
                {
                    bfq.Should = leftFilterDescriptor.MergeShouldFilters(rightFilterDescriptor);
                }
                else
                {
                    //create a new nested bool with two separate should bool sections
                    bfq.Should = new[] { leftFilterDescriptor, rightFilterDescriptor }
                };
            }
            return(f);
        }
コード例 #23
0
        public ISearchResponse<Book> SearchBookWithAggregationFilters(string criteria = "", string searchField = "", 
            List<string> refinements = null,
            Dictionary<string,string> SearchFilters = null,
            int count = 10)
        {
            QueryContainer _query;

            if (String.IsNullOrEmpty(criteria))
            {
                _query = new MatchAllQuery();
            }
            else
            {
                if (String.IsNullOrEmpty(searchField))
                {
                    searchField = "_all";

                    _query = new MatchQuery()
                    {
                        Field = searchField,
                        Query = criteria
                    };
                }
                else
                {
                    _query = new TermQuery()
                    {
                        Field = searchField,
                        Value = criteria
                    };
                }

            }

            SearchRequest searchRequest = new SearchRequest
            {

                From = 0,
                Size = count,
                Query = _query
            };

            if (refinements != null && refinements.Count > 0)
            {
                var _aggregations = new Dictionary<string, IAggregationContainer>();

                foreach (var field in refinements)
                {
                    _aggregations.Add(field, new AggregationContainer
                    {
                        Terms = new TermsAggregator
                        {
                            Field = field
                        }
                    });
                }

                searchRequest.Aggregations = _aggregations;

            }

            if (SearchFilters != null && SearchFilters.Count > 0)
            {
                var searchFilterConfig = new FilterContainer();

                foreach(var sf in SearchFilters)
                {
                    searchFilterConfig = Filter<Book>.Term( sf.Key, sf.Value );
                }

                searchRequest.Filter = searchFilterConfig;
            }

            return esClient.Search<Book>(searchRequest);
        }
コード例 #24
0
		/// <summary>
		/// AND's two BaseFilters
		/// </summary>
		/// <returns>A new basefilter that represents the AND of the two</returns>
		public static FilterContainer operator &(FilterContainer leftFilterDescriptor, FilterContainer rightFilterDescriptor)
		{
			var defaultFilter = new FilterContainer() { IsConditionless = true };
			leftFilterDescriptor = leftFilterDescriptor ?? defaultFilter;
			rightFilterDescriptor = rightFilterDescriptor ?? defaultFilter;
			var combined = new[] { leftFilterDescriptor, rightFilterDescriptor };

			//if any of the queries is conditionless return the first one that is not
			//or return the defaultQuery
			if (combined.Any(bf => bf.IsConditionless))
				return combined.FirstOrDefault(bf=>!bf.IsConditionless) ?? defaultFilter;

			//return simple combination of the two if merging is not possible/necessary
			var noMergeFilter = CombineIfNoMergeIsNecessary(leftFilterDescriptor, rightFilterDescriptor, combined);
			if (noMergeFilter != null)
				return noMergeFilter;

			//if the left is a strict bool try to merge right on left first 
			var joinStrictLeft = StrictSingleSideAndMerge(leftFilterDescriptor, rightFilterDescriptor);
			if (joinStrictLeft != null)
				return joinStrictLeft;

			// if the right side is a strict bool try to merge left on right
			var joinStrictRight = StrictSingleSideAndMerge(rightFilterDescriptor, leftFilterDescriptor);
			if (joinStrictRight != null)
				return joinStrictRight;

			// if the left side is a normal bool try to merge right on left
			var joinLeft = SingleSideAndMerge(leftFilterDescriptor, rightFilterDescriptor);
			if (joinLeft != null)
				return joinLeft;

			// if the right side is a normal bool try to merge lefft on right
			var joinRight = SingleSideAndMerge(rightFilterDescriptor, leftFilterDescriptor);
			return joinRight ?? defaultFilter;
		}