Search in sources :

Example 1 with Correction

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

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)1 Collation (org.apache.solr.client.solrj.response.SpellCheckResponse.Collation)1 Correction (org.apache.solr.client.solrj.response.SpellCheckResponse.Correction)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 Test (org.junit.Test)1