Wednesday, February 8, 2017

Solr Configuration for providing search suggestions

Hi All,

Today we discuss how we configure solr for provide search suggestion.
For that I follow 2 blogs which are listed below the blog.

https://blog.horizontalintegration.com/2015/07/20/configuring-solr-to-provide-search-suggestions/

http://www.nttdatasitecore.com/Blog/2017/January/Faceted-Autocomplete-for-a-Sitecore-Multilingual-site-using-SOLR-Suggester-API

By using help of these blog I can configure solr with auto suggest. I use 1st blog for setup solr and using 2nd blog Building jQuery Autocomplete jquery : jquery function for autocomplete suggestion.

Note:
1.Keep in mind while follow first blog for updating solrconfig.xml field str contain the name of the field for example:
<searchComponent name="suggest" class="solr.SuggestComponent">
    <lst name="suggester">
        <str name="name">fuzzySuggester</str>
       ...
        <str name="field">page_title_t</str>
    ...
    </lst>
    <lst name="suggester">
        <str name="name">infixSuggester</str>
      ...
        <str name="field">page_title_t</str>
       ...
    </lst>
</searchComponent>

2. While using 2nd blog jquery function its give cross platform error , solution also provide in that blog but i am not able to resolve that so i use my solution that are below:
<script type="text/javascript">
        $('#search-field').autocomplete({
            source: function (request, response) {
                var currentLanguage = 'en';
                var url = "http://localhost:8983/solr/custom_index_autocomplete/suggest?suggest.q=" + request.term + "&suggest.cfq=" + currentLanguage + "&wt=json&json.wrf=?";
                $.getJSON(url, function (data) {
                    //response($.map(data.suggest.suggester[request.term].suggestions, function (value) { return value.term }));
                    response($.map(data.suggest.fuzzySuggester[request.term].suggestions, function (value) { return value.term }));
                });
            }
        });
</script>

3. For combined multiple indexes for autosuggestion please follow as below:

   <script type="text/javascript">
        $('#search-field').autocomplete({
            source: function (request, response) {
                var currentLanguage = 'en';
                var url = "http://localhost:8983/solr/custom_index_autocomplete/suggest?suggest.q=" + request.term + "&suggest.cfq=" + currentLanguage + "&wt=json&json.wrf=?";
                var url1 = "http://localhost:8038/solr/content/suggest?suggest.q=" + request.term + "&suggest.cfq=" + currentLanguage + "&wt=json&json.wrf=?";

                var res;
                var res1;
                $.getJSON(url, function (data) {
                    res = data.suggest.fuzzySuggester[request.term].suggestions;
                    $.getJSON(url1, function (data) {
                        res1 = res.concat(data.suggest.fuzzySuggester[request.term].suggestions);
                        response($.map(res1, function (value)
                        { return value.term }
                           ));
                    });
                });
            }
        });
    </script>

No comments:

Post a Comment