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.)
- 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)
{
}
}
}
}