Search in sources :

Example 26 with SolrQuery

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

the class TestDistribIDF method testMultiCollectionQuery.

@Test
public void testMultiCollectionQuery() throws Exception {
    // collection1 and collection2 are collections which have distributed idf enabled
    // collection1_local and collection2_local don't have distributed idf available
    // Only one doc has cat:football in each collection
    // When doing queries across collections we want to test that the query takes into account
    // distributed idf for the collection=collection1,collection2 query.
    // The way we verify is that score should be the same when querying across collection1 and collection2
    // But should be different when querying across collection1_local and collection2_local
    // since the idf is calculated per shard
    createCollection("collection1", "conf1");
    createCollection("collection1_local", "conf2");
    createCollection("collection2", "conf1");
    createCollection("collection2_local", "conf2");
    addDocsRandomly();
    //Test against all nodes
    for (JettySolrRunner jettySolrRunner : solrCluster.getJettySolrRunners()) {
        try (SolrClient solrClient = getHttpSolrClient(jettySolrRunner.getBaseUrl().toString())) {
            try (SolrClient solrClient_local = getHttpSolrClient(jettySolrRunner.getBaseUrl().toString())) {
                SolrQuery query = new SolrQuery("cat:football");
                query.setFields("*,score").add("collection", "collection1,collection2");
                QueryResponse queryResponse = solrClient.query("collection1", query);
                assertEquals(2, queryResponse.getResults().getNumFound());
                float score1 = (float) queryResponse.getResults().get(0).get("score");
                float score2 = (float) queryResponse.getResults().get(1).get("score");
                assertEquals("Doc1 score=" + score1 + " Doc2 score=" + score2, 0, Float.compare(score1, score2));
                query = new SolrQuery("cat:football");
                query.setFields("*,score").add("collection", "collection1_local,collection2_local");
                queryResponse = solrClient_local.query("collection1_local", query);
                assertEquals(2, queryResponse.getResults().getNumFound());
                assertEquals(2, queryResponse.getResults().get(0).get("id"));
                assertEquals(1, queryResponse.getResults().get(1).get("id"));
                float score1_local = (float) queryResponse.getResults().get(0).get("score");
                float score2_local = (float) queryResponse.getResults().get(1).get("score");
                assertEquals("Doc1 score=" + score1_local + " Doc2 score=" + score2_local, 1, Float.compare(score1_local, score2_local));
            }
        }
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 27 with SolrQuery

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

the class CloudMLTQParserTest method testMinDF.

@Test
public void testMinDF() throws Exception {
    QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u mindf=0 mintf=1}3").setShowDebugInfo(true));
    SolrDocumentList solrDocuments = queryResponse.getResults();
    int[] expectedIds = new int[] { 29, 27, 26, 28 };
    int[] actualIds = new int[solrDocuments.size()];
    int i = 0;
    for (SolrDocument solrDocument : solrDocuments) {
        actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
    }
    assertArrayEquals(expectedIds, actualIds);
    String[] expectedQueryStrings = new String[] { "+(lowerfilt_u:bmw lowerfilt_u:usa) -id:3", "+(lowerfilt_u:usa lowerfilt_u:bmw) -id:3" };
    String[] actualParsedQueries;
    if (queryResponse.getDebugMap().get("parsedquery") instanceof String) {
        String parsedQueryString = (String) queryResponse.getDebugMap().get("parsedquery");
        assertTrue(parsedQueryString.equals(expectedQueryStrings[0]) || parsedQueryString.equals(expectedQueryStrings[1]));
    } else {
        actualParsedQueries = ((ArrayList<String>) queryResponse.getDebugMap().get("parsedquery")).toArray(new String[0]);
        Arrays.sort(actualParsedQueries);
        assertArrayEquals(expectedQueryStrings, actualParsedQueries);
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 28 with SolrQuery

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

the class CloudMLTQParserTest method testBoost.

@Test
public void testBoost() throws Exception {
    QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u boost=true}17"));
    SolrDocumentList solrDocuments = queryResponse.getResults();
    int[] expectedIds = new int[] { 7, 9, 13, 14, 15, 16, 20, 22, 24, 32 };
    int[] actualIds = new int[solrDocuments.size()];
    int i = 0;
    for (SolrDocument solrDocument : solrDocuments) {
        actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
    }
    assertArrayEquals(expectedIds, actualIds);
    queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u^10,lowerfilt1_u^1000 boost=false mintf=0 mindf=0}30"));
    solrDocuments = queryResponse.getResults();
    expectedIds = new int[] { 31, 18, 23, 13, 14, 20, 22, 32, 19, 21 };
    actualIds = new int[solrDocuments.size()];
    i = 0;
    for (SolrDocument solrDocument : solrDocuments) {
        actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
    }
    System.out.println("DEBUG ACTUAL IDS 1: " + Arrays.toString(actualIds));
    assertArrayEquals(expectedIds, actualIds);
    queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u^10,lowerfilt1_u^1000 boost=true mintf=0 mindf=0}30"));
    solrDocuments = queryResponse.getResults();
    expectedIds = new int[] { 29, 31, 32, 18, 23, 13, 14, 20, 22, 19 };
    actualIds = new int[solrDocuments.size()];
    i = 0;
    for (SolrDocument solrDocument : solrDocuments) {
        actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
    }
    System.out.println("DEBUG ACTUAL IDS 2: " + Arrays.toString(actualIds));
    assertArrayEquals(expectedIds, actualIds);
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 29 with SolrQuery

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

the class CloudMLTQParserTest method testHighDFValue.

@Test
public void testHighDFValue() throws Exception {
    // Test out a high value of df and make sure nothing matches.
    QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u mindf=20 mintf=1}3"));
    SolrDocumentList solrDocuments = queryResponse.getResults();
    assertEquals("Expected to match 0 documents with a mindf of 20 but found more", solrDocuments.size(), 0);
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 30 with SolrQuery

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

the class CloudMLTQParserTest method testHighWLValue.

@Test
public void testHighWLValue() throws Exception {
    // Test out a high value of wl and make sure nothing matches.
    QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt qf=lowerfilt_u minwl=4 mintf=1}3"));
    SolrDocumentList solrDocuments = queryResponse.getResults();
    assertEquals("Expected to match 0 documents with a minwl of 4 but found more", solrDocuments.size(), 0);
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)316 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)123 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 SolrDocument (org.apache.solr.common.SolrDocument)45 ArrayList (java.util.ArrayList)44 IOException (java.io.IOException)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)32 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)23 Replica (org.apache.solr.common.cloud.Replica)23 Slice (org.apache.solr.common.cloud.Slice)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 Map (java.util.Map)20 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18