use of org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion in project tutorials by eugenp.
the class ItemSearchServiceLiveTest method whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned.
@Test
public void whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned() 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", "Washing equipments", 250f);
SolrQuery query = new SolrQuery();
query.setQuery("hme");
query.set("spellcheck", "on");
QueryResponse response = solrClient.query(query);
SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
assertEquals(false, spellCheckResponse.isCorrectlySpelled());
Suggestion suggestion = spellCheckResponse.getSuggestions().get(0);
assertEquals("hme", suggestion.getToken());
List<String> alternatives = suggestion.getAlternatives();
String alternative = alternatives.get(0);
assertEquals("home", alternative);
}
use of org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion in project nextprot-api by calipho-sib.
the class SolrServiceImpl method buildSearchResult.
private SearchResult buildSearchResult(SolrQuery query, String indexName, String url, QueryResponse response) {
SearchResult results = new SearchResult(indexName, url);
SolrDocumentList docs = response.getResults();
Logger.debug("Response doc size:" + docs.size());
List<Map<String, Object>> res = new ArrayList<>();
Map<String, Object> item = null;
for (SolrDocument doc : docs) {
item = new HashMap<>();
for (Entry<String, Object> e : doc.entrySet()) item.put(e.getKey(), e.getValue());
res.add(item);
}
results.addAllResults(res);
if (query.getStart() != null)
results.setStart(query.getStart());
results.setRows(query.getRows());
results.setElapsedTime(response.getElapsedTime());
results.setFound(docs.getNumFound());
if (docs.getMaxScore() != null)
results.setScore(docs.getMaxScore());
// Facets
List<FacetField> facetFields = response.getFacetFields();
Logger.debug("Response facet fields:" + facetFields.size());
if (facetFields != null) {
Facet facet = null;
for (FacetField ff : facetFields) {
facet = new Facet(ff.getName());
Logger.debug("Response facet field:" + ff.getName() + " count:" + ff.getValueCount());
for (Count c : ff.getValues()) facet.addFacetField(c.getName(), c.getCount());
results.addSearchResultFacet(facet);
}
}
// Spellcheck
SpellCheckResponse spellcheckResponse = response.getSpellCheckResponse();
if (spellcheckResponse != null) {
Spellcheck spellcheckResult = new Spellcheck();
List<Suggestion> suggestions = spellcheckResponse.getSuggestions();
List<Collation> collations = spellcheckResponse.getCollatedResults();
if (collations != null) {
for (Collation c : collations) spellcheckResult.addCollation(c.getCollationQueryString(), c.getNumberOfHits());
}
if (suggestions != null)
for (Suggestion s : suggestions) spellcheckResult.addSuggestions(s.getToken(), s.getAlternatives());
results.setSpellCheck(spellcheckResult);
}
return results;
}
Aggregations