Search in sources :

Example 21 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestSuggesterResponse method testEmptySuggesterResponse.

@Test
public void testEmptySuggesterResponse() throws Exception {
    getSolrClient();
    addSampleDocs();
    SolrQuery query = new SolrQuery("*:*");
    query.set(CommonParams.QT, "/suggest");
    query.set("suggest.dictionary", "mySuggester");
    query.set("suggest.q", "Empty");
    query.set("suggest.build", true);
    QueryRequest request = new QueryRequest(query);
    QueryResponse queryResponse = request.process(client);
    SuggesterResponse response = queryResponse.getSuggesterResponse();
    Map<String, List<String>> dictionary2suggestions = response.getSuggestedTerms();
    assertTrue(dictionary2suggestions.keySet().contains("mySuggester"));
    List<String> mySuggester = dictionary2suggestions.get("mySuggester");
    assertEquals(0, mySuggester.size());
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) List(java.util.List) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 22 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestSpellCheckResponse method testSpellCheckResponse.

@Test
public void testSpellCheckResponse() throws Exception {
    getSolrClient();
    client.deleteByQuery("*:*");
    client.commit(true, true);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "111");
    doc.setField(field, "Samsung");
    client.add(doc);
    client.commit(true, true);
    SolrQuery query = new SolrQuery("*:*");
    query.set(CommonParams.QT, "/spell");
    query.set("spellcheck", true);
    query.set(SpellingParams.SPELLCHECK_Q, "samsang");
    QueryRequest request = new QueryRequest(query);
    SpellCheckResponse response = request.process(client).getSpellCheckResponse();
    Assert.assertEquals("samsung", response.getFirstSuggestion("samsang"));
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 23 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestSpellCheckResponse method testSpellCheckResponse_Extended.

@Test
public void testSpellCheckResponse_Extended() throws Exception {
    getSolrClient();
    client.deleteByQuery("*:*");
    client.commit(true, true);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "111");
    doc.setField(field, "Samsung");
    client.add(doc);
    client.commit(true, true);
    SolrQuery query = new SolrQuery("*:*");
    query.set(CommonParams.QT, "/spell");
    query.set("spellcheck", true);
    query.set(SpellingParams.SPELLCHECK_Q, "samsang");
    query.set(SpellingParams.SPELLCHECK_EXTENDED_RESULTS, true);
    QueryRequest request = new QueryRequest(query);
    SpellCheckResponse response = request.process(client).getSpellCheckResponse();
    assertEquals("samsung", response.getFirstSuggestion("samsang"));
    SpellCheckResponse.Suggestion sug = response.getSuggestion("samsang");
    List<SpellCheckResponse.Suggestion> sugs = response.getSuggestions();
    assertEquals(sug.getAlternatives().size(), sug.getAlternativeFrequencies().size());
    assertEquals(sugs.get(0).getAlternatives().size(), sugs.get(0).getAlternativeFrequencies().size());
    assertEquals("samsung", sug.getAlternatives().get(0));
    assertEquals("samsung", sugs.get(0).getAlternatives().get(0));
    // basic test if fields were filled in
    assertTrue(sug.getEndOffset() > 0);
    assertTrue(sug.getToken().length() > 0);
    assertTrue(sug.getNumFound() > 0);
    // assertTrue(sug.getOriginalFrequency() > 0);
    // Hmmm... the API for SpellCheckResponse could be nicer:
    response.getSuggestions().get(0).getAlternatives().get(0);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 24 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest 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 25 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class ShardSplitTest method splitShard.

protected void splitShard(String collection, String shardId, List<DocRouter.Range> subRanges, String splitKey) throws SolrServerException, IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionParams.CollectionAction.SPLITSHARD.toString());
    params.set("collection", collection);
    if (shardId != null) {
        params.set("shard", shardId);
    }
    if (subRanges != null) {
        StringBuilder ranges = new StringBuilder();
        for (int i = 0; i < subRanges.size(); i++) {
            DocRouter.Range subRange = subRanges.get(i);
            ranges.append(subRange.toString());
            if (i < subRanges.size() - 1)
                ranges.append(",");
        }
        params.set("ranges", ranges.toString());
    }
    if (splitKey != null) {
        params.set("split.key", splitKey);
    }
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    String baseUrl = ((HttpSolrClient) shardToJetty.get(SHARD1).get(0).client.solrClient).getBaseURL();
    baseUrl = baseUrl.substring(0, baseUrl.length() - "collection1".length());
    try (HttpSolrClient baseServer = getHttpSolrClient(baseUrl)) {
        baseServer.setConnectionTimeout(30000);
        baseServer.setSoTimeout(60000 * 5);
        baseServer.request(request);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) DocRouter(org.apache.solr.common.cloud.DocRouter) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)110 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)75 SolrRequest (org.apache.solr.client.solrj.SolrRequest)35 NamedList (org.apache.solr.common.util.NamedList)35 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Test (org.junit.Test)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)17 Map (java.util.Map)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)17 IOException (java.io.IOException)16 SolrException (org.apache.solr.common.SolrException)15 SolrInputDocument (org.apache.solr.common.SolrInputDocument)14 SolrClient (org.apache.solr.client.solrj.SolrClient)13 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)12 List (java.util.List)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)8