use of org.apache.solr.client.solrj.response.SuggesterResponse in project tutorials by eugenp.
the class ItemSearchServiceLiveTest method whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned.
@Test
public void whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned() throws Exception {
itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f);
itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f);
itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f);
itemSearchService.index("hm0004", "Brand2 Dishwasher", "Home washing equipments", 250f);
SolrQuery query = new SolrQuery();
query.setRequestHandler("/suggest");
query.set("suggest", "true");
query.set("suggest.build", "true");
query.set("suggest.dictionary", "mySuggester");
query.set("suggest.q", "Hom");
QueryResponse response = solrClient.query(query);
SuggesterResponse suggesterResponse = response.getSuggesterResponse();
Map<String, List<String>> suggestedTerms = suggesterResponse.getSuggestedTerms();
List<String> suggestions = suggestedTerms.get("mySuggester");
assertEquals(2, suggestions.size());
for (String term : suggestions) {
if (!"Home Appliances".equals(term) && !"Home washing equipments".equals(term)) {
fail("Unexpected suggestions");
}
}
}
use of org.apache.solr.client.solrj.response.SuggesterResponse in project ddf by codice.
the class GazetteerQueryOfflineSolr method getSuggestedNames.
@Override
public List<Suggestion> getSuggestedNames(String queryString, int maxResults) throws GeoEntryQueryException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.setRequestHandler(GAZETTEER_REQUEST_HANDLER);
solrQuery.setParam(SUGGEST_Q_KEY, ClientUtils.escapeQueryChars(queryString));
solrQuery.setParam(SUGGEST_DICT_KEY, SUGGEST_DICT);
solrQuery.setParam(SUGGEST_COUNT_KEY, Integer.toString(Math.min(maxResults, MAX_RESULTS)));
QueryResponse response;
try {
response = client.query(solrQuery);
} catch (SolrServerException | IOException e) {
throw new GeoEntryQueryException("Error while querying", e);
}
return Optional.ofNullable(response).map(QueryResponse::getSuggesterResponse).map(SuggesterResponse::getSuggestions).map(suggestionsPerDict -> suggestionsPerDict.get(SUGGEST_DICT)).orElse(Collections.emptyList()).stream().map(suggestion -> new SuggestionImpl(suggestion.getPayload(), suggestion.getTerm())).collect(Collectors.toList());
}
use of org.apache.solr.client.solrj.response.SuggesterResponse in project ddf by codice.
the class SolrMetacardClientImpl method handleSuggestionResponse.
private void handleSuggestionResponse(QueryResponse solrResponse, Map<String, Serializable> responseProps) {
SuggesterResponse suggesterResponse = solrResponse.getSuggesterResponse();
if (suggesterResponse != null) {
List<Map.Entry<String, String>> suggestionResults = suggesterResponse.getSuggestions().values().stream().flatMap(List::stream).map(suggestion -> new AbstractMap.SimpleImmutableEntry<>(suggestion.getPayload(), suggestion.getTerm())).collect(Collectors.toList());
responseProps.put(SUGGESTION_RESULT_KEY, (Serializable) suggestionResults);
}
}
Aggregations