public async Task <IActionResult> Query(AdminQueryViewModel model) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageIndexes)) { return(Unauthorized()); } if (!_luceneIndexProvider.Exists(model.IndexName)) { return(NotFound()); } if (String.IsNullOrWhiteSpace(model.Query)) { return(View(model)); } var queryParser = new QueryParser(_luceneSettings.GetVersion(), "", _luceneSettings.GetAnalyzer()); var query = queryParser.Parse(model.Query); _luceneIndexProvider.Search(model.IndexName, searcher => { var docs = searcher.Search(query, 10); model.Documents = docs.ScoreDocs.Select(hit => searcher.Doc(hit.Doc)).ToList(); }); return(View(model)); }
public async Task <IActionResult> Index(string id, string q, PagerParameters pagerParameters) { var siteSettings = await _siteService.GetSiteSettingsAsync(); Pager pager = new Pager(pagerParameters, siteSettings.PageSize); var indexName = "Search"; if (!String.IsNullOrWhiteSpace(id)) { indexName = id; } if (!_luceneIndexProvider.Exists(indexName)) { return(NotFound()); } if (String.IsNullOrWhiteSpace(q)) { return(View(new SearchIndexViewModel { Pager = pager, IndexName = id, ContentItems = Enumerable.Empty <ContentItem>() })); } var queryParser = new MultiFieldQueryParser(_luceneSettings.GetVersion(), _luceneSettings.GetSearchFields(), _luceneSettings.GetAnalyzer()); var query = queryParser.Parse(QueryParser.Escape(q)); List <int> contentItemIds = new List <int>(); _luceneIndexProvider.Search(indexName, searcher => { // Fetch one more result than PageSize to generate "More" links TopScoreDocCollector collector = TopScoreDocCollector.Create(pager.PageSize + 1, true); searcher.Search(query, collector); TopDocs hits = collector.TopDocs(pager.GetStartIndex(), pager.PageSize + 1); foreach (var hit in hits.ScoreDocs) { var d = searcher.Doc(hit.Doc, IdSet); contentItemIds.Add(Convert.ToInt32(d.GetField("ContentItemId").StringValue)); } }); var contentItems = new List <ContentItem>(); foreach (var contentItemId in contentItemIds.Take(pager.PageSize)) { var contentItem = await _contentManager.GetAsync(contentItemId); if (contentItem != null) { contentItems.Add(contentItem); } } var model = new SearchIndexViewModel { HasMoreResults = contentItemIds.Count > pager.PageSize, Query = q, Pager = pager, IndexName = id, ContentItems = contentItems }; return(View(model)); }