1

I have read this article about how to create custom field reader and index

I would like to ask that at the very last step, in MySearchResultItem class, is it mandatory to match the index field name "My Ideas" with class property "MyIdeas"

[IndexField("My Ideas")]
 public List<string> MyIdeas { get; set; }

I have tried with [IndexField("_myideas")] but query is not able to find an result. I also looked in search.log and the search query is:

?q=myideas_t:(*content*))&rows=1&fl=*,score&fq=_indexname:(my_custom_index)
3
  • It looks like you missed field mapping <fieldType fieldName="my_ideas" returnType="stringCollection" />
    – Marek Musielak
    Jun 12, 2018 at 11:08
  • Sometimes, in order to correctly convert the fieldname to the name that is used in SOLR (e.g. "content_categories" to "content_categories_sm" you need to add special attribute: [IndexField("content_categories")] [TypeConverter(typeof(Sitecore.ContentSearch.Converters.IndexFieldEnumerableConverter))] public virtual IEnumerable<ID> ContentCategories { get; set; } Jun 12, 2018 at 11:26
  • @MarekMusielak, The configuration for field mapping is there but still having the same problem.
    – Kamran
    Jun 12, 2018 at 12:50

4 Answers 4

1

Ideally, something like this should work:

[IndexField("myideas")]
public List<string> MyIdeas { get; set; }

But I have seen others (depending on what version of Sitecore) have issues with very similar fields that requires them to more closely mirror the Solr field naming:

[IndexField("myideas_sm")]
public List<string> MyIdeas { get; set; }

Suffixing the _sm more or less tells Solr to interpret the field as a stringCollection which really should be inferred and not required, but as you can see others having similar issues.

Similar to the other linked post, you may have some luck with defining a TypeConverter:

[IndexField("myideas_sm")]
[TypeConverter(typeof(Sitecore.ContentSearch.Converters.IndexFieldEnumerableConverter))]
public List<string> MyIdeas { get; set; }

Hopefully this can help you get over the issue and continuing on.

1

StringCollection field types will be indexed using field name format fieldNameFormat="{0}_sm"

If field name is My Ideas use the following code in your MySearchResultItem class.considering MySearchResultItem implements SearchResultItem class.I use Ilist instead of list to query the string collection.

 [IndexField("my_ideas_sm")]
 public IList<string> locationFacetCategory { get; set; }

If it is MyIdeas, use following code

  [IndexField("myideas_sm")]
   public IList<string> locationFacetCategory { get; set; }

You can further troubleshoot this with following steps.

  1. Make sure you are querying against correct index.
  2. make sure http://localhost:8983/solr runs.
  3. Navigate to the Sitecore Control panel to rebuild the indexes using Indexing manager.
  4. Field has been correctly indexed and gets the value from Solr dashboard.for doing this navigate to http://localhost:8983/solr/#/custom_index/query and query the item in the dashboard to find the fields being indexed there.
1

Another way of doing this would be to create a computed field.

public class MyIdeasMultiValued: AbstractComputedIndexField
{
 public override object ComputeFieldValue(IIndexable indexable)
    {
        Item currentItem = indexable as SitecoreIndexableItem;
        List<string> result = new List<string>();

        if (currentItem.Fields["My Ideas"].HasValue)
        {

            var myIdeas = currentItem.Fields["My Ideas"].Value;
            var listOfVal = myIdeas.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            return listOfVal;                
        }
    }
}

Now you will have to tell sitecore to take your new computed field when building the index. You will need to patch the Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
  <contentSearch>
    <!-- Configuration sections for indexes -->
    <indexConfigurations>
     <sandboxIndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
      <indexAllFields>false</indexAllFields>
      <initializeOnAdd>false</initializeOnAdd>
      <documentOptions
        type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">            
        <fields hint="raw:AddComputedIndexField">
          <field fieldName="MyIdeasMultiValued" >Test.ComputedFields.MyIdeasMultiValued,Test</field>              
        </fields>
      </documentOptions>
      <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
        <typeMatches hint="raw:AddTypeMatch">
        </typeMatches>
        <fieldNames hint="raw:AddFieldByFieldName">         
            <field fieldName="MyIdeasMultiValued" returnType="stringCollection"></field>
        </fieldNames>
      </fieldMap>           
    </sandboxIndexConfiguration>
  </indexConfigurations>
</contentSearch>

In the MySearchResultItem you will add

[IndexField("MyIdeasMultiValued")]
 public List<string> MyIdeas { get; set; }

After you rebuild your index you can use the below code to perform the query to check the values.

using (var context = this.GetSearchIndex((Context.ContentDatabase ?? Context.Database).Name).CreateSearchContext())
{
  var listOfVal = context.GetQueryable<MySearchResultItem> ().Select(i=>i.MyIdeas);

}
0

Sitecore provides the Content Search API specifically to avoid customizing a solution for a particular provider: Solr, Azure, Lucene (don't use Lucene)

To make your code provider-agnostic, the proper solution is below.

CustomSearchResultItem object

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Data;

namespace MyNamespace
{
    public class CustomSearchResultItem : SearchResultItem
    {
        [IndexField("my_ideas")]
        [TypeConverter(typeof(IndexFieldEnumerableConverter))]
        public IEnumerable<ID> MyIdeas{ get; set; }
    }
}

Config Updates Specifically for Solr

<index id="your_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
    <configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
        <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <fields hint="raw:AddComputedIndexField">
                <!-- yes, the included AllTemplates class works great for this -->
                <field fieldName="my_ideas" storageType="yes" indexType="untokenized">Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch</field>
            </fields>
        </documentOptions>
        <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
            <fieldNames hint="raw:AddFieldByFieldName">
                <field fieldName="my_ideas" returnType="stringCollection"></field>
            </fieldNames>
        </fieldMap>
    </configuration>
</index>

Your Answer

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

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