1

I recently tried solr autosuggest search in my local,

Steps I followed :

  1. Added configuration in master in sensoft_master_index\conf\solrconfig.xml enter image description here

  2. Restarted solr8.1 service in services

  3. in C# code,

public IEnumerable GetSuggestion(string term) {

        try
        {
            using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                SolrSuggestQuery query = term;
                var options = new SuggestHandlerQueryOptions
                {
                    Parameters = new SuggestParameters
                    {
                        Count = 3
                    }
                };

                var result = context.Suggest(query, options);

                return result.Suggestions["default"].Suggestions.Select(a => a.Term);
            }
        }
        catch (Exception e)
        {
            System.IO.File.WriteAllText(@"D:\test1.txt", e.ToString());
        }
        return rr;
    }

I'm getting the results But now I want to filter the suggestion only from a particular path: for ex: I need the suggestions that have the path: /sitecore/content/home

what configuration changes I need to make? Any help

3 Answers 3

1

To filter suggestion result only particular path, there is three possible solutions:

Option 1: Set contextField as _fullpath and pass path in ContextFilterQuery

 <str name="contextField">_fullpath</str>
using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                SolrSuggestQuery query = term;
                var options = new SuggestHandlerQueryOptions
                {
                    Parameters = new SuggestParameters
                    {
                        Count = 3,
                        Build = true,
                        ContextFilterQuery="SomePath"
                    }
                };
                var result = context.Suggest(query, options);
                return result.Suggestions["default"].Suggestions.Select(a => a.Term);
            }

Option 2: Filter results on basis of template

<str name="contextField">_template</str>
using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                SolrSuggestQuery query = term;
                var options = new SuggestHandlerQueryOptions
                {
                    Parameters = new SuggestParameters
                    {
                        Count = 3,
                        Build = true,
                        ContextFilterQuery="sometemplateId"
                    }
                };
                var result = context.Suggest(query, options);
                return result.Suggestions["default"].Suggestions.Select(a => a.Term);
            }

See the blog post for more reference: Solr Auto Suggester With Sitecore

Option 3 Create a computed field where you can pass true and false based on the path and set the computed field name in contextField

<str name="contextField">computedfieldname</str>
using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                SolrSuggestQuery query = term;
                var options = new SuggestHandlerQueryOptions
                {
                    Parameters = new SuggestParameters
                    {
                        Count = 3,
                        Build = true,
                        ContextFilterQuery="true"
                    }
                };
                var result = context.Suggest(query, options);
                return result.Suggestions["default"].Suggestions.Select(a => a.Term);
            }
0

You need to add to the

<str name="contextField">_template</str> 

This will allow you filter our search results based on path , more filtering can be applied by using a custom Index which is mapped to a particular location

My blog on SOLR Suggester and filtering - https://sitecorejumpstart.wordpress.com/2019/10/06/sitecore-solr-autosuggest-with-template-and-language-filters/

2
  • I could not find any filtering (sample code in that url that you have shared). what is the purpose of that providing template in that field? which means template name also suggested ? .pls clarify
    – Rajasekar
    Commented Apr 12, 2020 at 10:02
  • yes contextField can take a template name as a name , I suggest you read this for more clarity - lucene.apache.org/solr/guide/6_6/…
    – Abhay Dhar
    Commented Apr 13, 2020 at 12:03
0

I don't see any direct functionality for the same as far as I know. But I can suggestion one work around.

Create a custom index field. Fill it's value only for the desired teamplate and path. Use the same custom index field for below configuration:

<str name="field">PUT_YOUR_CUSTOM_FIELD_NAME_VISIBLE_IN_SOLR</str>

This would definitely solve your problem.

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.