0

Locally I successfully enabled full text search on a field in the sitecore_web_index by implementing the following logic in my Sitecore solution:

1) Added this xml to a patch configuration file:

  <indexConfigurations>
    <defaultCloudIndexConfiguration type="Sitecore.ContentSearch.Azure.CloudIndexConfiguration, Sitecore.ContentSearch.Azure">
      <fieldMap type="Sitecore.ContentSearch.Azure.FieldMaps.CloudFieldMap, Sitecore.ContentSearch.Azure">
        <fieldNames hint="raw:AddFieldByFieldName">
          <field fieldName="subtitle" cloudFieldName="subtitle_2" searchable="YES" retrievable="YES" facetable="YES" filterable="YES" sortable="YES" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.Azure.CloudSearchFieldConfiguration, Sitecore.ContentSearch.Azure" />
        </fieldNames>
      </fieldMap>
    </defaultCloudIndexConfiguration>
  </indexConfigurations>

2) Added the index field name in the custom SearchResultItem model:

public class CustomSearchResultItem : SearchResultItem
{
    [IndexField("subtitle_2")]
    public virtual string Subtitle { get; set; }
}

3) I ensured this field was used in the search predicate:

searchQuery = searchQuery.Or(q => q.Subtitle.Contains(searchTerm));

However the above logic has just failed in the UAT environment where Azure Search is the provider.

I'm about to look through the logs for clues but is there a better/easier way of enabling full text search for a single field? Or does anyone know why the above may have failed?

  • Don't undo moderator edits, please. We're here to assist. sitecore.meta.stackexchange.com/questions/172/… – Mark Cassidy Nov 19 at 13:30
  • what version of Sitecore? – Chris Auer Nov 19 at 13:32
  • @MarkCassidy - the answer below which is really useful is an Azure Search vs SOLR answer - if the title doesn't include Azure Search it just comes across as a general Content Search question – R. Allinson Nov 19 at 13:41
  • @ChrisAuer It's for Sitecore version 8.2 – R. Allinson Nov 19 at 13:42
1

I've had similar issues to this in the past. Contains() seems to Work in a strange way in Azure Search vs Solr. If I recall it does a regex match on each word. The documentation here is a little confusing: https://doc.sitecore.com/developers/90/platform-administration-and-architecture/en/support-reference-for-azure-search.html

What I've seen work is a like fairly well is creating a 'phrased search query' so that the query is treated as a single string and then using the Like() operator instead like so:

var phrasedSearchTerm = $@"""{searchTerm.Trim()}""";
searchQuery = searchQuery.Or(q => q.Subtitle.Like(phrasedSearchTerm));

An alternative you can also try is the MatchWildcard() operator like so:

searchQuery = searchQuery.Or(q => q.Subtitle.MatchWildcard($"*{searchTerm.Trim()}*"));

One other thing I would consider doing (depending on what validation you have in place in your search) is creating an extension method to strip out certain special characters as these can confuse your search. e.g something like:

public static string EscapeCharacters(this string input, string charactersToEscape = @" ,(,),&,-,+,*")
{
    foreach (var character in charactersToEscape.Split(',').ToList())
    {
        input = input.Replace(character, $@"\{character}");
    }
    return input;
}

Lastly when testing this look in the latest 'search.log.[data].txt' log files to see what Query is actually being generated by Sitecore and sent off to Azure Search, the logs will look something like:

ManagedPoolThread #9 10:47:10 INFO AzureSearch Query [query here]

You can test the search yourself in the Azure Portal too and see what you get back.

There is more info on issues with Azure search queries here: Azure search matching wildcard instead of equals

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.