public IEnumerable <BrowseSelection> GetSelections(ResourceSearchQuery query) { return(from h in _handlers let s = h.GetSelection(query) where s != null select s); }
Filter IContentHandler.GetFilter(ResourceSearchQuery searchQuery) { if (!searchQuery.SubcategoryId.HasValue && !searchQuery.CategoryId.HasValue) { return(null); } var filter = new TermsFilter(); //use subcategory in preference to category if (searchQuery.SubcategoryId.HasValue) { filter.addTerm(new Term(FieldName.SubcategoryId, searchQuery.SubcategoryId.Value.ToFieldValue())); } else { var category = _categories.FirstOrDefault(c => c.Id == searchQuery.CategoryId); if (category != null) { foreach (var subcategory in category.Subcategories) { filter.addTerm(new Term(FieldName.SubcategoryId, subcategory.Id.ToFieldValue())); } } } return(filter); }
public LuceneQuery GetQuery(ResourceSearchQuery searchQuery) { var fieldQueries = CreateFieldQueries(searchQuery); var query = fieldQueries.CombineQueries(BooleanClause.Occur.MUST); return(query ?? new MatchAllDocsQuery()); }
private LuceneQuery[] CreateFieldQueries(ResourceSearchQuery searchQuery) { return(new[] { GetContentQuery(searchQuery) }); }
Sort IContentHandler.GetSort(ResourceSearchQuery sortQuery) { return(new Sort(new[] { //reverse to make highest to lowest the default new SortField(FieldName.Popularity, SortField.INT, !sortQuery.ReverseSortOrder), SortField.FIELD_SCORE })); }
public LuceneFilter GetFilter(ResourceSearchQuery query) { var filters = (from h in _handlers let f = h.GetFilter(query) where f != null select f).ToList(); return(filters.Count == 0 ? null : new ChainedFilter(filters.ToArray(), ChainedFilter.AND)); }
protected ResourceSearchResults Search(ResourceSearchQuery resourceQuery, float minScore) { var reader = _indexWriter.getReader(); var searcher = new Searcher(reader); var query = _indexer.GetQuery(resourceQuery); var filter = _indexer.GetFilter(resourceQuery); var selections = _indexer.GetSelections(resourceQuery); var sort = _indexer.GetSort(resourceQuery); var searchResults = searcher.Search(query, filter, selections, sort == null ? null : sort.getSort(), resourceQuery.Skip, resourceQuery.Take ?? reader.maxDoc(), true); return(searchResults); }
ResourceSearchResults IResourceSearchService.Search(ResourceSearchQuery searchQuery, bool includeFacets) { const string method = "Search"; try { #region Log Stopwatch searchTime = null; if (EventSource.IsEnabled(Event.Trace)) { searchTime = Stopwatch.StartNew(); } #endregion var query = _indexer.GetQuery(searchQuery); var filter = _indexer.GetFilter(searchQuery); var selections = _indexer.GetSelections(searchQuery); var sort = _indexer.GetSort(searchQuery); #region Log if (EventSource.IsEnabled(Event.Trace)) { EventSource.Raise(Event.Trace, method, "Executing query.", Event.Arg("query", (query != null) ? query.toString() : string.Empty)); } #endregion var reader = GetReader(); var searcher = new Searcher(reader); var sorts = (sort != null) ? sort.getSort() : null; var searchResults = searcher.Search(query, filter, selections, sorts, searchQuery.Skip, searchQuery.Take ?? reader.maxDoc(), includeFacets); #region Log if (searchTime != null) { searchTime.Stop(); EventSource.Raise(Event.Trace, method, "Query execution complete.", Event.Arg("query", (query != null) ? query.toString() : string.Empty), Event.Arg("total hits", searchResults.TotalMatches), Event.Arg("result count", searchResults.ResourceIds.Count), Event.Arg("searchTime", searchTime.ElapsedMilliseconds)); } #endregion return(searchResults); } catch (Exception e) { #region Log EventSource.Raise(Event.Error, method, "Unexpected exception.", e); #endregion throw; } }
private void TestViewedItems <TResource>(ResourceType type, Func <IList <TResource> > getItems, Func <Guid, ReadOnlyUrl> getUrl) where TResource : Resource { // Pick some random items. var items = getItems(); var item1 = items[5]; var item2 = items[7]; // Search. var query = new ResourceSearchQuery { SortOrder = ResourceSortOrder.Popularity, ResourceType = type }; var results = _service1.Search(query, true); Assert.AreEqual(items.Count, results.ResourceIds.Count); results = _service2.Search(query, true); Assert.AreEqual(items.Count, results.ResourceIds.Count); // View the items to push them to the top. var url = getUrl(item1.Id); for (var index = 0; index < 5; ++index) { AssertJsonSuccess(Deserialize <JsonResponseModel>(Post(url))); } url = getUrl(item2.Id); for (var index = 0; index < 7; ++index) { AssertJsonSuccess(Deserialize <JsonResponseModel>(Post(url))); } // Do some searches again. results = _service1.Search(query, true); Assert.AreEqual(items.Count, results.ResourceIds.Count); Assert.AreEqual(item2.Id, results.ResourceIds[0]); Assert.AreEqual(item1.Id, results.ResourceIds[1]); // Wait for the polling to kick in. Thread.Sleep(3 * MonitorInterval * 1000); results = _service2.Search(query, true); Assert.AreEqual(items.Count, results.ResourceIds.Count); Assert.AreEqual(item2.Id, results.ResourceIds[0]); Assert.AreEqual(item1.Id, results.ResourceIds[1]); }
BrowseSelection IContentHandler.GetSelection(ResourceSearchQuery searchQuery) { if (searchQuery.ResourceType == null) { return(null); } var selection = new BrowseSelection(FieldName.ItemType); selection.addValue(searchQuery.ResourceType.Value.Encode()); return(selection); }
public void MultipleTypesTest() { //Create items var subcategory1 = Guid.NewGuid(); var article1 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now.AddDays(-2), SubcategoryId = subcategory1, Text = "An article", Title = "An article title", }; IndexItem(article1); var qna1 = new QnA { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "An answered question", Title = "An answered question title", }; IndexItem(qna1); var faq1 = new Faq { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "How do I do this", Title = "Read the manual", }; IndexItem(faq1); // search var resourceQuery = new ResourceSearchQuery { ResourceType = ResourceType.Article }; var results = Search(resourceQuery, 10); Assert.AreEqual(1, results.ResourceIds.Count); Assert.AreEqual(article1.Id, results.ResourceIds[0]); }
public LuceneSort GetSort(ResourceSearchQuery query) { switch (query.SortOrder) { case ResourceSortOrder.Relevance: // Default sort. return(null); case ResourceSortOrder.CreatedTime: return(_createdTimeHandler.GetSort(query)); case ResourceSortOrder.Popularity: return(_popularityHandler.GetSort(query)); default: return(null); } }
public void PopularitySortTest() { //Create items var subcategory1 = Guid.NewGuid(); var article1 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now.AddDays(-2), SubcategoryId = subcategory1, Text = "An article", Title = "An article title", }; IndexItem(article1); var article2 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "Another article", Title = "Another article title", }; IndexItem(article2); var article3 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "Yet another article", Title = "Yet another article title", }; var userId = Guid.NewGuid(); _resourcesCommand.ViewArticle(userId, article3.Id); _resourcesCommand.ViewArticle(Guid.NewGuid(), article3.Id); IndexItem(article3); var article4 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "Still another article", Title = "Still another article title", }; IndexItem(article4); // Sort by popularity. var resourceQuery = new ResourceSearchQuery { SortOrder = ResourceSortOrder.Popularity, ResourceType = ResourceType.Article }; var results = Search(resourceQuery, 10); Assert.AreEqual(4, results.ResourceIds.Count); Assert.AreEqual(article3.Id, results.ResourceIds[0]); // View and reindex _resourcesCommand.ViewArticle(userId, article4.Id); _resourcesCommand.ViewArticle(Guid.NewGuid(), article4.Id); _resourcesCommand.ViewArticle(Guid.NewGuid(), article4.Id); _resourcesCommand.ViewArticle(Guid.NewGuid(), article4.Id); IndexItem(article4, false); resourceQuery = new ResourceSearchQuery { SortOrder = ResourceSortOrder.Popularity, ResourceType = ResourceType.Article }; results = Search(resourceQuery, 10); Assert.AreEqual(4, results.ResourceIds.Count); Assert.AreEqual(article4.Id, results.ResourceIds[0]); Assert.AreEqual(article3.Id, results.ResourceIds[1]); // View article 3 again by the same person _resourcesCommand.ViewArticle(userId, article3.Id); _resourcesCommand.ViewArticle(userId, article3.Id); _resourcesCommand.ViewArticle(userId, article3.Id); IndexItem(article3, false); resourceQuery = new ResourceSearchQuery { SortOrder = ResourceSortOrder.Popularity, ResourceType = ResourceType.Article }; results = Search(resourceQuery, 10); Assert.AreEqual(4, results.ResourceIds.Count); Assert.AreEqual(article3.Id, results.ResourceIds[0]); Assert.AreEqual(article4.Id, results.ResourceIds[1]); }
public LuceneQuery GetContentQuery(ResourceSearchQuery query) { return(query.Keywords.GetLuceneQuery(FieldName.Content, _queryAnalyzer)); }
protected ResourceSearchResults Search(ResourceSearchQuery resourceQuery) { return(Search(resourceQuery, 0)); }
BrowseSelection IContentHandler.GetSelection(ResourceSearchQuery searchQuery) { return(null); }
Sort IContentHandler.GetSort(ResourceSearchQuery sortQuery) { return(null); }
public LuceneQuery GetQuery(ResourceSearchQuery query) { return(_queryBuilder.GetQuery(query)); }
public void CreatedTimeSortTest() { //Create items var subcategory1 = Guid.NewGuid(); var article1 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now.AddDays(-2), SubcategoryId = subcategory1, Text = "An article", Title = "An article title", }; IndexItem(article1); var article2 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, SubcategoryId = subcategory1, Text = "Another article", Title = "Another article title", }; IndexItem(article2); var article3 = new Article { Id = Guid.NewGuid(), CreatedTime = DateTime.Now.AddDays(-5), SubcategoryId = subcategory1, Text = "Another article", Title = "Another article title", }; IndexItem(article3); var video = new Video { Id = Guid.NewGuid(), CreatedTime = DateTime.Now, ExternalVideoId = "12345", SubcategoryId = subcategory1, Text = "A Video", Title = "A video title", }; IndexItem(video); var qna = new QnA { Id = Guid.NewGuid(), CreatedTime = DateTime.Now.AddDays(-1), SubcategoryId = subcategory1, Text = "An answer", Title = "A question", }; IndexItem(qna); // Sort by createdTime. var query = new ResourceSearchQuery { SortOrder = ResourceSortOrder.CreatedTime, ResourceType = ResourceType.Article }; var results = Search(query, 10); Assert.AreEqual(3, results.ResourceIds.Count); Assert.AreEqual(article2.Id, results.ResourceIds[0]); Assert.AreEqual(article1.Id, results.ResourceIds[1]); Assert.AreEqual(article3.Id, results.ResourceIds[2]); var resourceTypeHits = results.ResourceTypeHits.ToDictionary(h => h.Key, h => h.Value); Assert.AreEqual(3, resourceTypeHits[ResourceType.Article]); Assert.AreEqual(1, resourceTypeHits[ResourceType.Video]); Assert.AreEqual(1, resourceTypeHits[ResourceType.QnA]); query = new ResourceSearchQuery { SortOrder = ResourceSortOrder.CreatedTime, ReverseSortOrder = false, ResourceType = ResourceType.Article }; results = Search(query, 10); Assert.AreEqual(3, results.ResourceIds.Count); Assert.AreEqual(article2.Id, results.ResourceIds[0]); Assert.AreEqual(article1.Id, results.ResourceIds[1]); Assert.AreEqual(article3.Id, results.ResourceIds[2]); resourceTypeHits = results.ResourceTypeHits.ToDictionary(h => h.Key, h => h.Value); Assert.AreEqual(3, resourceTypeHits[ResourceType.Article]); Assert.AreEqual(1, resourceTypeHits[ResourceType.Video]); Assert.AreEqual(1, resourceTypeHits[ResourceType.QnA]); query = new ResourceSearchQuery { SortOrder = ResourceSortOrder.CreatedTime, ReverseSortOrder = true, ResourceType = ResourceType.Article }; results = Search(query, 10); Assert.AreEqual(3, results.ResourceIds.Count); Assert.AreEqual(article3.Id, results.ResourceIds[0]); Assert.AreEqual(article1.Id, results.ResourceIds[1]); Assert.AreEqual(article2.Id, results.ResourceIds[2]); resourceTypeHits = results.ResourceTypeHits.ToDictionary(h => h.Key, h => h.Value); Assert.AreEqual(3, resourceTypeHits[ResourceType.Article]); Assert.AreEqual(1, resourceTypeHits[ResourceType.Video]); Assert.AreEqual(1, resourceTypeHits[ResourceType.QnA]); }
Sort IContentHandler.GetSort(ResourceSearchQuery searchQuery) { // Reverse the natural order, i.e. newest at the top. return(GetSort(!searchQuery.ReverseSortOrder)); }
LuceneFilter IContentHandler.GetFilter(ResourceSearchQuery searchQuery) { return(null); }