Search in sources :

Example 1 with Collation

use of org.apache.solr.client.solrj.response.SpellCheckResponse.Collation in project lucene-solr by apache.

the class TestSpellCheckResponse method testSpellCheckCollationResponse.

@Test
public void testSpellCheckCollationResponse() throws Exception {
    getSolrClient();
    client.deleteByQuery("*:*");
    client.commit(true, true);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "0");
    doc.setField("name", "faith hope and love");
    client.add(doc);
    doc = new SolrInputDocument();
    doc.setField("id", "1");
    doc.setField("name", "faith hope and loaves");
    client.add(doc);
    doc = new SolrInputDocument();
    doc.setField("id", "2");
    doc.setField("name", "fat hops and loaves");
    client.add(doc);
    doc = new SolrInputDocument();
    doc.setField("id", "3");
    doc.setField("name", "faith of homer");
    client.add(doc);
    doc = new SolrInputDocument();
    doc.setField("id", "4");
    doc.setField("name", "fat of homer");
    client.add(doc);
    client.commit(true, true);
    //Test Backwards Compatibility
    SolrQuery query = new SolrQuery("name:(+fauth +home +loane)");
    query.set(CommonParams.QT, "/spell");
    query.set("spellcheck", true);
    query.set(SpellingParams.SPELLCHECK_COUNT, 10);
    query.set(SpellingParams.SPELLCHECK_COLLATE, true);
    QueryRequest request = new QueryRequest(query);
    SpellCheckResponse response = request.process(client).getSpellCheckResponse();
    response = request.process(client).getSpellCheckResponse();
    assertTrue("name:(+faith +hope +loaves)".equals(response.getCollatedResult()));
    //Test Expanded Collation Results
    query.set(SpellingParams.SPELLCHECK_COLLATE_EXTENDED_RESULTS, true);
    query.set(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, 10);
    query.set(SpellingParams.SPELLCHECK_MAX_COLLATIONS, 2);
    request = new QueryRequest(query);
    response = request.process(client).getSpellCheckResponse();
    assertTrue("name:(+faith +hope +love)".equals(response.getCollatedResult()) || "name:(+faith +hope +loaves)".equals(response.getCollatedResult()));
    List<Collation> collations = response.getCollatedResults();
    assertEquals(2, collations.size());
    for (Collation collation : collations) {
        assertTrue("name:(+faith +hope +love)".equals(collation.getCollationQueryString()) || "name:(+faith +hope +loaves)".equals(collation.getCollationQueryString()));
        assertTrue(collation.getNumberOfHits() == 1);
        List<Correction> misspellingsAndCorrections = collation.getMisspellingsAndCorrections();
        assertTrue(misspellingsAndCorrections.size() == 3);
        for (Correction correction : misspellingsAndCorrections) {
            if ("fauth".equals(correction.getOriginal())) {
                assertTrue("faith".equals(correction.getCorrection()));
            } else if ("home".equals(correction.getOriginal())) {
                assertTrue("hope".equals(correction.getCorrection()));
            } else if ("loane".equals(correction.getOriginal())) {
                assertTrue("love".equals(correction.getCorrection()) || "loaves".equals(correction.getCorrection()));
            } else {
                fail("Original Word Should have been either fauth, home or loane.");
            }
        }
    }
    query.set(SpellingParams.SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    response = request.process(client).getSpellCheckResponse();
    {
        collations = response.getCollatedResults();
        assertEquals(2, collations.size());
        String collation1 = collations.get(0).getCollationQueryString();
        String collation2 = collations.get(1).getCollationQueryString();
        assertFalse(collation1 + " equals " + collation2, collation1.equals(collation2));
        for (Collation collation : collations) {
            assertTrue("name:(+faith +hope +love)".equals(collation.getCollationQueryString()) || "name:(+faith +hope +loaves)".equals(collation.getCollationQueryString()));
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) Correction(org.apache.solr.client.solrj.response.SpellCheckResponse.Correction) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 2 with Collation

use of org.apache.solr.client.solrj.response.SpellCheckResponse.Collation 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)

Example 3 with Collation

use of org.apache.solr.client.solrj.response.SpellCheckResponse.Collation in project ddf by codice.

the class SolrMetacardClientImpl method handleSpellcheck.

private SolrDocumentList handleSpellcheck(QueryRequest request, QueryResponse solrResponse, Map<String, Serializable> responseProps, SolrQuery query, SolrDocumentList originalDocs, boolean userSpellcheckIsOn) throws IOException, SolrServerException {
    QueryResponse highlightResponse = solrResponse;
    SolrDocumentList resultDocs = originalDocs;
    if (userSpellcheckIsOn && solrSpellcheckHasResults(solrResponse)) {
        Collation collation = getCollationToResend(solrResponse);
        query.set("q", collation.getCollationQueryString());
        query.set(SPELLCHECK_KEY, false);
        highlighter.processPreQuery(request, query);
        QueryResponse solrResponseRequery = client.query(query, METHOD.POST);
        SolrDocumentList docs = solrResponseRequery.getResults();
        if (docs != null && docs.size() > originalDocs.size()) {
            resultDocs = docs;
            highlightResponse = solrResponseRequery;
            Set<String> originals = new HashSet<>();
            Set<String> corrections = new HashSet<>();
            collation.getMisspellingsAndCorrections().stream().forEach(correction -> {
                originals.add(correction.getOriginal());
                corrections.add(correction.getCorrection());
            });
            responseProps.put(DID_YOU_MEAN_KEY, new ArrayList<>(originals));
            responseProps.put(SHOWING_RESULTS_FOR_KEY, new ArrayList<>(corrections));
        }
    }
    highlighter.processPostQuery(highlightResponse, responseProps);
    return resultDocs;
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) HashSet(java.util.HashSet)

Example 4 with Collation

use of org.apache.solr.client.solrj.response.SpellCheckResponse.Collation in project ddf by codice.

the class SolrMetacardClientImpl method getCollationToResend.

private Collation getCollationToResend(QueryResponse solrResponse) {
    List<Collation> collations = solrResponse.getSpellCheckResponse().getCollatedResults();
    long maxHits = Integer.MIN_VALUE;
    Collation bestCollation = collations.get(0);
    for (Collation collation : collations) {
        if (maxHits < collation.getNumberOfHits()) {
            maxHits = collation.getNumberOfHits();
            bestCollation = collation;
        }
    }
    return bestCollation;
}
Also used : Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation)

Example 5 with Collation

use of org.apache.solr.client.solrj.response.SpellCheckResponse.Collation in project ddf by codice.

the class SolrMetacardClientImplTest method testQuerySpellCheckOn.

@Test
public void testQuerySpellCheckOn() throws Exception {
    QueryRequest request = createQuery(builder.attribute("anyText").is().like().text("normal"));
    request.getProperties().put("spellcheck", new Boolean("true"));
    List<String> names = Collections.singletonList("title");
    List<String> values = Collections.singletonList("normal");
    SpellCheckResponse spellCheckResponse = mock(SpellCheckResponse.class);
    List<Collation> collations = new ArrayList<>();
    Collation collation = new Collation();
    collation.setCollationQueryString("real");
    collation.setNumberOfHits(2);
    collations.add(collation);
    Map<String, String> attributes = createAttributes(names, values);
    mockDynamicSchemsolverCalls(createAttributeDescriptor(names), attributes);
    when(queryResponse.getSpellCheckResponse()).thenReturn(spellCheckResponse);
    when(queryResponse.getSpellCheckResponse().getCollatedResults()).thenReturn(collations);
    when(queryResponse.getResults()).thenReturn(createSolrDocumentList(attributes));
    List<Result> results = clientImpl.query(request).getResults();
    assertThat(results.size(), is(1));
    verify(queryResponse, times(2)).getResults();
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Aggregations

Collation (org.apache.solr.client.solrj.response.SpellCheckResponse.Collation)5 SpellCheckResponse (org.apache.solr.client.solrj.response.SpellCheckResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 Test (org.junit.Test)2 Result (ddf.catalog.data.Result)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)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 Correction (org.apache.solr.client.solrj.response.SpellCheckResponse.Correction)1 Suggestion (org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Facet (org.nextprot.api.solr.SearchResult.Facet)1 Spellcheck (org.nextprot.api.solr.SearchResult.Spellcheck)1