Search in sources :

Example 81 with SolrQuery

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

the class DistributedDebugComponentTest method testDebugSections.

@Test
public void testDebugSections() throws Exception {
    SolrQuery query = new SolrQuery();
    query.setQuery("text:_query_with_no_results_");
    query.set("distrib", "true");
    query.setFields("id", "text");
    query.set("shards", shard1 + "," + shard2);
    verifyDebugSections(query, collection1);
    query.setQuery("id:1 OR text:_query_with_no_results_ OR id:[0 TO 300]");
    verifyDebugSections(query, collection1);
}
Also used : SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 82 with SolrQuery

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

the class DistributedDebugComponentTest method testRandom.

@Test
public void testRandom() throws Exception {
    final int NUM_ITERS = atLeast(50);
    for (int i = 0; i < NUM_ITERS; i++) {
        SolrClient client = random().nextBoolean() ? collection1 : collection2;
        SolrQuery q = new SolrQuery();
        q.set("distrib", "true");
        q.setFields("id", "text");
        boolean shard1Results = random().nextBoolean();
        boolean shard2Results = random().nextBoolean();
        String qs = "_query_with_no_results_";
        if (shard1Results) {
            qs += " OR batman";
        }
        if (shard2Results) {
            qs += " OR superman";
        }
        q.setQuery(qs);
        Set<String> shards = new HashSet<String>(Arrays.asList(shard1, shard2));
        if (random().nextBoolean()) {
            shards.remove(shard1);
            shard1Results = false;
        } else if (random().nextBoolean()) {
            shards.remove(shard2);
            shard2Results = false;
        }
        q.set("shards", StringUtils.join(shards, ","));
        List<String> debug = new ArrayList<String>(10);
        boolean all = false;
        final boolean timing = random().nextBoolean();
        final boolean query = random().nextBoolean();
        final boolean results = random().nextBoolean();
        final boolean track = random().nextBoolean();
        if (timing) {
            debug.add("timing");
        }
        if (query) {
            debug.add("query");
        }
        if (results) {
            debug.add("results");
        }
        if (track) {
            debug.add("track");
        }
        if (debug.isEmpty()) {
            debug.add("true");
            all = true;
        }
        q.set("debug", (String[]) debug.toArray(new String[debug.size()]));
        QueryResponse r = client.query(q);
        try {
            assertDebug(r, all || track, "track");
            assertDebug(r, all || query, "rawquerystring");
            assertDebug(r, all || query, "querystring");
            assertDebug(r, all || query, "parsedquery");
            assertDebug(r, all || query, "parsedquery_toString");
            assertDebug(r, all || query, "QParser");
            assertDebug(r, all || results, "explain");
            assertDebug(r, all || timing, "timing");
        } catch (AssertionError e) {
            throw new AssertionError(q.toString() + ": " + e.getMessage(), e);
        }
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with SolrQuery

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

the class DistributedDebugComponentTest method testSimpleSearch.

@Test
@SuppressWarnings("unchecked")
public void testSimpleSearch() throws Exception {
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.set("debug", "track");
    query.set("distrib", "true");
    query.setFields("id", "text");
    query.set("shards", shard1 + "," + shard2);
    QueryResponse response = collection1.query(query);
    NamedList<Object> track = (NamedList<Object>) response.getDebugMap().get("track");
    assertNotNull(track);
    assertNotNull(track.get("rid"));
    assertNotNull(track.get("EXECUTE_QUERY"));
    assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1));
    assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2));
    assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard1));
    assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard2));
    assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
    assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
    assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("GET_FIELDS")).get(shard1), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
    assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("GET_FIELDS")).get(shard2), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
    query.add("omitHeader", "true");
    response = collection1.query(query);
    assertNull("QTime is not included in the response when omitHeader is set to true", ((NamedList<Object>) response.getDebugMap().get("track")).findRecursive("EXECUTE_QUERY", shard1, "QTime"));
    assertNull("QTime is not included in the response when omitHeader is set to true", ((NamedList<Object>) response.getDebugMap().get("track")).findRecursive("GET_FIELDS", shard2, "QTime"));
    query.setQuery("id:1");
    response = collection1.query(query);
    track = (NamedList<Object>) response.getDebugMap().get("track");
    assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1));
    assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2));
    assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard1));
    // This test is invalid, as GET_FIELDS should not be executed in shard 2
    assertNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard2));
}
Also used : NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 84 with SolrQuery

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

the class TestDistribIDF method testSimpleQuery.

@Test
public void testSimpleQuery() throws Exception {
    //3 shards. 3rd shard won't have any data.
    createCollection("onecollection", "conf1", ImplicitDocRouter.NAME);
    createCollection("onecollection_local", "conf2", ImplicitDocRouter.NAME);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", 1);
    doc.setField("cat", "football");
    doc.addField(ShardParams._ROUTE_, "a");
    solrCluster.getSolrClient().add("onecollection", doc);
    solrCluster.getSolrClient().add("onecollection_local", doc);
    doc = new SolrInputDocument();
    doc.setField("id", 2);
    doc.setField("cat", "football");
    doc.addField(ShardParams._ROUTE_, "b");
    solrCluster.getSolrClient().add("onecollection", doc);
    solrCluster.getSolrClient().add("onecollection_local", doc);
    int nDocs = TestUtil.nextInt(random(), 10, 100);
    for (int i = 0; i < nDocs; i++) {
        doc = new SolrInputDocument();
        doc.setField("id", 3 + i);
        String cat = TestUtil.randomSimpleString(random());
        if (!cat.equals("football")) {
            //Making sure no other document has the query term in it.
            doc.setField("cat", cat);
            if (rarely()) {
                //Put most documents in shard b so that 'football' becomes 'rare' in shard b
                doc.addField(ShardParams._ROUTE_, "a");
            } else {
                doc.addField(ShardParams._ROUTE_, "b");
            }
            solrCluster.getSolrClient().add("onecollection", doc);
            solrCluster.getSolrClient().add("onecollection_local", doc);
        }
    }
    solrCluster.getSolrClient().commit("onecollection");
    solrCluster.getSolrClient().commit("onecollection_local");
    //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");
                QueryResponse queryResponse = solrClient.query("onecollection", 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.setShowDebugInfo(true);
                query.setFields("*,score");
                queryResponse = solrClient_local.query("onecollection_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 : SolrInputDocument(org.apache.solr.common.SolrInputDocument) 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 85 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)

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)327 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)129 SolrServerException (org.apache.solr.client.solrj.SolrServerException)53 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrDocument (org.apache.solr.common.SolrDocument)47 ArrayList (java.util.ArrayList)46 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 IOException (java.io.IOException)36 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 Map (java.util.Map)23 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 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18