public void AndOrCombinations() { var x = Expression.Parameter(typeof(char), "x"); var p1 = new PredicateExpression { Predicate = Expression.Equal(x, Expression.Constant('x')) }; var p2 = new PredicateExpression { Predicate = Expression.Equal(x, Expression.Constant('y')) }; var p = p1.And(p2); var p3 = new PredicateExpression { Predicate = Expression.Equal(x, Expression.Constant('z')) }; p = p.And(p3); var andp = (AndExpression)p; foreach (var component in andp.Members) { Assert.IsInstanceOf<PredicateExpression>(component); } p = p1.Or(p2).Or(p3); var orp = (OrExpression)p; foreach (var component in orp.Members) { Assert.IsInstanceOf<PredicateExpression>(component); } }
/// <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> /// 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; }