private List<String> composeTerms(final String base, final SuggestionResults suggestions) { List<String> terms = new LinkedList<String>(); for (SuggestionResult suggestion : suggestions.getSuggestions()) { terms.add(base+suggestion.getToken()); } return terms; }
@Override public SuggestionResults fetchSuggestions(SuggestionRequestWrapper requestWrapper) { SearchType searchType = requestWrapper.getSearchType(); PortalSearchService searchService = fetchSearchService(searchType); if (searchService != null) { return searchService.acquireSuggestions(requestWrapper.getSearchQuery()); } else { logger.error("No searcher found: {}", searchType); return new SuggestionResults(); } }
/** * Handler returns list of user collections * * @throws IOException */ @RequestMapping(value = "/search/suggestions/{searchType}", method = { RequestMethod.GET, RequestMethod.POST }) public void fetchSuggestion(@RequestParam String term, @PathVariable String searchType, HttpServletResponse response) throws IOException { final String base = extractBase(term); final String query = extractQuery(term); SuggestionResults suggestions = suggestionHandler.fetchSuggestion(query, SearchType.fromCode(searchType)); if (suggestions.getNumFound() > 0) { new SimpleJsonResponseUtil().handleResponse(response, composeTerms(base, suggestions)); }else{ new SimpleJsonResponseUtil().handleResponse(response, ""); } }
@Override public SuggestionResults fetchSuggections(final SolrIndex<FulltextIndexSchema> index, final String query) { SolrQuery params = new SolrQuery(); params.set(PARAM_QT, "/suggest"); params.set(PARAM_QUERY, query); params.set(PARAM_SPELLCHECK, "true"); QueryResponse response = null; try { response = index.query(params); } catch (final SearchParserException spe) { log.debug("Error in query {}", query, spe); throw new SearchException("Error in query " + query, spe); } final SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse(); if (spellCheckResponse == null) { return new SuggestionResults(); } final Suggestion suggestion = spellCheckResponse.getSuggestion(query); if (suggestion == null) { return new SuggestionResults(); } List<SuggestionResult> alternatives = new ArrayList<SuggestionResult>(); for (String token : suggestion.getAlternatives()) { alternatives.add(new SuggestionResult(token)); } return new SuggestionResults(suggestion.getNumFound(), suggestion.getOriginalFrequency(), alternatives); }