public ISearchResult Search(SearchCriteria criteria)
        {
            if (criteria == null || string.IsNullOrWhiteSpace(criteria.Text))
            {
                return(new SearchResult());
            }

            var timer = new Sitecore.Diagnostics.HighResTimer(true);

            var index = ContentSearchManager.GetIndex("sitecore_master_index") as SpatialLuceneIndex;

            using (var context = index.CreateSearchContext() as SpatialLuceneSearchContext)
            {
                if (criteria.PointRadius != null)
                {
                    context.SetGeoFilter(new PointRadiusCriterion()
                    {
                        FieldName = "_geohash",
                        RadiusKm  = criteria.PointRadius.RadiusKm,
                        Latitude  = criteria.PointRadius.Latitude,
                        Longitude = criteria.PointRadius.Longitude,
                        Sort      = PointRadiusCriterion.SortOption.Ascending
                    });
                }

                var queriable = context.GetQueryable <SearchResultItem>();

                Expression <Func <SearchResultItem, bool> > predicate = PredicateBuilder.True <SearchResultItem>();
                foreach (var entry in criteria.Text.Split(' '))
                {
                    predicate = predicate.And(i => i.Content == entry);
                }

                var result = queriable
                             .Where(predicate)
                             .Page(criteria.CurrentPage - 1, criteria.PageSize)
                             .FacetPivotOn(p => p.FacetOn(d => d["categories"]).FacetOn(d => d["haspoint"]))
                             .GetResults();

                var searchResult = new SearchResult()
                {
                    ResultDocuments    = result.Hits.Select(hit => hit.Document).ToList(),
                    Facets             = CollectFacets(result.Facets.Categories),
                    TotalSearchResults = result.TotalSearchResults,
                };
                searchResult.TotalMilliseconds = timer.ElapsedTimeSpan.Milliseconds;

                return(searchResult);
            }
        }
        public ISearchResult Search(SearchCriteria criteria)
        {
            if (criteria == null || string.IsNullOrWhiteSpace(criteria.Text))
                return new SearchResult();

            var timer = new Sitecore.Diagnostics.HighResTimer(true);

            var index = ContentSearchManager.GetIndex("sitecore_master_index") as SpatialLuceneIndex;

            using (var context = index.CreateSearchContext() as SpatialLuceneSearchContext)
            {
                if (criteria.PointRadius != null)
                {
                    context.SetGeoFilter(new PointRadiusCriterion()
                    {
                        FieldName = "_geohash",
                        RadiusKm = criteria.PointRadius.RadiusKm,
                        Latitude = criteria.PointRadius.Latitude,
                        Longitude = criteria.PointRadius.Longitude,
                        Sort = PointRadiusCriterion.SortOption.Ascending
                    });
                }

                var queriable = context.GetQueryable<SearchResultItem>();

                Expression<Func<SearchResultItem, bool>> predicate = PredicateBuilder.True<SearchResultItem>();
                foreach(var entry in criteria.Text.Split(' '))
                {
                    predicate = predicate.And(i => i.Content == entry);
                }
                
                var result = queriable
                    .Where(predicate)
                    .Page(criteria.CurrentPage-1, criteria.PageSize)
                    .FacetPivotOn(p => p.FacetOn(d => d["categories"]).FacetOn(d => d["haspoint"]))
                    .GetResults();

                var searchResult = new SearchResult()
                {
                    ResultDocuments = result.Hits.Select(hit => hit.Document).ToList(),
                    Facets = CollectFacets(result.Facets.Categories),
                    TotalSearchResults = result.TotalSearchResults,
                };
                searchResult.TotalMilliseconds = timer.ElapsedTimeSpan.Milliseconds;

                return searchResult;
            }
        }
        public ActionResult Dragons()
        {
            var index = ContentSearchManager.GetIndex("sitecore_master_index");

            using (var context = index.CreateSearchContext())
            {
                var queryable = context.GetQueryable<SearchResultItem>();

                // Note how the Lamda is exactly the same for these two types. The compiler whether
                // it needs to create it as an expression tree or an anonymous function.
                Expression<Func<SearchResultItem, bool>> expression = (i => i.Name.Contains("Standard"));
                Func<SearchResultItem, bool> func =                   (i => i.Name.Contains("Standard"));

                // Using intellisense on the Where you can see that it applies to a IQueryable
                var timer1 = new Sitecore.Diagnostics.HighResTimer();
                timer1.Start();
                var result1 = queryable
                    .Where(expression)
                    .ToList();
                timer1.Stop();

                // Using intellisense on this Where you can see that it applies to IEnumerable
                var timer2 = new Sitecore.Diagnostics.HighResTimer();
                timer2.Start();
                var result2 = queryable
                    .Where(func)
                    .ToList();
                timer2.Stop();

                ViewBag.Timer1 = timer1.ElapsedTimeSpan.Milliseconds;
                ViewBag.Timer2 = timer2.ElapsedTimeSpan.Milliseconds;

                // If you look in the Search log you can see something that looks like:
                //
                // 10408 20:30:26 INFO  ExecuteQueryAgainstLucene : _name:*standard* - Filter : 
                // 10408 20:30:26 INFO  ExecuteQueryAgainstLucene : *:* - Filter : 
                //
                // What can we tell from that? When using the same Lamda expression against an 
                // IQueryable and an IEnumerable - the IEnumerable will fetch EVERYTHING from the 
                // database and then apply the Where filter as Linq to Object - rather than doing
                // the query in the index.
                ViewBag.Result1 = result1;
                ViewBag.Result2 = result2;

                return View(result2);
            }
        }