4

I am trying to fetch List of articles using Solr Search context. This is the code block trying to fetch the results.

var searchIndex = ContentSearchManager.GetIndex(indexName); // Get the search index
var searchPredicate = GetSearchPredicate(false, speciality, category, 
filterModel.Specific_Speciality, true); // Build the search predicate
using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the 
search index
{
    var searchResults = (IQueryable<SearchModel>)null;
{

    searchResults = searchContext.GetQueryable<SearchModel>().Where(searchPredicate).OrderByDescending(i => i.CreatedOn);//.Where(searchPredicate)
    searchResults = searchResults.Where(i => i.Category.Equals(category) && i.Category != "Cardio news").Take(count);
}
var fullResults = searchResults.GetResults();

fullResults contains zero results after execution.

I checked the Search.Config to get the Solr query.

?q=((category_t:("News") AND _templatename:("News Article")) AND _latestversion:("True")) AND _val_:__boost&start=4&rows=4&fl=*,score&fq=_indexname:(sitecore_master_index)&wt=xml&sort=createddate_tdt desc

I executed this query in Solr and it returned zero results. When I searched for category_t in the Solr index, I noticed that the category field is getting indexed as category_s not category_t. Hence returning zero results.

Update - #1

I checked the search predicate and the search model as well.

[IndexField("category_t")] public virtual string Category { get; set; }

Search model is referencing category_t. I made the changes in search model making category_s instead of category_t.

Also, my predicate code is referring to category_s field. enter image description here

After updating the model, I did restarted the IIS and did the index rebuilding.

I am still seeing same query in the Search.Config file.

3 Answers 3

1

Sitecore interally uses the below Solr Query to determine the field type before making search to Solr.

/select?q=_name:(category)&fq=_templatename:("Template+field")

This will return all the field types combination with the category field in your Sitecore archirecture. You can request the above url to fetch all combinations of category field created on the CMS.

In order to address this issue, you can add below configuration to make sure category field is resolved using text type when stored in Solr.

<configuration>
    <sitecore>
         <contentSearch>
           <indexConfigurations>
                <defaultSolrIndexConfiguration>
                     <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
                        <fieldNames hint="raw:AddFieldByFieldName">
                         <field fieldName="category" returnType="text" />
                        </fieldNames>
                     </fieldMap>
                </defaultSolrIndexConfiguration>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>

Once these changes are added, you can re-index the items and they will be stored with name category_t in the index.

Note: Its not necessary to add suffix _t on the model. It will be resolved automatically by Content Search API.

Hope this helps!!! Let me know in case you have any queries.

2
  • 1
    Adding this patch helped me. category is getting indexed as category_t. Rest the code is working fine. Commented Oct 28 at 4:25
  • Glad to hear that @MohitPatil!!!
    – ckhanna
    Commented Oct 28 at 5:59
2

The search isn't working because the category field is set up as a string instead of text. That's why you're not getting any results.

To fix this, modify your query to use the right field name.

1. Update the Solr Query: Change your search predicate to use category_s instead of category_t.

2. Adjust the Code: Modify your code block where you build the search predicate to ensure it references category_s. For example, if you have something like this:

searchPredicate = searchPredicate.AndAlso(i => i.Category.Equals(category));

Ensure that Category is referencing the correct field type (string).

3. Check Schema Configuration: If you have control over the Solr schema, you might want to verify whether category_t should be indexed and potentially reindex your data. But if category_s is the intended field for search operations, just stick with it.

4.Reindexing: If changes are made to the schema, ensure that you reindex your data so that the latest configuration is reflected.

After you make these changes, try searching again. If the files are there, you should now be able to find them.

Hope this help!!

1
  • Tried this but no luck Commented Oct 28 at 4:26
1

Based on the information you've provided, the primary reason for zero results is the discrepancy between the expected field type category_t and the actual indexed field type category_s in Solr. This mismatch prevents the search query from correctly matching the desired documents.

Ensure that the category field is defined with the correct type category_t in your Solr schema. This will allow for proper text-based search and filtering on the category field.

Double-check your Solr configuration to ensure that the category field is indexed correctly. If necessary, re-index your data to reflect the updated field type.

Once updated try with the simple search query that only targets the category field like this.

searchResults = searchContext.GetQueryable<SearchModel>().Where(i => i.Category == category).Take(count);

If this query returns results, it confirms that the issue lies in the additional filtering or sorting conditions.

Hope this helps.

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.