3

In a project, I have implemented SOLR search. I am able to get Items from search, based on template name.

In a scenario,I have to get items using Search from a MultiList field which are selected.

Please provide suggestions how this can be achieved?

2 Answers 2

4

I recommend to use ComputedIndexField for your case.

Computed fields are a great way of storing calculated information while Sitecore indexes your items as opposed to using more resource to calculate it on the fly for use in your sublayouts/layouts.

public class ColorField: AbstractComputedIndexField

{  

public string FieldName { get; set; }
public string ReturnType { get; set; }

public override object ComputeFieldValue(IIndexable indexable)
{
  var item = indexable as SitecoreIndexableItem;

  MultilistField f = item.Fields["Colors"];

  if (f != null)
  {
     var multilist = f.GetItems();
     if (multilist == null || multilist.Length == 0)
     return null;

     return string.Join(" ", multilist.Select(t => t["Name"]));
  }

   return null;
  }

}

You need to register your custom computed field :

<fields hint="raw:AddComputedIndexField">
          <field fieldName="colorcustomfield" returnType="string">YourNamespace.ColorField,YourAssembly</field>
</fields>

You need to extend SearchResult item class :

public class  CustomSearchResultItem
{
[IndexField("colorcustomfield")]
public virtual string ColorField { get; set; }
}

You can use my code if you want to search for Color in a multilist field

 var index = ContentSearchManager.GetIndex(indexName);
   using (var context = index.CreateSearchContext())
   {
     var query = context.GetQueryable<CustomSearchResultItem>()
                .Where(item => item.ColorField.Contains("Red"))
                .ToList();
   }
4

Value from your multilist field in solr will look like this:

"multilistfield_sm":["c42546c15d30434b98bfa9f421165ea5",
  "3cf0df1b900147e88da7ab7c6fb5e236",
  "7185ea8a03474d95aacaf991917b7ed1",
  "9b5672f57f6b4e01a077e8f6deb0c3b1",
  "be945209f663495a91e75df145daff1b",
  "e815fa1cc2a34525be4208d88777c6b0",
  "28f13cabd6c041fea529ca74081c0d69"],

You can define your custom search result item and add your field to it, like this:

public class CustomSearchResultItem
{
    [IndexField("multilistfield_name")]
    [DataMember]
    [TypeConverter(typeof(IndexFieldEnumerableConverter))]
    public virtual IEnumerable<ID> MultilistField { get; set; }
}

Then you can easly query by this field or get value of this field:

var index = ContentSearchManager.GetIndex(indexName);
using (var context = index.CreateSearchContext())
{
    var query = context.GetQueryable<CustomSearchResultItem>()
                .Where(item => item.MultilistField.Contains(someId))
                .ToList();
}

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.