Try our conversational search powered by Generative AI!

Filtering with OR operator

Vote:
 

Hello,

Now here's one for you. I am applying filtering but want it to be OR so that if two filters are selected it resturns results that match both filters, if I turn one off it only shows results for the remaining.

I'm using the below where selected filters is a List<string>

                foreach (string filter in this.SelectedFilters)
                {
                    query = query.Filter(x => x.SearchCategories.MatchCaseInsensitive(filter));
                }

Now where there is more than one filter this works using AND which is logical and what you would expect. I need this to work as OR but can't find any examples of how I might achieve this.

Sorry this is probably really obvious but some pointers would be good.

Thanks in advance,

Mark

#79011
Dec 06, 2013 11:54
Vote:
 

Hi,

you can use .OrFilter(...) to do that.

http://find.episerver.com/Documentation/dotnet-api-filtering

#79012
Dec 06, 2013 12:12
Vote:
 

Hi Per,

Thanks for that, I can't believe I didn't spot that!

AlIhough it works it doesn't quite help with my scenario though. I'm building a query with section and sub section and then adding any selected filters afterwards. While this mostly works it's producing an OR where not quite needed when I have more than one filter, what it's doing is:

Section AND SubSection AND Filter1 OR Filter2

What I need is something like

Section AND SubSection AND (Filter1 OR Filter2)

My query is as follows:

var query =
           SearchClient.Instance.UnifiedSearchFor(this.SearchTerm)
           .TermsFacetFor(x => x.SearchSection)
           .TermsFacetFor(x => x.SearchSubsection)
           .Skip((PagingPage - 1) * CurrentPage.PageSize)
           .Take(CurrentPage.PageSize);

            if (!String.IsNullOrWhiteSpace(SearchSection))
            {
                query = query.Filter(x => x.SearchSection.Match(SearchSection));
            }

            if (!String.IsNullOrWhiteSpace(SearchSubSection))
            {
                query = query.Filter(x => x.SearchSubsection.MatchCaseInsensitive(SearchSubSection));
            }

           foreach (string filter in this.SelectedFilters)
           {
               if (this.SelectedFilters.First() == filter)
               {
                   query = query.Filter(x => x.SearchCategories.MatchCaseInsensitive(filter));
               }
               else
               {
                   query = query.OrFilter(x => x.SearchCategories.MatchCaseInsensitive(filter));
               }
           }
 

#79021
Dec 06, 2013 15:19
Vote:
 

Check out the BuildFilter<T> method, it is documentet on the page I linked to earlier. Maybe it will solve your problem.

#79024
Dec 06, 2013 15:29
Vote:
 

Will that work with UnifiedSearch?

#79025
Dec 06, 2013 15:33
Vote:
 

Seems to work as expected, thanks Per,

Mark

#79085
Dec 09, 2013 12:29
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.