示例#1
0
        public Expression <Func <SearchResultItem, bool> > GetPredicatesFromRules(IProviderSearchContext context, string rawRules, Database db)
        {
            Assert.ArgumentNotNull(db, nameof(db));

            var expression = PredicateBuilder.True <SearchResultItem>();

            Sitecore.ContentSearch.Rules.QueryableRuleFactory ruleFactory = new Sitecore.ContentSearch.Rules.QueryableRuleFactory();
            var rules = ruleFactory.ParseRules <Sitecore.ContentSearch.Rules.QueryableRuleContext <SearchResultItem> >(db, rawRules);

            if (!rules.Rules.Any())
            {
                return(null);
            }

            foreach (var rule in rules.Rules)
            {
                if (rule.Condition != null)
                {
                    var ruleContext = new Sitecore.ContentSearch.Rules.QueryableRuleContext <SearchResultItem>(context);
                    var stack       = new RuleStack();
                    rule.Condition.Evaluate(ruleContext, stack);
                    rule.Execute(ruleContext);
                    if (stack.Any())
                    {
                        expression = ruleContext.Where;
                    }
                }
            }

            return(expression);
        }
示例#2
0
        public static Expression <Func <SearchResultItem, bool> > ProcessQueryRules(IProviderSearchContext context, string queryFilter, SearchOperation operation)
        {
            var predicate = operation == SearchOperation.Or
                ? PredicateBuilder.False <SearchResultItem>()
                : PredicateBuilder.True <SearchResultItem>();

            var crawler = context.Index.Crawlers.FirstOrDefault(c => c is SitecoreItemCrawler);

            if (crawler == null)
            {
                return(predicate);
            }

            var database = ((SitecoreItemCrawler)crawler).Database;

            if (string.IsNullOrEmpty(database))
            {
                return(predicate);
            }

            var ruleFactory = new Sitecore.ContentSearch.Rules.QueryableRuleFactory();
            var rules       = ruleFactory.ParseRules <Sitecore.ContentSearch.Rules.QueryableRuleContext <SearchResultItem> >(Factory.GetDatabase(database), queryFilter);

            foreach (var rule in rules.Rules)
            {
                if (rule.Condition == null)
                {
                    continue;
                }

                var ruleContext = new Sitecore.ContentSearch.Rules.QueryableRuleContext <SearchResultItem>(context);
                var stack       = new RuleStack();
                rule.Condition.Evaluate(ruleContext, stack);
                rule.Execute(ruleContext);
                if (stack.Any())
                {
                    predicate = ruleContext.Where;
                }
            }

            return(predicate);
        }