Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Cannot filter string by contains

Vote:
 

Hi, 

I need to filter on search based on if a string contians something. I couldnt find any info in the docs (https://world.episerver.com/documentation/developer-guides/search-navigation/NET-Client-API/searching/Filtering/Strings/ ) or in the code (EPiServer.Find.Filters). 

I tried to use MatchContained also but that requires another lambda and I cant pass anything that makes sense. Here is some code I tried to accomplish this:

If I try the following:
var domainFilter = searchClient.BuildFilter<ISearchContent>();
            domainFilter = domainFilter.Or(x => x.SearchHitUrl.Prefix(startPage));

            domainFilter = domainFilter.Or(y => y.SearchHitUrl.MatchContained(a => a.Key,"financial"));

var query = querySearch

                .Filter(domainFilter)

It says "Char does not contain definition for KEY", even though other form posts listed this as a solution.

I also tried this:

var domainFilter = searchClient.BuildFilter<ISearchContent>();
            domainFilter = domainFilter.Or(x => x.SearchHitUrl.Prefix(startPage));

                domainFilter = domainFilter.Or(y => y.SearchHitUrl.MatchContained(a => a.ToString(),"financial"));

var query = querySearch

                .Filter(domainFilter)

Also tried the following which also didnt return the correct results:

var domainFilter = searchClient.BuildFilter<ISearchContent>();
            domainFilter = domainFilter.Or(x => x.SearchHitUrl.Prefix(startPage));

                 domainFilter = domainFilter.Or(x => x.SearchHitUrl.Contains("financial").Match(true));

var query = querySearch

                .Filter(domainFilter)

But it doesnt show the results where SearchHitUrl contains financial?

Anyone have any ideas?

Episerver Find v 13.2.5

#249478
Edited, Mar 02, 2021 17:04
Vote:
 

Can you try the below code?

  • Include the field in search

SearchClient.Instance.Convention.ForInstancesOf<YourPageType>().IncludeField(x => x.SearchHitUrl);

  • And then apply filter

var domainFilter= domainFilter.And(x => x.SearchHitUrl.MatchCaseInsensitive(keys));

#249546
Mar 04, 2021 6:29
Vote:
 

To summarize the issue, the search hit URL must be part of the filtering that goes into the search before it is executed.  I don't know if that creates a problem with how the search works but I'm not aware of other options to search across a multi site setup.  It should be easier to filter based on the URL of the search hit.  I don't see a clear way to implement a multi site

@Sanjay, indexing the field makes sense but the instance type is ISearchContent.  Is that the preferred approach?

This is the exact initializer code I was able to use:

SearchClient.Instance.Conventions.ForInstancesOf<ISearchContent>().IncludeField(x => x.SearchHitUrl);

I reran the find index job and results are still site specific when using the domain filter.  I suspect the search hit URL is still not being indexed because it seems like it is more of an output than an input to filter on.

The way I am trying to filter the results by domain:

var domainFilter = searchClient.BuildFilter<ISearchContent>();
domainFilter = domainFilter.Or(x => x.SearchHitUrl.Prefix(SiteDefinition.Current.SiteUrl.AbsoluteUri)); // Include current site results

if (currentSite == SiteName.A)
{
  domainFilter = domainFilter.Or(x => x.SearchHitUrl.Contains("siteBdomain.com").Match(true)); // Include other site results
}

var query = querySearch
...
  .Filter(domainFilter)
...

#249570
Mar 04, 2021 19:40
Vote:
 

If I understand correctly you want to create a filter to get search results of the current site in a multi-site setup. You have one search index and multiple websites

The following code snippet might able to help you

I'm filtering results based on the Current start page Ancestors

private IMultiSearch<IContentData> BuildMultiSearch(string searchTerm, int maxResults, int currentPage, int cacheResultsForMinutes,
            bool ignoreDefaultOperator = false, BestBetsResults bestBets = null)
        {
            var search = _findClient.Search<IContentData>();
            var useAndAsDefaultOperator = false;
            var startPageContentLink = SiteDefinition.Current.StartPage;
            if (startPageContentLink != null && startPageContentLink.ID > 0)
            {
                var startPage = _contentLoader.Get<StartPage>(startPageContentLink);
                useAndAsDefaultOperator = startPage.UseAndAsDefaultOperator;
            }

            if (startPageContentLink != null)
            {
                search = search.Filter(x =>
                    ((IContent)x).Ancestors().Match(startPageContentLink
                        .ToReferenceWithoutVersion().ToString()));
            }

           .......
#249627
Mar 05, 2021 22:17
QRiley327 - Mar 08, 2021 3:41
Is there any way to modify this to show results from 2 websites? I have 3 sites and want to share results between 2, is this possible with your implementation?
Vote:
 

Antoher option is to use .FilterOnCurrentSite() to get domain specific results

#249628
Mar 05, 2021 22:40
Vote:
 

So we actually have 3 sites. We want to share the results from 2 sites between each other and have site 3s results be entirely its own pages. So site A and B both get A and B pages, and site C gets C pages only. I was sucessfully using a filter builder with an or filter(x => x.prefix("https://MySite1URL") or filter(x => x.prefix("https://MySite2URL") but we need a solution that will work across environments and multiple domains. We need it to filter on the urls automatically, if that makes sense? 

#249692
Mar 08, 2021 4:09
Vote:
 

You can create a Filter method that applies a Filter based on parameter. 

Have you tried using Grouping filters?

Something like 

.Filter(x => (((PageData)x).ExternalURL.Match("site1url")) | ((PageData)x).ExternalURL.Match("site1url"))
//or
.Filter(x => (((PageData)x).ExternalURL.Prefix("site1url")) | ((PageData)x).ExternalURL.Prefix("site1url"))

Have not tried in solution but logically it should work. 

#249787
Mar 09, 2021 21:36
Vote:
 

You can also do a match on the SiteId. Take a look at

https://world.episerver.com/documentation/developer-guides/search-navigation/Integration/cms-integration/Querying-examples/

The last example is the following:

var content = SearchClient.Instance.Search<IContent>()
  .Filter(x => x.SiteId().Match("MySiteId"))
  .GetContentResult();

I haven't used this, but it might should accomplsh what you are after.

#250150
Mar 15, 2021 14:54
* 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.