public virtual List<SkinnyItem> RunQuery(Query query, bool showAllVersions)
      {
         Assert.ArgumentNotNull(Index, "Aqueduct.SitecoreLib");

         var items = new List<SkinnyItem>();

         try
         {
            using (var context = new IndexSearchContext(Index))
            {
               var hits = context.Search(query);

               if (hits == null)
               {
                  return null;
               }

               var resultCollection = hits.FetchResults(0, hits.Length);
               SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
            }
         }
         catch (Exception exception)
         {
            Log.Error("Aqueduct.SitecoreLib. There was a problem while running a search query. Details: " + exception.Message, this);
            Log.Error(exception.StackTrace, this);
            throw;
         }

         return items;
      }
Beispiel #2
0
        public virtual List <SkinnyItem> RunQuery(Query query, bool showAllVersions)
        {
            Assert.ArgumentNotNull(Index, "Aqueduct.SitecoreLib");

            var items = new List <SkinnyItem>();

            try
            {
                using (var context = new IndexSearchContext(Index))
                {
                    var hits = context.Search(query);

                    if (hits == null)
                    {
                        return(null);
                    }

                    var resultCollection = hits.FetchResults(0, hits.Length);
                    SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
                }
            }
            catch (Exception exception)
            {
                Log.Error("Aqueduct.SitecoreLib. There was a problem while running a search query. Details: " + exception.Message, this);
                Log.Error(exception.StackTrace, this);
                throw;
            }

            return(items);
        }
Beispiel #3
0
 /// <summary>
 /// Searches this instance.
 /// </summary>
 /// <returns>Returns the search results</returns>
 public virtual IEnumerable <SearchResult> Search()
 {
     using (IndexSearchContext context = SearchManager.GetIndex(this.indexName).CreateSearchContext())
     {
         SearchResultController controller = new SearchResultController();
         return(controller.GetSearchResult(context.Search(new PreparedQuery(this.query), int.MaxValue)));
     }
 }
Beispiel #4
0
        public virtual List <SkinnyItem> RunQuery(Query query, bool showAllVersions, Sort sorter, int start, int end, out int totalResults)
        {
            Assert.ArgumentNotNull(Index, "Index");

            var items = new List <SkinnyItem>();

            if (query == null || string.IsNullOrEmpty(query.ToString()))
            {
                Log.Debug("SitecoreSearchContrib: Attempt to execute an empty query.");
                totalResults = 0;
                return(items);
            }

            try
            {
                using (var context = new IndexSearchContext(Index))
                {
                    Log.Debug(string.Format("SitecoreSearchContrib: Executing query: {0}", query));
                    SearchHits searchhits;
                    if (sorter != null)
                    {
                        var hits = context.Searcher.Search(query, sorter);
                        searchhits = new SearchHits(hits);
                    }
                    else
                    {
                        searchhits = this.UsePreparedQuery ? context.Search(new PreparedQuery(query)) :
                                     context.Search(query);
                    }

                    if (searchhits == null)
                    {
                        totalResults = 0;
                        return(null);
                    }

                    totalResults = searchhits.Length;
                    if (end == 0 || end > searchhits.Length)
                    {
                        end = totalResults;
                    }

                    Log.Debug(string.Format("SitecoreSearchContrib: Total hits: {0}", totalResults));
                    var resultCollection = searchhits.FetchResults(start, end - start);
                    SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
                    Log.Debug(string.Format("SitecoreSearchContrib: Total results: {0}", resultCollection.Count));
                }
            }
            catch (Exception exception)
            {
                Log.Error("scSearchContrib.Searcher. There was a problem while running a search query. Details: " + exception.Message, this);
                Log.Error(exception.StackTrace, this);
                throw;
            }

            return(items);
        }
Beispiel #5
0
        public virtual List <SkinnyItem> RunQuery(Query query, bool showAllVersions, string sortField, bool reverse, int start, int end, out int totalResults)
        {
            Assert.ArgumentNotNull(Index, "Demo");

            var items = new List <SkinnyItem>();

            try
            {
                using (var context = new IndexSearchContext(Index))
                {
                    SearchHits searchhits;
                    if (!sortField.IsNullOrEmpty())
                    {
                        // type hardcoded to 3 - means sorting as string
                        var sort = new Sort(new SortField(sortField, 3, reverse));
                        var hits = context.Searcher.Search(query, sort);
                        searchhits = new SearchHits(hits);
                    }
                    else
                    {
                        searchhits = context.Search(query);
                    }

                    if (searchhits == null)
                    {
                        totalResults = 0;
                        return(null);
                    }
                    totalResults = searchhits.Length;
                    if (end == 0 || end > searchhits.Length)
                    {
                        end = totalResults;
                    }
                    var resultCollection = searchhits.FetchResults(start, end);
                    SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
                }
            }
            catch (Exception exception)
            {
                Log.Error("scSearchContrib.Searcher. There was a problem while running a search query. Details: " + exception.Message, this);
                Log.Error(exception.StackTrace, this);
                throw;
            }

            return(items);
        }
Beispiel #6
0
        public IEnumerable <T> Query(Query query, out int totalResults, Guid rootItemId = default(Guid), int start = 0,
                                     int count = int.MaxValue)
        {
            var sitecoreService = _sitecoreServiceProvider.GetSitecoreService();
            var config          = sitecoreService.GlassContext.GetTypeConfiguration <SitecoreTypeConfiguration>(typeof(T));

            totalResults = 0;
            if (config == null)
            {
                return(null);
            }
            var searchContext = new FixedSearchContext()
            {
                TemplateID = config.TemplateId.ToGuid()
            };

            var boolQuery = new BooleanQuery();

            if (query != null)
            {
                boolQuery.Add(query, Occur.MUST);
            }
            if (rootItemId == (default(Guid)) && rootItemId != global::Sitecore.ItemIDs.RootID.ToGuid())
            {
                var rootItem = sitecoreService.Database.GetItem(ID.Parse(rootItemId));
                searchContext.Item = rootItem;
            }

            searchContext.ContentLanguage = GetLanguage(_dbProvider);
            using (var context = new IndexSearchContext(_searchContextProvider.GetProviderSearchContext().Index as ILuceneIndex))
            {
                var searchHits = context.Search(boolQuery, searchContext);

                totalResults = searchHits.Length;
                //var resultCollection = searchHits.FetchResults(start, count);
                var hits = (count == int.MaxValue) ? searchHits.Slice(start) : searchHits.Slice(start, count);
                return(GetItems(
                           hits.Select(hit => hit.Document.GetField(BuiltinFields.Url))
                           .Where(uriField => uriField != null && !string.IsNullOrEmpty(uriField.StringValue))
                           .Select(uriField => ItemUri.Parse(uriField.StringValue))
                           .ToList()));
            }
        }
Beispiel #7
0
        private SearchResultCollection GetSearchHits(QueryBase q, int maxResults)
        {
            if (q == null)
            {
                return(null);
            }

            var index = SearchManager.GetIndex(this.Index.Name);

            using (IndexSearchContext context = index.CreateSearchContext())
            {
                var preparedQuery = context.Prepare(q);
                var hits          = context.Search(preparedQuery, maxResults);

                if (this.Explain > -1)
                {
                    this.Explanation = context.Explain(preparedQuery, this.Explain);
                }

                return(hits.FetchResults(0, Math.Min(hits.Length, maxResults)));
            }
        }
Beispiel #8
0
        /// <summary>
        /// Searches the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="database">The database.</param>
        /// <returns>Returns collection of items</returns>
        public override IEnumerable <Item> Search(Query query, Database database)
        {
            Assert.ArgumentNotNull(query, "query");
            Assert.ArgumentNotNull(database, "database");

            string indexName = string.IsNullOrEmpty(this.IndexName) ? "products" : this.IndexName;

            IEnumerable <Item> items;

            LuceneQueryBuilder builder     = new LuceneQueryBuilder(query, this.Database);
            BooleanQuery       luceneQuery = builder.BuildResultQuery();

            using (IndexSearchContext context = SearchManager.GetIndex(indexName).CreateSearchContext())
            {
                this.AddDecorations(luceneQuery, database.Name);
                PreparedQuery pq = new PreparedQuery(luceneQuery);
                ////Sitecore Query parser does not allow to use IDs in field values.
                SearchHits hits = context.Search(pq, int.MaxValue);
                items = this.GetSearchResultItems(hits);
            }

            return(items);
        }
        public virtual List<SkinnyItem> RunQuery(Query query, bool showAllVersions, string sortField, bool reverse, int start, int end, out int totalResults)
        {
            Assert.ArgumentNotNull(Index, "Demo");

            var items = new List<SkinnyItem>();

            try
            {
                using (var context = new IndexSearchContext(Index))
                {
                    SearchHits searchhits;
                    if (!sortField.IsNullOrEmpty())
                    {
                        // type hardcoded to 3 - means sorting as string
                        var sort = new Sort(new SortField(sortField, 3, reverse));
                        var hits = context.Searcher.Search(query, sort);
                        searchhits = new SearchHits(hits);
                    }
                    else
                    {
                        searchhits = context.Search(query);
                    }

                    if (searchhits == null)
                    {
                        totalResults = 0;
                        return null;
                    }
                    totalResults = searchhits.Length;
                    if (end == 0 || end > searchhits.Length) end = totalResults;
                    var resultCollection = searchhits.FetchResults(start, end);
                    SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
                }
            }
            catch (Exception exception)
            {
                Log.Error("scSearchContrib.Searcher. There was a problem while running a search query. Details: " + exception.Message, this);
                Log.Error(exception.StackTrace, this);
                throw;
            }

            return items;
        }
        public virtual List<SkinnyItem> RunQuery(Query query, bool showAllVersions, Sort sorter, int start, int end, out int totalResults)
        {
            Assert.ArgumentNotNull(Index, "Index");

            var items = new List<SkinnyItem>();

            if (query == null || string.IsNullOrEmpty(query.ToString()))
            {
                Log.Debug("SitecoreSearchContrib: Attempt to execute an empty query.");
                totalResults = 0;
                return items;
            }

            try
            {
                using (var context = new IndexSearchContext(Index))
                {
                    Log.Debug(string.Format("SitecoreSearchContrib: Executing query: {0}", query));
                    SearchHits searchhits;
                    if (sorter != null)
                    {
                        var hits = context.Searcher.Search(query, sorter);
                        searchhits = new SearchHits(hits);
                    }
                    else
                    {
                        searchhits = this.UsePreparedQuery ? context.Search(new PreparedQuery(query)) :
                                                             context.Search(query);
                    }

                    if (searchhits == null)
                    {
                        totalResults = 0;
                        return null;
                    }

                    totalResults = searchhits.Length;
                    if (end == 0 || end > searchhits.Length)
                    {
                        end = totalResults;
                    }

                    Log.Debug(string.Format("SitecoreSearchContrib: Total hits: {0}", totalResults));
                    var resultCollection = searchhits.FetchResults(start, end - start);
                    SearchHelper.GetItemsFromSearchResult(resultCollection, items, showAllVersions);
                    Log.Debug(string.Format("SitecoreSearchContrib: Total results: {0}", resultCollection.Count));
                }
            }
            catch (Exception exception)
            {
                Log.Error("scSearchContrib.Searcher. There was a problem while running a search query. Details: " + exception.Message, this);
                Log.Error(exception.StackTrace, this);
                throw;
            }

            return items;
        }
Beispiel #11
0
        /// <summary>
        /// Gets the items from the GetIndexes()
        /// </summary>
        /// <param name="queryString">
        /// The query string.
        /// </param>
        /// <param name="useQueryParser">
        /// if set to <c>true</c> [use query parser].
        /// </param>
        /// <returns>
        /// Returns Query Result
        /// </returns>
        public QueryResult GetItems(string queryString, bool useQueryParser)
        {
            // Result object used to pass result and errormessages back to sender.
            QueryResult result = new QueryResult();

            List <string> indexes = this.GetIndexes();

            string[] resultItemIds = null;
            foreach (string indexName in indexes)
            {
                string       database = "master";
                HighResTimer timer    = new HighResTimer(true);

                // get the specified index
                Sitecore.Search.Index searchIndex = SearchManager.GetIndex(indexName);

                // get the database to perform the search in..
                Database db = Factory.GetDatabase(database);

                SearchHits hits;
                try
                {
                    if (useQueryParser)
                    {
                        using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
                        {
                            // get a new standard analyser so we can create a query..
                            Analyzer    analyzer    = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                            QueryParser queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, queryString, analyzer);
                            Query       qry         = queryParser.Query("_content");
                            hits = searchContext.Search(qry, int.MaxValue);
                        }
                    }
                    else
                    {
                        using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
                        {
                            // perform the search and get the results back as a Hits list..
                            hits = searchContext.Search(queryString, int.MaxValue);
                        }
                    }

                    result.Success = true;

                    resultItemIds = new string[hits.Length];

                    int i = 0;

                    foreach (Document document in hits.Documents)
                    {
                        string str = document.Get("_docID");
                        resultItemIds[i++] = str;
                    }
                }
                catch (ParseException e)
                {
                    result.ErrorMessage   = e.Message;
                    result.ParseException = e;
                    result.Success        = false;
                }
            }

            result.Result = resultItemIds;
            return(result);
        }