Search in sources :

Example 1 with Suggestion

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);
}
Also used : Suggestion(org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) Test(org.junit.Test)

Example 2 with Suggestion

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;
}
Also used : Spellcheck(org.nextprot.api.solr.SearchResult.Spellcheck) FacetField(org.apache.solr.client.solrj.response.FacetField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Count(org.apache.solr.client.solrj.response.FacetField.Count) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) Suggestion(org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion) SolrDocument(org.apache.solr.common.SolrDocument) Facet(org.nextprot.api.solr.SearchResult.Facet)

Aggregations

SpellCheckResponse (org.apache.solr.client.solrj.response.SpellCheckResponse)2 Suggestion (org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 FacetField (org.apache.solr.client.solrj.response.FacetField)1 Count (org.apache.solr.client.solrj.response.FacetField.Count)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 Collation (org.apache.solr.client.solrj.response.SpellCheckResponse.Collation)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 Test (org.junit.Test)1 Facet (org.nextprot.api.solr.SearchResult.Facet)1 Spellcheck (org.nextprot.api.solr.SearchResult.Spellcheck)1