0

I am using Solr facets and I want to filter tagging and exclude it.

My query is :

https://localhost:8989/solr/rya_custom_web_index/select?q=*%3A*&fq={!tag=courseactivities_s:Dinghy}courseactivities_s:Dinghy&facet=true&facet.field={!ex=courseactivities_s:Dinghy}courseactivities_s&facet.field=courselevels_sm

It is working fine in Solr, now I want to use it in Sitecore using search API. Could you please guide me on how I can use it?

1

You can do it with SolrNet by passing QueryOptions as a parameter:

using (var context = index.CreateSearchContext())
{
    var field = "courseactivities_s";
    var value = "Dinghy";
    
    var options = new QueryOptions
    {
         FilterQueries = new List<ISolrQuery>
         {
             new LocalParams {{"tag", field }} + Query.Field(field).Is(value), // yor can multiply Query.Field by || and &&
             ...
         },
         Facet = new FacetParameters
         {
             Queries = new List<ISolrFacetQuery>
             {
                 new SolrFacetQuery(new LocalParams {{"ex", field }} + Query.Field(field).Is(value)),  // yor can multiply Query.Field by || and &&
                 ...
             },
         },
         Rows = 10,
         ...
    };
    
    var results = context.Query<SearchResultItem>(query, options);    
}

If you do not have main query representation in ISolrQuery format, you can easy convert it from usual IQuaryable by:

var solrQuery = new SolrQuery(((IHasNativeQuery)query).Query.ToString());
0

Take a look at the Doco here: https://doc.sitecore.com/developers/93/sitecore-experience-manager/en/linq-to-sitecore.html

Sounds as though you want to specifically look at the Filter() and FacetOn() calls.

You can confirm what the translated Solr Query will look like if you're having trouble by hopping into your Logs and reviewing the contents of the Search Logs which in a Standalone install will be in your Logs directory and be within a seperate file named some like 'Search.log.20200907.104037.txt'. This log contains all Sitecore to Solr queries so you will just need to find your specific log entry.

This is untested but I would think it would be something like..

results = queryable.Filter(x => x.CourseActivites != "Dinghy"); 

You might need to add some extra clauses to the queryable before the .Filter() to make both work but just have a play around and check the logs like I mentioned and you'll work it out :)

4
  • Filter and FacetOn I am already doing just wanted to ask how I can use this {!tag=courseactivities_s:Dinghy} and this {!ex=courseactivities_s:Dinghy} Sep 7 '20 at 2:32
  • This is untested but I would think it would be something like.. results = queryable.Filter(x => x.CourseActivites != "Dinghy"); You might need to add some extra clauses tot he queryable before the .Filter to make both work but just have a play around and check the logs like I mentioned and you'll work it out :) Sep 8 '20 at 13:56
  • @DaveGoosem please update your answer vs adding comments if you have more detail
    – Richard Seal
    Sep 8 '20 at 14:24
  • @RichardSeal no worries mate, I've updated the answer Sep 8 '20 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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