4

Need your help in filtering.

We have documents tagged to categories as below:

d1 tagged to 1 ,3 & 4 and d2 = 2 ,6 & 7

I need to output as below:

  • User selects category 1 and 2 and then click search.Result will be d1 and d2

  • 3 => d1

  • 7 => d2

  • 3,6 =>d1 ,d2

  • 5 => nothing

With respect to implementation, I have a parameter named filter whose value will be comma seperated category (i.e the one selected by user in the ui Eg: 1,2) and I have a field exposed for each doecument as 1,2 (i.e comma seperated categories to which they are tagged)

So now how can I get all documents corresponsing to the filter selected (passed as a parameter) by the end user?

1
  • can you add more details to your question: 1) how your fields are defined; 2) are you talking about LINQ kind of search OR you want to filter in the content editor search window?; 3) what have you to achieve your goal? maybe some code snippets, etc Jan 16, 2017 at 8:42

1 Answer 1

2

I assume you have created multilist field on documents and data-source of the multilist field is Category items.

Now if you select 1,3 and 4 categories into mulitlist of document 1 and 2 ,6 & 7 categories for document d2 multilist field.

On the front end you have checkboxes or any other thing for select categories now if you are selecting 1 and 2 then both document will show right?

If this is the requirement then you can handle this by creating predicate builder and you can pass pipe separated categories into predicate builder as below code -

var builder = PredicateBuilder.True();
//Filter with TemplateId
builder = builder.And(i => i.TemplateId == [TemplateId]);

//Created new builder for Category items and added again into main builder
if (!string.IsNullOrEmpty(Categorys))
{   var Categorybuilder = PredicateBuilder.False();
    var CategoryItems = Categorys.Split('|');
    foreach (var Category in CategoryItems)
    {
        var ct = Sitecore.ContentSearch.Utilities.IdHelper.NormalizeGuid(Categorys, true);
        Categorybuilder = Categorybuilder.Or(i => i.Category.Contains(ct));
    }
    builder = builder.And(Categorybuilder);
}

Now pass this builder to search results query as below -

var results=context.GetQueryable().Where(builder).Select(i => (Item)i.GetItem()).ToList()

That way you can get the document values with OR.

For more details you can also have a look my post -

http://www.nttdatasitecore.com/Blog/2016/September/Sitecore-Lucene-Search-filtering-with-Predicate-Builder

In this post I have written same scenario with multiple filters.

Please let me know if something is not clear to you.

2
  • That helped thanks.Can you answer me on similar lines..What is the purpose of using Filter Method then?
    – Suhas
    Jan 23, 2017 at 5:30
  • Sorry @Suhas for filtering I always use predicate builder I never use Filter method Jan 24, 2017 at 9:47

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.