Search in sources :

Example 61 with SolrClientCache

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

the class GraphTest method testShortestPathStream.

@Test
public void testShortestPathStream() throws Exception {
    new UpdateRequest().add(id, "0", "from_s", "jim", "to_s", "mike", "predicate_s", "knows").add(id, "1", "from_s", "jim", "to_s", "dave", "predicate_s", "knows").add(id, "2", "from_s", "jim", "to_s", "stan", "predicate_s", "knows").add(id, "3", "from_s", "dave", "to_s", "stan", "predicate_s", "knows").add(id, "4", "from_s", "dave", "to_s", "bill", "predicate_s", "knows").add(id, "5", "from_s", "dave", "to_s", "mike", "predicate_s", "knows").add(id, "20", "from_s", "dave", "to_s", "alex", "predicate_s", "knows").add(id, "21", "from_s", "alex", "to_s", "steve", "predicate_s", "knows").add(id, "6", "from_s", "stan", "to_s", "alice", "predicate_s", "knows").add(id, "7", "from_s", "stan", "to_s", "mary", "predicate_s", "knows").add(id, "8", "from_s", "stan", "to_s", "dave", "predicate_s", "knows").add(id, "10", "from_s", "mary", "to_s", "mike", "predicate_s", "knows").add(id, "11", "from_s", "mary", "to_s", "max", "predicate_s", "knows").add(id, "12", "from_s", "mary", "to_s", "jim", "predicate_s", "knows").add(id, "13", "from_s", "mary", "to_s", "steve", "predicate_s", "knows").commit(cluster.getSolrClient(), COLLECTION);
    List<Tuple> tuples = null;
    Set<String> paths = null;
    ShortestPathStream stream = null;
    String zkHost = cluster.getZkServer().getZkAddress();
    StreamContext context = new StreamContext();
    SolrClientCache cache = new SolrClientCache();
    context.setSolrClientCache(cache);
    SolrParams sParams = StreamingTest.mapParams("fq", "predicate_s:knows");
    stream = new ShortestPathStream(zkHost, "collection1", "jim", "steve", "from_s", "to_s", sParams, 20, 3, 6);
    stream.setStreamContext(context);
    paths = new HashSet();
    tuples = getTuples(stream);
    assertTrue(tuples.size() == 2);
    for (Tuple tuple : tuples) {
        paths.add(tuple.getStrings("path").toString());
    }
    assertTrue(paths.contains("[jim, dave, alex, steve]"));
    assertTrue(paths.contains("[jim, stan, mary, steve]"));
    //Test with batch size of 1
    sParams = StreamingTest.mapParams("fq", "predicate_s:knows");
    stream = new ShortestPathStream(zkHost, "collection1", "jim", "steve", "from_s", "to_s", sParams, 1, 3, 6);
    stream.setStreamContext(context);
    paths = new HashSet();
    tuples = getTuples(stream);
    assertTrue(tuples.size() == 2);
    for (Tuple tuple : tuples) {
        paths.add(tuple.getStrings("path").toString());
    }
    assertTrue(paths.contains("[jim, dave, alex, steve]"));
    assertTrue(paths.contains("[jim, stan, mary, steve]"));
    //Test with bad predicate
    sParams = StreamingTest.mapParams("fq", "predicate_s:crap");
    stream = new ShortestPathStream(zkHost, "collection1", "jim", "steve", "from_s", "to_s", sParams, 1, 3, 6);
    stream.setStreamContext(context);
    paths = new HashSet();
    tuples = getTuples(stream);
    assertTrue(tuples.size() == 0);
    //Test with depth 2
    sParams = StreamingTest.mapParams("fq", "predicate_s:knows");
    stream = new ShortestPathStream(zkHost, "collection1", "jim", "steve", "from_s", "to_s", sParams, 1, 3, 2);
    stream.setStreamContext(context);
    paths = new HashSet();
    tuples = getTuples(stream);
    assertTrue(tuples.size() == 0);
    //Take out alex
    sParams = StreamingTest.mapParams("fq", "predicate_s:knows NOT to_s:alex");
    stream = new ShortestPathStream(zkHost, "collection1", "jim", "steve", "from_s", "to_s", sParams, 10, 3, 6);
    stream.setStreamContext(context);
    paths = new HashSet();
    tuples = getTuples(stream);
    assertTrue(tuples.size() == 1);
    for (Tuple tuple : tuples) {
        paths.add(tuple.getStrings("path").toString());
    }
    assertTrue(paths.contains("[jim, stan, mary, steve]"));
    cache.close();
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) SolrParams(org.apache.solr.common.params.SolrParams) Tuple(org.apache.solr.client.solrj.io.Tuple) HashSet(java.util.HashSet) StreamingTest(org.apache.solr.client.solrj.io.stream.StreamingTest) Test(org.junit.Test)

Example 62 with SolrClientCache

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

the class JDBCStreamTest method testJDBCSolrInnerJoinRollupExpression.

@Test
public void testJDBCSolrInnerJoinRollupExpression() throws Exception {
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("select", SelectStream.class).withFunctionName("hashJoin", HashJoinStream.class).withFunctionName("rollup", RollupStream.class).withFunctionName("jdbc", JDBCStream.class).withFunctionName("max", MaxMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class);
    // Load Database Data
    try (Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:.");
        Statement statement = connection.createStatement()) {
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('US', 'United States')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NL', 'Netherlands')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NP', 'Nepal')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NO', 'Norway')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (11,'Emma','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (12,'Grace','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (13,'Hailey','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (14,'Isabella','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (15,'Lily','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (16,'Madison','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (17,'Mia','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (18,'Natalie','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (19,'Olivia','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (20,'Samantha','US')");
    }
    // Load solr data
    new UpdateRequest().add(id, "1", "rating_f", "3.5", "personId_i", "11").add(id, "3", "rating_f", "2.2", "personId_i", "13").add(id, "4", "rating_f", "4.3", "personId_i", "14").add(id, "5", "rating_f", "3.5", "personId_i", "15").add(id, "8", "rating_f", "4", "personId_i", "18").add(id, "9", "rating_f", "4.1", "personId_i", "19").add(id, "2", "rating_f", "5", "personId_i", "12").add(id, "6", "rating_f", "3", "personId_i", "16").add(id, "7", "rating_f", "3", "personId_i", "17").add(id, "10", "rating_f", "4.8", "personId_i", "20").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expression;
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    try {
        // Basic test
        expression = "rollup(" + "  hashJoin(" + "    hashed=select(" + "      search(" + COLLECTIONORALIAS + ", fl=\"personId_i,rating_f\", q=\"rating_f:*\", sort=\"personId_i asc\")," + "      personId_i as personId," + "      rating_f as rating" + "    )," + "    select(" + "      jdbc(connection=\"jdbc:hsqldb:mem:.\", sql=\"select PEOPLE.ID, PEOPLE.NAME, COUNTRIES.COUNTRY_NAME from PEOPLE inner join COUNTRIES on PEOPLE.COUNTRY_CODE = COUNTRIES.CODE order by COUNTRIES.COUNTRY_NAME\", sort=\"COUNTRIES.COUNTRY_NAME asc\")," + "      ID as personId," + "      NAME as personName," + "      COUNTRY_NAME as country" + "    )," + "    on=\"personId\"" + "  )," + "  over=\"country\"," + "  max(rating)," + "  min(rating)," + "  avg(rating)," + "  count(*)" + ")";
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertEquals(2, tuples.size());
        Tuple tuple = tuples.get(0);
        assertEquals("Netherlands", tuple.getString("country"));
        assertTrue(4.3D == tuple.getDouble("max(rating)"));
        assertTrue(2.2D == tuple.getDouble("min(rating)"));
        assertTrue(3.6D == tuple.getDouble("avg(rating)"));
        assertTrue(6D == tuple.getDouble("count(*)"));
        tuple = tuples.get(1);
        assertEquals("United States", tuple.getString("country"));
        assertTrue(5D == tuple.getDouble("max(rating)"));
        assertTrue(3D == tuple.getDouble("min(rating)"));
        assertTrue(3.95D == tuple.getDouble("avg(rating)"));
        assertTrue(4D == tuple.getDouble("count(*)"));
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Statement(java.sql.Statement) Connection(java.sql.Connection) MaxMetric(org.apache.solr.client.solrj.io.stream.metrics.MaxMetric) MeanMetric(org.apache.solr.client.solrj.io.stream.metrics.MeanMetric) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 63 with SolrClientCache

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

the class JDBCStreamTest method testJDBCSolrInnerJoinExpressionWithProperties.

@Test
public void testJDBCSolrInnerJoinExpressionWithProperties() throws Exception {
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("select", SelectStream.class).withFunctionName("innerJoin", InnerJoinStream.class).withFunctionName("jdbc", JDBCStream.class);
    // Load Database Data
    try (Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:.");
        Statement statement = connection.createStatement()) {
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('US', 'United States')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NL', 'Netherlands')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NP', 'Nepal')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NO', 'Norway')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (11,'Emma','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (12,'Grace','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (13,'Hailey','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (14,'Isabella','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (15,'Lily','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (16,'Madison','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (17,'Mia','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (18,'Natalie','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (19,'Olivia','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (20,'Samantha','US')");
    }
    // Load solr data
    new UpdateRequest().add(id, "1", "rating_f", "3.5", "personId_i", "11").add(id, "2", "rating_f", "5", "personId_i", "12").add(id, "3", "rating_f", "2.2", "personId_i", "13").add(id, "4", "rating_f", "4.3", "personId_i", "14").add(id, "5", "rating_f", "3.5", "personId_i", "15").add(id, "6", "rating_f", "3", "personId_i", "16").add(id, "7", "rating_f", "3", "personId_i", "17").add(id, "8", "rating_f", "4", "personId_i", "18").add(id, "9", "rating_f", "4.1", "personId_i", "19").add(id, "10", "rating_f", "4.8", "personId_i", "20").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expression;
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    try {
        // Basic test for no alias
        expression = "innerJoin(" + "  select(" + "    search(" + COLLECTIONORALIAS + ", fl=\"personId_i,rating_f\", q=\"rating_f:*\", sort=\"personId_i asc\")," + "    personId_i as personId," + "    rating_f as rating" + "  )," + "  select(" + "    jdbc(connection=\"jdbc:hsqldb:mem:.\", sql=\"select PEOPLE.ID, PEOPLE.NAME, COUNTRIES.COUNTRY_NAME from PEOPLE inner join COUNTRIES on PEOPLE.COUNTRY_CODE = COUNTRIES.CODE order by PEOPLE.ID\", sort=\"ID asc\")," + "    ID as personId," + "    NAME as personName," + "    COUNTRY_NAME as country" + "  )," + "  on=\"personId\"" + ")";
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertEquals(10, tuples.size());
        assertOrderOf(tuples, "personId", 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
        assertOrderOf(tuples, "rating", 3.5d, 5d, 2.2d, 4.3d, 3.5d, 3d, 3d, 4d, 4.1d, 4.8d);
        assertOrderOf(tuples, "personName", "Emma", "Grace", "Hailey", "Isabella", "Lily", "Madison", "Mia", "Natalie", "Olivia", "Samantha");
        assertOrderOf(tuples, "country", "Netherlands", "United States", "Netherlands", "Netherlands", "Netherlands", "United States", "United States", "Netherlands", "Netherlands", "United States");
        // Basic test for alias
        expression = "innerJoin(" + "  select(" + "    search(" + COLLECTIONORALIAS + ", fl=\"personId_i,rating_f\", q=\"rating_f:*\", sort=\"personId_i asc\")," + "    personId_i as personId," + "    rating_f as rating" + "  )," + "  select(" + "    jdbc(connection=\"jdbc:hsqldb:mem:.\", sql=\"select PEOPLE.ID as PERSONID, PEOPLE.NAME, COUNTRIES.COUNTRY_NAME from PEOPLE inner join COUNTRIES on PEOPLE.COUNTRY_CODE = COUNTRIES.CODE order by PEOPLE.ID\", sort=\"PERSONID asc\")," + "    PERSONID as personId," + "    NAME as personName," + "    COUNTRY_NAME as country" + "  )," + "  on=\"personId\"" + ")";
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertEquals(10, tuples.size());
        assertOrderOf(tuples, "personId", 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
        assertOrderOf(tuples, "rating", 3.5d, 5d, 2.2d, 4.3d, 3.5d, 3d, 3d, 4d, 4.1d, 4.8d);
        assertOrderOf(tuples, "personName", "Emma", "Grace", "Hailey", "Isabella", "Lily", "Madison", "Mia", "Natalie", "Olivia", "Samantha");
        assertOrderOf(tuples, "country", "Netherlands", "United States", "Netherlands", "Netherlands", "Netherlands", "United States", "United States", "Netherlands", "Netherlands", "United States");
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Statement(java.sql.Statement) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) Connection(java.sql.Connection) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 64 with SolrClientCache

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

the class JDBCStreamTest method testJDBCSolrInnerJoinExpression.

@Test
public void testJDBCSolrInnerJoinExpression() throws Exception {
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("select", SelectStream.class).withFunctionName("innerJoin", InnerJoinStream.class).withFunctionName("jdbc", JDBCStream.class);
    // Load Database Data
    try (Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:.");
        Statement statement = connection.createStatement()) {
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('US', 'United States')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NL', 'Netherlands')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NP', 'Nepal')");
        statement.executeUpdate("insert into COUNTRIES (CODE,COUNTRY_NAME) values ('NO', 'Norway')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (11,'Emma','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (12,'Grace','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (13,'Hailey','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (14,'Isabella','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (15,'Lily','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (16,'Madison','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (17,'Mia','US')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (18,'Natalie','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (19,'Olivia','NL')");
        statement.executeUpdate("insert into PEOPLE (ID, NAME, COUNTRY_CODE) values (20,'Samantha','US')");
    }
    // Load solr data
    new UpdateRequest().add(id, "1", "rating_f", "3.5", "personId_i", "11").add(id, "2", "rating_f", "5", "personId_i", "12").add(id, "3", "rating_f", "2.2", "personId_i", "13").add(id, "4", "rating_f", "4.3", "personId_i", "14").add(id, "5", "rating_f", "3.5", "personId_i", "15").add(id, "6", "rating_f", "3", "personId_i", "16").add(id, "7", "rating_f", "3", "personId_i", "17").add(id, "8", "rating_f", "4", "personId_i", "18").add(id, "9", "rating_f", "4.1", "personId_i", "19").add(id, "10", "rating_f", "4.8", "personId_i", "20").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expression;
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    try {
        // Basic test
        expression = "innerJoin(" + "  select(" + "    search(" + COLLECTIONORALIAS + ", fl=\"personId_i,rating_f\", q=\"rating_f:*\", sort=\"personId_i asc\")," + "    personId_i as personId," + "    rating_f as rating" + "  )," + "  select(" + "    jdbc(connection=\"jdbc:hsqldb:mem:.\", sql=\"select PEOPLE.ID, PEOPLE.NAME, COUNTRIES.COUNTRY_NAME from PEOPLE inner join COUNTRIES on PEOPLE.COUNTRY_CODE = COUNTRIES.CODE order by PEOPLE.ID\", sort=\"ID asc\")," + "    ID as personId," + "    NAME as personName," + "    COUNTRY_NAME as country" + "  )," + "  on=\"personId\"" + ")";
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertEquals(10, tuples.size());
        assertOrderOf(tuples, "personId", 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
        assertOrderOf(tuples, "rating", 3.5d, 5d, 2.2d, 4.3d, 3.5d, 3d, 3d, 4d, 4.1d, 4.8d);
        assertOrderOf(tuples, "personName", "Emma", "Grace", "Hailey", "Isabella", "Lily", "Madison", "Mia", "Natalie", "Olivia", "Samantha");
        assertOrderOf(tuples, "country", "Netherlands", "United States", "Netherlands", "Netherlands", "Netherlands", "United States", "United States", "Netherlands", "Netherlands", "United States");
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Statement(java.sql.Statement) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) Connection(java.sql.Connection) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 65 with SolrClientCache

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

the class StreamExpressionTest method testCloudSolrStream.

@Test
public void testCloudSolrStream() throws Exception {
    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "0").add(id, "2", "a_s", "hello2", "a_i", "2", "a_f", "0").add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3").add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4").add(id, "1", "a_s", "hello1", "a_i", "1", "a_f", "1").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress());
    StreamExpression expression;
    CloudSolrStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    try {
        // Basic test
        expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\")");
        stream = new CloudSolrStream(expression, factory);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assert (tuples.size() == 5);
        assertOrder(tuples, 0, 2, 1, 3, 4);
        assertLong(tuples.get(0), "a_i", 0);
        // Basic w/aliases
        expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\", aliases=\"a_i=alias.a_i, a_s=name\")");
        stream = new CloudSolrStream(expression, factory);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assert (tuples.size() == 5);
        assertOrder(tuples, 0, 2, 1, 3, 4);
        assertLong(tuples.get(0), "alias.a_i", 0);
        assertString(tuples.get(0), "name", "hello0");
        // Basic filtered test
        expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", q=\"id:(0 3 4)\", fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\")");
        stream = new CloudSolrStream(expression, factory);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assert (tuples.size() == 3);
        assertOrder(tuples, 0, 3, 4);
        assertLong(tuples.get(1), "a_i", 3);
        try {
            expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\")");
            stream = new CloudSolrStream(expression, factory);
            stream.setStreamContext(streamContext);
            tuples = getTuples(stream);
            throw new Exception("Should be an exception here");
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("q param expected for search function"));
        }
        try {
            expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", q=\"blah\", sort=\"a_f asc, a_i asc\")");
            stream = new CloudSolrStream(expression, factory);
            stream.setStreamContext(streamContext);
            tuples = getTuples(stream);
            throw new Exception("Should be an exception here");
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("fl param expected for search function"));
        }
        try {
            expression = StreamExpressionParser.parse("search(" + COLLECTIONORALIAS + ", q=\"blah\", fl=\"id, a_f\", sort=\"a_f\")");
            stream = new CloudSolrStream(expression, factory);
            stream.setStreamContext(streamContext);
            tuples = getTuples(stream);
            throw new Exception("Should be an exception here");
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("Invalid sort spec"));
        }
        // Test with shards param
        List<String> shardUrls = TupleStream.getShards(cluster.getZkServer().getZkAddress(), COLLECTIONORALIAS, streamContext);
        Map<String, List<String>> shardsMap = new HashMap();
        shardsMap.put("myCollection", shardUrls);
        StreamContext context = new StreamContext();
        context.put("shards", shardsMap);
        context.setSolrClientCache(solrClientCache);
        // Basic test
        expression = StreamExpressionParser.parse("search(myCollection, q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\")");
        stream = new CloudSolrStream(expression, factory);
        stream.setStreamContext(context);
        tuples = getTuples(stream);
        assert (tuples.size() == 5);
        assertOrder(tuples, 0, 2, 1, 3, 4);
        assertLong(tuples.get(0), "a_i", 0);
        //Execersise the /stream hander
        //Add the shards http parameter for the myCollection
        StringBuilder buf = new StringBuilder();
        for (String shardUrl : shardUrls) {
            if (buf.length() > 0) {
                buf.append(",");
            }
            buf.append(shardUrl);
        }
        ModifiableSolrParams solrParams = new ModifiableSolrParams();
        solrParams.add("qt", "/stream");
        solrParams.add("expr", "search(myCollection, q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\")");
        solrParams.add("myCollection.shards", buf.toString());
        SolrStream solrStream = new SolrStream(shardUrls.get(0), solrParams);
        stream.setStreamContext(context);
        tuples = getTuples(stream);
        assert (tuples.size() == 5);
        assertOrder(tuples, 0, 2, 1, 3, 4);
        assertLong(tuples.get(0), "a_i", 0);
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) HashMap(java.util.HashMap) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) StreamExpression(org.apache.solr.client.solrj.io.stream.expr.StreamExpression) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Aggregations

SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)96 Tuple (org.apache.solr.client.solrj.io.Tuple)92 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)88 Test (org.junit.Test)88 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)61 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)39 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)36 SolrParams (org.apache.solr.common.params.SolrParams)27 FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)20 MultipleFieldComparator (org.apache.solr.client.solrj.io.comp.MultipleFieldComparator)14 MeanMetric (org.apache.solr.client.solrj.io.stream.metrics.MeanMetric)13 MinMetric (org.apache.solr.client.solrj.io.stream.metrics.MinMetric)12 CountMetric (org.apache.solr.client.solrj.io.stream.metrics.CountMetric)7 MaxMetric (org.apache.solr.client.solrj.io.stream.metrics.MaxMetric)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)6 FieldEqualitor (org.apache.solr.client.solrj.io.eq.FieldEqualitor)6 StreamContext (org.apache.solr.client.solrj.io.stream.StreamContext)6 SumMetric (org.apache.solr.client.solrj.io.stream.metrics.SumMetric)6