internal virtual KeyValuePair<int, List<SitecoreItem>> RunQuery(QueryBase query, int numberOfResults)
 {
     var translator = new QueryTranslator(Index);
     var luceneQuery = translator.Translate(query);
     return this.RunQuery(luceneQuery, numberOfResults, 0);
 }
        public static BooleanQuery ContructQuery(DateRangeSearchParam param)
        {
            var indexName = BucketManager.GetContextIndex(Context.ContentDatabase.GetItem(param.LocationIds));

            if (indexName.EndsWith("_remote"))
            {
                Index = RemoteSearchManager.GetIndex(indexName) as RemoteIndex;
            }
            else if (indexName.EndsWith("_inmemory"))
            {
                Index = InMemorySearchManager.GetIndex(indexName) as InMemoryIndex;
            }
            else
            {
                Index = SearchManager.GetIndex(indexName) as Index;
            }
            if (Index.IsNotNull())
            {
                var globalQuery = new CombinedQuery();
                var fullTextQuery = param.FullTextQuery; // .TrimStart('*').TrimEnd('*') + "*";
                SearcherMethods.ApplyLanguageClause(globalQuery, param.Language);
                if (!string.IsNullOrWhiteSpace(fullTextQuery))
                {
                    if (!fullTextQuery.StartsWith("*"))
                    {
                        SearcherMethods.ApplyFullTextClause(globalQuery, fullTextQuery, indexName);
                    }
                }

                SearcherMethods.ApplyRelationFilter(globalQuery, param.RelatedIds);
                SearcherMethods.ApplyTemplateFilter(globalQuery, param.TemplateIds);
                SearcherMethods.ApplyTemplateNotFilter(globalQuery);
                SearcherMethods.ApplyIDFilter(globalQuery, param.ID);
                SearcherMethods.ApplyLocationFilter(globalQuery, param.LocationIds);
                SearcherMethods.ApplyRefinements(globalQuery, param.Refinements, QueryOccurance.Should);
                SearcherMethods.ApplyLatestVersion(globalQuery);

                if (Config.ExcludeContextItemFromResult)
                {
                    SearcherMethods.ApplyContextItemRemoval(globalQuery, param.LocationIds);
                }

                var translator = new QueryTranslator(Index);
                var booleanQuery = translator.ConvertCombinedQuery(globalQuery);
                var innerOccurance = translator.GetOccur(param.Occurance);

                if (!string.IsNullOrWhiteSpace(fullTextQuery))
                {
                    if (fullTextQuery.StartsWith("*"))
                    {
                        SearcherMethods.ApplyFullTextClause(booleanQuery, fullTextQuery, indexName);
                    }
                }

                SearcherMethods.ApplyAuthor(booleanQuery, param.Author);
                SearcherMethods.ApplyDateRangeSearchParam(booleanQuery, param, innerOccurance);
                if (Config.EnableBucketDebug || Constants.EnableTemporaryBucketDebug)
                {
                    Log.Info("Search Clauses Number: " + booleanQuery.Clauses().Count, booleanQuery);
                }

                return booleanQuery;
            }

            return null;
        }
 internal virtual KeyValuePair<int, List<SitecoreItem>> RunQuery(QueryBase query, int numberOfResults, int pageNumber, string sortField, string sortDirection)
 {
     var translator = new QueryTranslator(Index);
     var luceneQuery = translator.Translate(query);
     return this.RunQuery(luceneQuery, numberOfResults, pageNumber, sortField, sortDirection);
 }
        internal virtual KeyValuePair<int, List<SitecoreItem>> GetItems(DateRangeSearchParam param)
        {
            var db = Context.ContentDatabase ?? Sitecore.Context.Database;
            if (db != null)
            {
                var indexName = Util.Constants.Index.Name;
                var item = db.GetItem(param.LocationIds);
                indexName = BucketManager.GetContextIndex(item.IsNotNull() ? item : db.GetItem(Sitecore.ItemIDs.RootID));

                if (indexName.EndsWith("_remote"))
                {
                    Index = RemoteSearchManager.GetIndex(indexName) as RemoteIndex;
                }
                else if (indexName.EndsWith("_inmemory"))
                {
                    Index = InMemorySearchManager.GetIndex(indexName) as InMemoryIndex;
                }
                else
                {
                    Index = SearchManager.GetIndex(indexName) as Index;
                }

                if (Index.IsNotNull())
                {
                    var globalQuery = new CombinedQuery();
                    SearcherMethods.ApplyLanguageClause(globalQuery, param.Language);
                    if (!string.IsNullOrWhiteSpace(param.FullTextQuery))
                    {
                        if (!param.FullTextQuery.StartsWith("*"))
                        {
                            if (param.FullTextQuery != "*All*" && param.FullTextQuery != "*" && param.FullTextQuery != "**")
                            {
                                SearcherMethods.ApplyFullTextClause(globalQuery, param.FullTextQuery, indexName);
                            }
                        }
                    }

                    SearcherMethods.ApplyRelationFilter(globalQuery, param.RelatedIds);
                    SearcherMethods.ApplyTemplateFilter(globalQuery, param.TemplateIds);
                    SearcherMethods.ApplyTemplateNotFilter(globalQuery);
                    SearcherMethods.ApplyIDFilter(globalQuery, param.ID);
                    if (param.LocationIds.Contains("|"))
                    {
                        SearcherMethods.ApplyCombinedLocationFilter(globalQuery, param.LocationIds);
                    }
                    else
                    {
                        SearcherMethods.ApplyLocationFilter(globalQuery, param.LocationIds);
                    }
                    if (!param.Refinements.ContainsKey("__workflow state")) //Hack!!!!!
                    {
                        SearcherMethods.ApplyRefinements(globalQuery, param.Refinements, QueryOccurance.Should);
                    }
                    SearcherMethods.ApplyLatestVersion(globalQuery);

                    if (Config.ExcludeContextItemFromResult)
                    {
                        SearcherMethods.ApplyContextItemRemoval(globalQuery, param.LocationIds);
                    }
                    SearcherMethods.ApplyNameFilter(globalQuery, param.ItemName);
                    var translator = new QueryTranslator(Index);
                    var booleanQuery = translator.ConvertCombinedQuery(globalQuery);
                    var innerOccurance = translator.GetOccur(param.Occurance);

                    if (!string.IsNullOrWhiteSpace(param.FullTextQuery))
                    {
                        if (param.FullTextQuery.StartsWith("*"))
                        {
                            if (param.FullTextQuery != "*All*" && param.FullTextQuery != "*" && param.FullTextQuery != "**")
                            {
                                SearcherMethods.ApplyFullTextClause(booleanQuery, param.FullTextQuery, indexName);
                            }
                        }
                    }

                    SearcherMethods.ApplyAuthor(booleanQuery, param.Author);
                    SearcherMethods.ApplyDateRangeSearchParam(booleanQuery, param, innerOccurance);
                    if (param.Refinements.ContainsKey("__workflow state"))
                    {
                        SearcherMethods.AddFieldValueClause(booleanQuery, "__workflow state", param.Refinements["__workflow state"], QueryOccurance.Should);
                    }
                    if (Config.EnableBucketDebug || Constants.EnableTemporaryBucketDebug)
                    {
                        Log.Info("Search Clauses Number: " + booleanQuery.Clauses().Count, this);
                    }

                    if (!param.SortByField.IsNullOrEmpty())
                    {
                        return this.RunQuery(booleanQuery, param.PageSize, param.PageNumber, param.SortByField, param.SortDirection);
                    }

                    return param.PageNumber != 0 ? this.RunQuery(booleanQuery, param.PageSize, param.PageNumber) : this.RunQuery(booleanQuery, 20, 0);
                }
            }

            return new KeyValuePair<int, List<SitecoreItem>>();
        }
 /// <summary>
 /// Runs the query.
 /// </summary>
 /// <param name="query">The query.</param>
 /// <returns>The key value pair of the running query.</returns>
 internal virtual KeyValuePair<int, List<SitecoreItem>> RunQuery(QueryBase query)
 {
     var translator = new QueryTranslator(Index);
       Query luceneQuery = translator.Translate(query);
       return this.RunQuery(luceneQuery, 20, 0);
 }