Ejemplo n.º 1
0
        public SearchResults Search(SearchRequest request)
        {
            if (string.IsNullOrEmpty(request.Query))
            {
                return new SearchResults();
            }

            var take = request.Take > 0 ? request.Take : SearchModuleConstants.DefaultSearchResultsCount;

            var url = string.Format(GoogleSiteSearchUrl, 
                configuration.Search.GetValue("GoogleSiteSearchApiKey"), 
                configuration.Search.GetValue("GoogleSiteSearchEngineKey"), 
                HttpUtility.UrlEncode(request.Query),
                take);

            if (request.Skip > 0)
            {
                url = string.Format("{0}&{1}={2}", url, SkipParameterName, request.Skip);
            }

            var data = webClient.DownloadData(url);

            var results = JsonConvert.DeserializeObject<GoogleSearchResults>(data);

            results.Query = request.Query;
            results.TotalResults = results.SearchInformation.TotalResults;

            return results;
        }
Ejemplo n.º 2
0
        public SearchResults Search(SearchRequest request)
        {
            if (!OpenReader())
            {
                return new SearchResults { Query = request.Query };
            }

            if (!reader.IsCurrent())
            {
                reader = reader.Reopen();
            }

            var take = request.Take > 0 ? request.Take : SearchModuleConstants.DefaultSearchResultsCount;
            var skip = request.Skip > 0 ? request.Skip : 0;

            var result = new List<SearchResultItem>();
            TopScoreDocCollector collector = TopScoreDocCollector.Create(take + skip, true);
            using (var searcher = new IndexSearcher(reader))
            {
                var searchQuery = request.Query;
                Query query;
                try
                {
                    query = parser.Parse(searchQuery);
                }
                catch (ParseException)
                {
                    try
                    {
                        searchQuery = QueryParser.Escape(searchQuery);
                        query = parser.Parse(searchQuery);
                    }
                    catch (ParseException exc)
                    {
                        throw new ValidationException(() => exc.Message, exc.Message, exc);
                    }
                }

                Filter isPublishedFilter = null;
                if (!RetrieveUnpublishedPages())
                {
                    var isPublishedQuery = new TermQuery(new Term(LuceneIndexDocumentKeys.IsPublished, "true"));
                    isPublishedFilter = new QueryWrapperFilter(isPublishedQuery);
                }

                if (LuceneSearchHelper.Search != null)
                {
                    collector = LuceneSearchHelper.Search(query, isPublishedFilter, collector);
                }
                else
                {
                    query = LuceneEvents.Instance.OnSearchQueryExecuting(query, searchQuery).Query;

                    if (isPublishedFilter != null)
                    {
                        // Exclude unpublished pages
                        searcher.Search(query, isPublishedFilter, collector);
                    }
                    else
                    {
                        // Search within all the pages
                        searcher.Search(query, collector);
                    }
                }

                ScoreDoc[] hits = collector.TopDocs(skip, take).ScoreDocs;
                List<Document> hitDocuments = new List<Document>();
                for (int i = 0; i < hits.Length; i++)
                {
                    int docId = hits[i].Doc;
                    Document d = searcher.Doc(docId);
                    hitDocuments.Add(d);
                    result.Add(
                        new SearchResultItem
                        {
                            FormattedUrl = d.Get(LuceneIndexDocumentKeys.Path),
                            Link = d.Get(LuceneIndexDocumentKeys.Path),
                            Title = d.Get(LuceneIndexDocumentKeys.Title),
                            Snippet = GetSnippet(d.Get(LuceneIndexDocumentKeys.Content), request.Query)
                        });
                }

                CheckAvailability(result);

                LuceneEvents.Instance.OnSearchResultRetrieving(hitDocuments, result);
            }

            return new SearchResults
                       {
                           Items = result,
                           Query = request.Query,
                           TotalResults = collector.TotalHits
                       };
        }