1

We have a number of searches on our site that should be possible through a pure filtered search - we're searching across a few fields, some of which are mutli-selects, others are booleans and one's a string. We're able to force the use of the filter term for most of these fields, but I'm struggling to get one particular field to behave.

The reason I'm bothering with this is because this particular search doesn't really need scoring or free-text searching, we're just trying to find items tagged with values - and with scoring applied I'm getting duplicated and missing items across the pages of results as the scores are all very close.

Unlike How to force Sitecore to use &$filter we're on Sitecore 9.1.1, so should already have the fix applied.

The fields are all marked as "Filterable" in both the Index Configuration and the portal.

Example of methods that add a filter property correctly:

private IQueryable<SearchResultItem> FilterOnTaxonomies(IQueryable<SearchResultItem> queryable,
                                                        IEnumerable<Guid> taxonomies)
{
  if (taxonomies != null && taxonomies.Any())
  {
    var expression = PredicateBuilder.False<IndexedItem>();
    foreach (var taxonomy in taxonomies)
    {
      expression = expression.Or(i => i.TaxonomyTerms.Contains(taxonomy));
    }

    return queryable.Cast<IndexedItem>().Where(expression);
  }

  return queryable;
}

private IQueryable<SearchResultItem> FilterOnVersion(IQueryable<SearchResultItem> queryable)
{
  queryable = queryable.Cast<IndexedItem>().Filter(item => item.IsLatestVersion);
  return queryable;
}

However using the same form for the Language field causes the search method to be used:

private IQueryable<SearchResultItem> FilterOnLanguageSettings(IQueryable<SearchResultItem> queryable, Language language)
{
  queryable = queryable.Cast<IndexedItem>().Filter(item => item.Language == language.Name);
  return queryable;
}

Adds the language field as a search term:

{
  "search": "language_1:(en)",
  "searchMode": "any",
  "queryType": "full",
  "filter": "((taxonomyterms/any(t:t eq 'bec57e0dc45a415fa7428d3866142493')) and ((taxonomyterms/any(t:t eq '03ff97548a4d4be8b14ff989897b2061')) or (taxonomyterms/any(t:t eq 'a51c78bf63c743c2b50481ab0c78b367'))) and (taxonomyterms/any(t:t eq 'b56cfc3389a54b8a9b12725ba015bee4')) and latestversion_1 and (path_1/any(t:t eq 'e99621f99cd243619f53f78afbbd390a')))",
  "facets": [
    "templatename_1",
    "smallcreateddate_2"
  ],
  "top": 14,
  "scoringProfile": "itemboost",
  "count": true
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.