/// <summary> /// Creates the search sort clause for the element passed in /// </summary> /// <param name="element">Element.</param> /// <returns>SortClause object to be used in a sort expression</returns> private static ISortClause CreateSearchSortClause(SearchResultsOrderSetting element) { ISortClause toReturn = null; switch (element) { case SearchResultsOrderSetting.ForumAscending: toReturn = ForumFields.ForumName.Ascending(); break; case SearchResultsOrderSetting.ForumDescending: toReturn = ForumFields.ForumName.Descending(); break; case SearchResultsOrderSetting.LastPostDateAscending: toReturn = ThreadFields.ThreadLastPostingDate.Ascending(); break; case SearchResultsOrderSetting.LastPostDateDescending: toReturn = ThreadFields.ThreadLastPostingDate.Descending(); break; case SearchResultsOrderSetting.ThreadSubjectAscending: toReturn = ThreadFields.Subject.Ascending(); break; case SearchResultsOrderSetting.ThreadSubjectDescending: toReturn = ThreadFields.Subject.Descending(); break; } return(toReturn); }
/// <summary> /// Does the search using MS Full text search /// </summary> /// <param name="searchString">Search string.</param> /// <param name="forumIDs">Forum Ids of forums to search into.</param> /// <param name="orderFirstElement">Order first element setting.</param> /// <param name="orderSecondElement">Order second element setting.</param> /// <param name="forumsWithThreadsFromOthers">The forums with threads from others.</param> /// <param name="userID">The userid of the calling user.</param> /// <param name="targetToSearch">The target to search.</param> /// <returns> /// TypedList filled with threads matching the query. /// </returns> public static SearchResultTypedList DoSearch(string searchString, List<int> forumIDs, SearchResultsOrderSetting orderFirstElement, SearchResultsOrderSetting orderSecondElement, List<int> forumsWithThreadsFromOthers, int userID, SearchTarget targetToSearch) { // the search utilizes full text search. It performs a CONTAINS upon the MessageText field of the Message entity. string searchTerms = PrepareSearchTerms(searchString); bool searchMessageText = (targetToSearch == SearchTarget.MessageText) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); bool searchSubject = (targetToSearch == SearchTarget.ThreadSubject) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); if(!(searchSubject || searchMessageText)) { // no target specified, select message searchMessageText = true; } PredicateExpression searchTermFilter = new PredicateExpression(); if(searchMessageText) { // Message contents filter searchTermFilter.Add(new FieldCompareSetPredicate(ThreadFields.ThreadID, MessageFields.ThreadID, SetOperator.In, new FieldFullTextSearchPredicate(MessageFields.MessageText, FullTextSearchOperator.Contains, searchTerms))); } if(searchSubject) { // Thread subject filter if(searchMessageText) { searchTermFilter.AddWithOr(new FieldFullTextSearchPredicate(ThreadFields.Subject, FullTextSearchOperator.Contains, searchTerms)); } else { searchTermFilter.Add(new FieldFullTextSearchPredicate(ThreadFields.Subject, FullTextSearchOperator.Contains, searchTerms)); } } IPredicateExpression mainFilter = searchTermFilter .And(ForumFields.ForumID == forumIDs) .And(ThreadGuiHelper.CreateThreadFilter(forumsWithThreadsFromOthers, userID)); ISortExpression sorter = new SortExpression(); // add first element sorter.Add(CreateSearchSortClause(orderFirstElement)); if(orderSecondElement != orderFirstElement) { sorter.Add(CreateSearchSortClause(orderSecondElement)); } SearchResultTypedList results = new SearchResultTypedList(false); try { // get the data from the db. results.Fill(500, sorter, false, mainFilter); } catch { // probably an error with the search words / user error. Swallow for now, which will result in an empty resultset. } return results; }
private async Task PerformSearchAsync(string searchParameters, List <int> forumIDs, SearchResultsOrderSetting orderFirstElement, SearchResultsOrderSetting orderSecondElement, SearchTarget targetToSearch) { var searchTerms = searchParameters.Length > 1024 ? searchParameters.Substring(0, 1024) : searchParameters; var results = await Searcher.DoSearchAsync(searchTerms, forumIDs, orderFirstElement, orderSecondElement, this.HttpContext.Session.GetForumsWithActionRight(ActionRights.ViewNormalThreadsStartedByOthers), this.HttpContext.Session.GetUserID(), targetToSearch); this.HttpContext.Session.AddSearchTermsAndResults(_cache, searchTerms, results); }
private void btnSearch_ServerClick(object sender, System.EventArgs e) { if (Page.IsValid) { // grab forum id's List <int> forumIDs = new List <int>(); for (int i = 0; i < lbxForums.Items.Count; i++) { if (lbxForums.Items[i].Selected) { forumIDs.Add(Convert.ToInt32(lbxForums.Items[i].Value)); } } if (forumIDs.Count <= 0) { // no forums selected, add all of them for (int i = 0; i < lbxForums.Items.Count; i++) { forumIDs.Add(Convert.ToInt32(lbxForums.Items[i].Value)); } } SearchResultsOrderSetting orderFirstElement = (SearchResultsOrderSetting)cbxSortByFirstElement.SelectedIndex; SearchResultsOrderSetting orderSecondElement = (SearchResultsOrderSetting)cbxSortBySecondElement.SelectedIndex; SearchTarget targetToSearch = (SearchTarget)cbxElementToSearch.SelectedIndex; string searchTerms = tbxSearchString.Value; if (searchTerms.Length > 1024) { searchTerms = searchTerms.Substring(0, 1024); } // Use Full text search variant. SearchResultTypedList results = BL.Searcher.DoSearch(searchTerms, forumIDs, orderFirstElement, orderSecondElement, SessionAdapter.GetForumsWithActionRight(ActionRights.ViewNormalThreadsStartedByOthers), SessionAdapter.GetUserID(), targetToSearch); // store results in session. SessionAdapter.AddSearchTermsAndResults(searchTerms, results); // view results. Response.Redirect("SearchResults.aspx?Page=1", true); } }
/// <summary> /// Does the search using MS Full text search /// </summary> /// <param name="searchString">Search string.</param> /// <param name="forumIDs">Forum Ids of forums to search into.</param> /// <param name="orderFirstElement">Order first element setting.</param> /// <param name="orderSecondElement">Order second element setting.</param> /// <param name="forumsWithThreadsFromOthers">The forums with threads from others.</param> /// <param name="userID">The userid of the calling user.</param> /// <param name="targetToSearch">The target to search.</param> /// <returns> /// TypedList filled with threads matching the query. /// </returns> public static SearchResultTypedList DoSearch(string searchString, List <int> forumIDs, SearchResultsOrderSetting orderFirstElement, SearchResultsOrderSetting orderSecondElement, List <int> forumsWithThreadsFromOthers, int userID, SearchTarget targetToSearch) { // the search utilizes full text search. It performs a CONTAINS upon the MessageText field of the Message entity. string searchTerms = PrepareSearchTerms(searchString); bool searchMessageText = (targetToSearch == SearchTarget.MessageText) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); bool searchSubject = (targetToSearch == SearchTarget.ThreadSubject) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); if (!(searchSubject || searchMessageText)) { // no target specified, select message searchMessageText = true; } PredicateExpression searchTermFilter = new PredicateExpression(); if (searchMessageText) { // Message contents filter searchTermFilter.Add(new FieldCompareSetPredicate(ThreadFields.ThreadID, MessageFields.ThreadID, SetOperator.In, new FieldFullTextSearchPredicate(MessageFields.MessageText, FullTextSearchOperator.Contains, searchTerms))); } if (searchSubject) { // Thread subject filter if (searchMessageText) { searchTermFilter.AddWithOr(new FieldFullTextSearchPredicate(ThreadFields.Subject, FullTextSearchOperator.Contains, searchTerms)); } else { searchTermFilter.Add(new FieldFullTextSearchPredicate(ThreadFields.Subject, FullTextSearchOperator.Contains, searchTerms)); } } IPredicateExpression mainFilter = searchTermFilter .And(ForumFields.ForumID == forumIDs) .And(ThreadGuiHelper.CreateThreadFilter(forumsWithThreadsFromOthers, userID)); ISortExpression sorter = new SortExpression(); // add first element sorter.Add(CreateSearchSortClause(orderFirstElement)); if (orderSecondElement != orderFirstElement) { sorter.Add(CreateSearchSortClause(orderSecondElement)); } SearchResultTypedList results = new SearchResultTypedList(false); try { // get the data from the db. results.Fill(500, sorter, false, mainFilter); } catch { // probably an error with the search words / user error. Swallow for now, which will result in an empty resultset. } return(results); }
/// <summary> /// Does the search using MS Full text search /// </summary> /// <param name="searchString">Search string.</param> /// <param name="forumIds">Forum Ids of forums to search into.</param> /// <param name="orderFirstElement">Order first element setting.</param> /// <param name="orderSecondElement">Order second element setting.</param> /// <param name="forumsWithThreadsFromOthers">The forums with threads from others.</param> /// <param name="userId">The userid of the calling user.</param> /// <param name="targetToSearch">The target to search.</param> /// <returns> /// TypedList filled with threads matching the query. /// </returns> public static async Task <List <SearchResultRow> > DoSearchAsync(string searchString, List <int> forumIds, SearchResultsOrderSetting orderFirstElement, SearchResultsOrderSetting orderSecondElement, List <int> forumsWithThreadsFromOthers, int userId, SearchTarget targetToSearch) { // the search utilizes full text search. It performs a CONTAINS upon the MessageText field of the Message entity. var searchTerms = PrepareSearchTerms(searchString); var searchMessageText = (targetToSearch == SearchTarget.MessageText) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); var searchSubject = (targetToSearch == SearchTarget.ThreadSubject) || (targetToSearch == SearchTarget.MessageTextAndThreadSubject); if (!(searchSubject || searchMessageText)) { // no target specified, select message searchMessageText = true; } var qf = new QueryFactory(); var searchTermFilter = new PredicateExpression(); if (searchMessageText) { // Message contents filter searchTermFilter.Add(ThreadFields.ThreadID.In(qf.Create() .Select(MessageFields.ThreadID) .Where(new FieldFullTextSearchPredicate(MessageFields.MessageText, null, FullTextSearchOperator.Contains, searchTerms)))); } if (searchSubject) { // Thread subject filter if (searchMessageText) { searchTermFilter.AddWithOr(new FieldFullTextSearchPredicate(ThreadFields.Subject, null, FullTextSearchOperator.Contains, searchTerms)); } else { searchTermFilter.Add(new FieldFullTextSearchPredicate(ThreadFields.Subject, null, FullTextSearchOperator.Contains, searchTerms)); } } var q = qf.GetSearchResultTypedList() .Where(searchTermFilter .And(ForumFields.ForumID.In(forumIds)) .And(ThreadGuiHelper.CreateThreadFilter(forumsWithThreadsFromOthers, userId))) .Limit(500) .OrderBy(CreateSearchSortClause(orderFirstElement)) .Distinct(); if (orderSecondElement != orderFirstElement) { // simply call OrderBy again, it will append the sortclause to the existing one. q.OrderBy(CreateSearchSortClause(orderSecondElement)); } List <SearchResultRow> toReturn; try { // get the data from the db. using (var adapter = new DataAccessAdapter()) { toReturn = await adapter.FetchQueryAsync(q).ConfigureAwait(false); } } catch { // probably an error with the search words / user error. Swallow for now, which will result in an empty resultset. toReturn = new List <SearchResultRow>(); } return(toReturn); }
/// <summary> /// Creates the search sort clause for the element passed in /// </summary> /// <param name="element">Element.</param> /// <returns>SortClause object to be used in a sort expression</returns> private static ISortClause CreateSearchSortClause(SearchResultsOrderSetting element) { ISortClause toReturn = null; switch(element) { case SearchResultsOrderSetting.ForumAscending: toReturn = ForumFields.ForumName.Ascending(); break; case SearchResultsOrderSetting.ForumDescending: toReturn = ForumFields.ForumName.Descending(); break; case SearchResultsOrderSetting.LastPostDateAscending: toReturn = ThreadFields.ThreadLastPostingDate.Ascending(); break; case SearchResultsOrderSetting.LastPostDateDescending: toReturn = ThreadFields.ThreadLastPostingDate.Descending(); break; case SearchResultsOrderSetting.ThreadSubjectAscending: toReturn = ThreadFields.Subject.Ascending(); break; case SearchResultsOrderSetting.ThreadSubjectDescending: toReturn = ThreadFields.Subject.Descending(); break; } return toReturn; }