Do we have anything like contains in Unified search?

126 Views Asked by At

I have this line of code

query = query.Filter(i => 
        (
            !i.MatchTypeHierarchy(typeof(InfoPage)) 
            | (((InfoPage)i).SearchSubsection().Exists() 
            & ((InfoPage)i).GetSearchSubSection().Contains(SOMETHING))
        )
    ); // I want to check if it contains

I want to check if there is anything like a string contains a substring in FIND query. 

Thanks for help in advance. :)

1

There are 1 best solutions below

0
On

First things first, you are casting to InfoPage which indicates that you shouldn't be using Unified search, instead use the typed search feature.

Second, contains would typically be referred to as wildcard search.

I wrote a typed search wildcard method a while ago, see https://www.herlitz.io/2016/09/19/episerver-find-wildcard-searching/

public static class SearchExtensions
{
    public static IQueriedSearch<T> WildCardSearch<T>(this ITypeSearch<T> search, string query)
    {
        return search.For<T>(query, q => q.Query = string.Concat("*", query, "*"));
    }
}

Usage example

var result = SearchClient.Instance.Search<InfoPage>()
                    .WildCardSearch(query)
                    .OrderByDescending(x => x.Name)
                    .FilterForVisitor()
                    .GetContentResult();