cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Customizing Autosuggestion

Former Member
0 Likes
2,376

Hi Experts,

I need to customize the autosuggestion as follows:-

The search term in search box component should be matched in product name/description and all relevant categories of matching products should be shown as autosuggestion.

The out of the box Hybris matches the search term with category name.

Thanks, SPS.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes
 @ResponseBody
     @RequestMapping(value = "/autocomplete", method = RequestMethod.GET)
     public AutocompleteResultData getAutocompleteSuggestions(@RequestParam("term") final String term)
     {
         final List<String> terms = new ArrayList<String>();
         boolean addFacet = true;
         final AutocompleteResultData resultData = new AutocompleteResultData();
         final List<AutocompleteSuggestionData> autocompleteSuggestionData = productSearchFacade.getAutocompleteSuggestions(term);
         for (final AutocompleteSuggestionData termData : autocompleteSuggestionData)
         {
             terms.add(termData.getTerm());
             if (addFacet)
             {
                 addFacet = false;
                 final FacetSearchPageData<SolrSearchQueryData, SearchResultValueData> searchPageData = commerceProductSearchService
                         .textSearch(term, null);
 
                 final List<FacetData<SolrSearchQueryData>> facets = searchPageData.getFacets();
 
                 for (final FacetData<SolrSearchQueryData> facetData : facets)
                 {
                     if (CATEGORY_FACET_NAME.equalsIgnoreCase(facetData.getName()))
                     {
                         resultData.setCategoryResults(getCategories(facetData, term));
                     }
                 }
                 resultData.setProductCount(searchPageData.getPagination().getTotalNumberOfResults());
             }
         }
         final List<ProductData> products = productSearchFacade.textSearch(term).getResults();
         if (CollectionUtils.isNotEmpty(products))
         {
             resultData.setProducts(sublist(products));
         }
         resultData.setSuggestions(terms);
         resultData.setOriginalTerm(term);
         return resultData;
     }


 private List<CategoryResultData> getCategories(final FacetData<SolrSearchQueryData> facetData, final String term)
     {
         final List<CategoryResultData> categories = new ArrayList<>();
         for (final FacetValueData<SolrSearchQueryData> facetValueData : facetData.getTopValues())
         {
             final CategoryData data = new CategoryData();
             data.setCode(facetValueData.getCode());
             data.setName(facetValueData.getName());
             // this is ugly but we have to make query URL in such a way that it is an AND between the selected text and the category which is clicked.
             // %3A is URL encoded value for `:`
             final String categoryUrl = Config.getParameter("website.url.https") + "/search?q=" + term
                     + "%3Arelevance%3Acategories%3A" + facetValueData.getCode();
 
             data.setUrl(categoryUrl);
             final CategoryResultData categoryResultData = new CategoryResultData();
             categoryResultData.setCategory(data);
             categoryResultData.setCount(facetValueData.getCount());
             categories.add(categoryResultData);
         }
         return categories;
     }
Former Member
0 Likes

Hi Sourabh,

Please explain which class is been used for commerceProductSearchService so that I can refer the same in controller.

AND the value of the constant CATEGORY_FACET_NAME.

Answers (5)

Answers (5)

Former Member
0 Likes

web\webroot\WEB-INF_ui-src\responsive\lib\ybase-0.1.0\js\acc.autocomplete.js

if(data.categories != null){ $.each(data.categories, function (i, obj) { autoSearchData.push({ value: ACC.sanitizer.sanitize(obj.name), url: ACC.config.encodedContextPath + "/search?q=" + encodeURIComponent(data.searchTerm)+"%3Arelevance%3Abrand%3A"+"&category_string="+encodeURIComponent(obj.code), searchterm: ACC.sanitizer.sanitize(data.searchTerm), type: "categoryResult" }); }); }

Former Member
0 Likes

Thanks Saurabh , I see that in DefaultSolrProductSearchFacade.java we have the method textSearch this method returns ProductSearchPageData.

I am not sure where should I customize to get the categories for a product. Any pointers will help.

Former Member
0 Likes

Fuzzy search is very expensive operation. See an excellent answer below https://stackoverflow.com/questions/10880976/solr-lucene-fuzzy-search-too-slow

won't be a good user experience.

  • changing algorithms on /suggest would not make any difference as solr suggestions use /spellcheck endpoint. So if you wish to tweak Tokenizers, do it on /spellcheck.

Former Member
0 Likes

Hello @sps : Please follow the link where I explained how solr works. If you wish solr text search to only look at product name/description. Then make these 2 fields as text and rest of the fields as string or double.

https://answers.sap.com/questions/12769324/customizing-autosuggestion-in-searchbox-to-show-pr.html?c...

So, solr will only make tokens out of product name/description. When you search, you will get results based on your search input matching product name/description.

When you get search results, exact all categories from the result set and display to the user. See a working example here: https://www.bestandless.com.au/

Go to the above URL and search women or dresses

Former Member
0 Likes

Hello,

Even I was looking for same !! So, please find some details which i think can be useful.

  1. In the SearchPageController there is a method called getAutocompleteSuggestions.

  2. After debugging the same you will reach to DefaultSolrAutoSuggestService.getAutoSuggestionsForQuery method which internally forms the Solr Query and use SolrJ to make a webservice call to Solr Server. Which Returns the information.

  3. In the SolrConfig.xml go to the requestHandler /suggest change the alogoithm on respective searchComponent to FuzzyLookUp. Additionally u might need to change tokenizer to KeywordTokenizer on autosuggest in schema.xml