2

Using Sitecore 8.2 update 3.
I'm new to search & Azure Search and followed this approach to implement the site search functionality on our multi-lingual site.

https://kamsar.net/index.php/2014/05/indexing-subcontent/

There are two issues now:
1. The search works for the English version, but only partially. When I search for a particular keyword "stories", it doesn't return any results. But this word is added to the content as I check in debug. (The thai text is also added to the field.)

  1. No results are returned when search in the Thai version.

I have not modified any default Sitecore configs.

Is the patch config correct. Is there a different way to retrieve results for Azure Search. Because the same code GetSearchResults works fine when checked with Lucene.

MyAzureSearch.config:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>      
      <setting name="ContentSearch.DefaultIndexType">
        <patch:attribute name="value">Sitecore.ContentSearch.Azure.CloudSearchProviderIndex, Sitecore.ContentSearch.Azure</patch:attribute>
      </setting>
      <setting name="ContentSearch.DefaultIndexConfigurationPath">
        <patch:attribute name="value">contentSearch/indexConfigurations/defaultCloudIndexConfiguration</patch:attribute>
      </setting>
    </settings>
    <contentSearch>
      <indexConfigurations>
        <defaultCloudIndexConfiguration>
          <fields hint="raw:AddComputedIndexField">
            <!-- indexes subcontent contents into parent's _content field in the index (for better site search) -->
            <field fieldName="_content" cloudFieldName="content__" type="MySite.Feature.Search.Models.SubcontentField, MySite.Feature.Search" boost="1f" settingType="Sitecore.ContentSearch.Azure.CloudSearchFieldConfiguration, Sitecore.ContentSearch.Azure"/>
          </fields>
        </defaultCloudIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

Adding content to the computed field:

public class SubcontentField : IComputedIndexField
{
    /// This function is triggered when rebuilding indexes is performed in CMS
    public object ComputeFieldValue(IIndexable indexable)
    {
        var sitecoreIndexable = indexable as SitecoreIndexableItem;

        if (sitecoreIndexable == null)
        {
            return null;
        }

        Item homeItem = GetHomeItem(SiteName, MasterDatabaseName);
        List<string> contentToAdd = new List<string>();

        //add values (of only the page items that are under home) to the computed field.
        if (homeItem != null && (sitecoreIndexable.Item.ID == homeItem.ID || sitecoreIndexable.Item.Paths.IsDescendantOf(homeItem)))
        {
            // find renderings with datasources set
            IEnumerable<Item> customDataSources = ExtractRenderingDataSourceItems(sitecoreIndexable.Item);
            IEnumerable<Item> relatedItems = customDataSources.SelectMany(GetRelatedItems);

            // extract text from data sources
            contentToAdd = customDataSources.SelectMany(GetItemContent).ToList();

            contentToAdd.AddRange(relatedItems.SelectMany(GetItemContent));

            if (contentToAdd.Count == 0) return null;
        }

        return string.Join("|", contentToAdd);
    }
}

Get search results:

private void GetSearchResults(string keywords)
{
    if (!string.IsNullOrEmpty(keywords))
    {
        string index = string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name.ToLower());

        using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
        {
            //baseQuery, set to True, so if we have no searchTerm, all items are returned.
            var baseQuery = PredicateBuilder.True<SearchResultItem>();
            var containsQuery = PredicateBuilder.True<SearchResultItem>();

            //here we are breaking up all the terms in the searchTerm                    
            var tokens = keywords.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var token in tokens)
            {
                var currentToken = token;
                containsQuery = containsQuery.Or(item => item.Name.Contains(currentToken));
                containsQuery = containsQuery.Or(item => item.Content.Contains(currentToken));
                containsQuery = containsQuery.Or(item => item.Content.Contains(HttpUtility.UrlEncode(currentToken)));
            }

            //join the baseQuery and the containsQuery
            baseQuery = baseQuery.And(containsQuery);
            IQueryable<SearchResultItem> query = context.GetQueryable<SearchResultItem>()
                .Where(baseQuery)
                .Where(x => x.Language == Sitecore.Context.Language.Name);                    

            if (query != null && query.Count() > 0)
            {

            }
        }
    }
}

3 Answers 3

1

In your case, I would suggest keeping the default field _content as is and try

enter image description here

I haven't tried the multi-lingual search, make sure these configurations are added in your Sitecore instance.

Let me know how it goes.

0

There is an overload for the GetQueryable() you can try:

var culture = Sitecore.Context.Language.CultureInfo;
var queryable = context.GetQueryable<TYPE>(new CultureExecutionContext(culture));

Use this along with the predicate where you specify the language and see if that helps.

0

Multilingual search is supported starting from 8.2 Update-7, see Release Notes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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