Search in sources :

Example 71 with UpdateRequest

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

the class StreamExpressionTest method testSelectStream.

@Test
public void testSelectStream() throws Exception {
    new UpdateRequest().add(id, "1", "side_s", "left", "join1_i", "0", "join2_s", "a", "ident_s", // 8, 9
    "left_1").add(id, "15", "side_s", "left", "join1_i", "0", "join2_s", "a", "ident_s", // 8, 9
    "left_1").add(id, "2", "side_s", "left", "join1_i", "0", "join2_s", "b", "ident_s", "left_2").add(id, "3", "side_s", "left", "join1_i", "1", "join2_s", "a", "ident_s", // 10
    "left_3").add(id, "4", "side_s", "left", "join1_i", "1", "join2_s", "b", "ident_s", // 11
    "left_4").add(id, "5", "side_s", "left", "join1_i", "1", "join2_s", "c", "ident_s", // 12
    "left_5").add(id, "6", "side_s", "left", "join1_i", "2", "join2_s", "d", "ident_s", "left_6").add(id, "7", "side_s", "left", "join1_i", "3", "join2_s", "e", "ident_s", // 14
    "left_7").add(id, "8", "side_s", "right", "join1_i", "0", "join2_s", "a", "ident_s", "right_1", "join3_i", // 1,15
    "0").add(id, "9", "side_s", "right", "join1_i", "0", "join2_s", "a", "ident_s", "right_2", "join3_i", // 1,15
    "0").add(id, "10", "side_s", "right", "join1_i", "1", "join2_s", "a", "ident_s", "right_3", "join3_i", // 3
    "1").add(id, "11", "side_s", "right", "join1_i", "1", "join2_s", "b", "ident_s", "right_4", "join3_i", // 4
    "1").add(id, "12", "side_s", "right", "join1_i", "1", "join2_s", "c", "ident_s", "right_5", "join3_i", // 5
    "1").add(id, "13", "side_s", "right", "join1_i", "2", "join2_s", "dad", "ident_s", "right_6", "join3_i", "2").add(id, "14", "side_s", "right", "join1_i", "3", "join2_s", "e", "ident_s", "right_7", "join3_i", // 7
    "3").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String clause;
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("innerJoin", InnerJoinStream.class).withFunctionName("select", SelectStream.class).withFunctionName("replace", ReplaceOperation.class).withFunctionName("concat", ConcatOperation.class).withFunctionName("add", AddEvaluator.class).withFunctionName("if", IfThenElseEvaluator.class).withFunctionName("gt", GreaterThanEvaluator.class);
    try {
        // Basic test
        clause = "select(" + "id, join1_i as join1, join2_s as join2, ident_s as identity," + "search(collection1, q=\"side_s:left\", fl=\"id,join1_i,join2_s,ident_s\", sort=\"join1_i asc, join2_s asc, id asc\")" + ")";
        stream = factory.constructStream(clause);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertFields(tuples, "id", "join1", "join2", "identity");
        assertNotFields(tuples, "join1_i", "join2_s", "ident_s");
        // Basic with replacements test
        clause = "select(" + "id, join1_i as join1, join2_s as join2, ident_s as identity," + "replace(join1, 0, withValue=12), replace(join1, 3, withValue=12), replace(join1, 2, withField=join2)," + "search(collection1, q=\"side_s:left\", fl=\"id,join1_i,join2_s,ident_s\", sort=\"join1_i asc, join2_s asc, id asc\")" + ")";
        stream = factory.constructStream(clause);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertFields(tuples, "id", "join1", "join2", "identity");
        assertNotFields(tuples, "join1_i", "join2_s", "ident_s");
        assertLong(tuples.get(0), "join1", 12);
        assertLong(tuples.get(1), "join1", 12);
        assertLong(tuples.get(2), "join1", 12);
        assertLong(tuples.get(7), "join1", 12);
        assertString(tuples.get(6), "join1", "d");
        // Basic with replacements and concat test
        clause = "select(" + "id, join1_i as join1, join2_s as join2, ident_s as identity," + "replace(join1, 0, withValue=12), replace(join1, 3, withValue=12), replace(join1, 2, withField=join2)," + "concat(fields=\"identity,join1\", as=\"newIdentity\",delim=\"-\")," + "search(collection1, q=\"side_s:left\", fl=\"id,join1_i,join2_s,ident_s\", sort=\"join1_i asc, join2_s asc, id asc\")" + ")";
        stream = factory.constructStream(clause);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertFields(tuples, "id", "join1", "join2", "identity", "newIdentity");
        assertNotFields(tuples, "join1_i", "join2_s", "ident_s");
        assertLong(tuples.get(0), "join1", 12);
        assertString(tuples.get(0), "newIdentity", "left_1-12");
        assertLong(tuples.get(1), "join1", 12);
        assertString(tuples.get(1), "newIdentity", "left_1-12");
        assertLong(tuples.get(2), "join1", 12);
        assertString(tuples.get(2), "newIdentity", "left_2-12");
        assertLong(tuples.get(7), "join1", 12);
        assertString(tuples.get(7), "newIdentity", "left_7-12");
        assertString(tuples.get(6), "join1", "d");
        assertString(tuples.get(6), "newIdentity", "left_6-d");
        // Inner stream test
        clause = "innerJoin(" + "select(" + "id, join1_i as left.join1, join2_s as left.join2, ident_s as left.ident," + "search(collection1, q=\"side_s:left\", fl=\"id,join1_i,join2_s,ident_s\", sort=\"join1_i asc, join2_s asc, id asc\")" + ")," + "select(" + "join3_i as right.join1, join2_s as right.join2, ident_s as right.ident," + "search(collection1, q=\"side_s:right\", fl=\"join3_i,join2_s,ident_s\", sort=\"join3_i asc, join2_s asc\")," + ")," + "on=\"left.join1=right.join1, left.join2=right.join2\"" + ")";
        stream = factory.constructStream(clause);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertFields(tuples, "id", "left.join1", "left.join2", "left.ident", "right.join1", "right.join2", "right.ident");
        // Wrapped select test
        clause = "select(" + "id, left.ident, right.ident," + "innerJoin(" + "select(" + "id, join1_i as left.join1, join2_s as left.join2, ident_s as left.ident," + "search(collection1, q=\"side_s:left\", fl=\"id,join1_i,join2_s,ident_s\", sort=\"join1_i asc, join2_s asc, id asc\")" + ")," + "select(" + "join3_i as right.join1, join2_s as right.join2, ident_s as right.ident," + "search(collection1, q=\"side_s:right\", fl=\"join3_i,join2_s,ident_s\", sort=\"join3_i asc, join2_s asc\")," + ")," + "on=\"left.join1=right.join1, left.join2=right.join2\"" + ")" + ")";
        stream = factory.constructStream(clause);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertFields(tuples, "id", "left.ident", "right.ident");
        assertNotFields(tuples, "left.join1", "left.join2", "right.join1", "right.join2");
    } finally {
        solrClientCache.close();
    }
}
Also used : ConcatOperation(org.apache.solr.client.solrj.io.ops.ConcatOperation) IfThenElseEvaluator(org.apache.solr.client.solrj.io.eval.IfThenElseEvaluator) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) 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 72 with UpdateRequest

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

the class StreamExpressionTest method testParallelReducerStream.

@Test
public void testParallelReducerStream() throws Exception {
    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1").add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2").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", "hello0", "a_i", "1", "a_f", "5").add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6").add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7").add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8").add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9").add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    String zkHost = cluster.getZkServer().getZkAddress();
    StreamFactory streamFactory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, zkHost).withFunctionName("search", CloudSolrStream.class).withFunctionName("group", GroupOperation.class).withFunctionName("reduce", ReducerStream.class).withFunctionName("parallel", ParallelStream.class);
    try {
        ParallelStream pstream = (ParallelStream) streamFactory.constructStream("parallel(" + COLLECTIONORALIAS + ", " + "reduce(" + "search(" + COLLECTIONORALIAS + ", q=\"*:*\", fl=\"id,a_s,a_i,a_f\", sort=\"a_s asc,a_f asc\", partitionKeys=\"a_s\"), " + "by=\"a_s\"," + "group(sort=\"a_i asc\", n=\"5\")), " + "workers=\"2\", zkHost=\"" + zkHost + "\", sort=\"a_s asc\")");
        pstream.setStreamContext(streamContext);
        List<Tuple> tuples = getTuples(pstream);
        assert (tuples.size() == 3);
        Tuple t0 = tuples.get(0);
        List<Map> maps0 = t0.getMaps("group");
        assertMaps(maps0, 0, 1, 2, 9);
        Tuple t1 = tuples.get(1);
        List<Map> maps1 = t1.getMaps("group");
        assertMaps(maps1, 3, 5, 7, 8);
        Tuple t2 = tuples.get(2);
        List<Map> maps2 = t2.getMaps("group");
        assertMaps(maps2, 4, 6);
        pstream = (ParallelStream) streamFactory.constructStream("parallel(" + COLLECTIONORALIAS + ", " + "reduce(" + "search(" + COLLECTIONORALIAS + ", q=\"*:*\", fl=\"id,a_s,a_i,a_f\", sort=\"a_s desc,a_f asc\", partitionKeys=\"a_s\"), " + "by=\"a_s\", " + "group(sort=\"a_i desc\", n=\"5\"))," + "workers=\"2\", zkHost=\"" + zkHost + "\", sort=\"a_s desc\")");
        pstream.setStreamContext(streamContext);
        tuples = getTuples(pstream);
        assert (tuples.size() == 3);
        t0 = tuples.get(0);
        maps0 = t0.getMaps("group");
        assertMaps(maps0, 6, 4);
        t1 = tuples.get(1);
        maps1 = t1.getMaps("group");
        assertMaps(maps1, 8, 7, 5, 3);
        t2 = tuples.get(2);
        maps2 = t2.getMaps("group");
        assertMaps(maps2, 9, 2, 1, 0);
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 73 with UpdateRequest

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

the class JDBCStreamTest method testJDBCSolrMerge.

@Test
public void testJDBCSolrMerge() throws Exception {
    // 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 COUNTRIES (CODE,COUNTRY_NAME) values ('AL', 'Algeria')");
    }
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    // Load Solr
    new UpdateRequest().add(id, "0", "code_s", "GB", "name_s", "Great Britian").add(id, "1", "code_s", "CA", "name_s", "Canada").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class);
    List<Tuple> tuples;
    try {
        // Simple 1
        TupleStream jdbcStream = new JDBCStream("jdbc:hsqldb:mem:.", "select CODE,COUNTRY_NAME from COUNTRIES order by CODE", new FieldComparator("CODE", ComparatorOrder.ASCENDING));
        TupleStream selectStream = new SelectStream(jdbcStream, new HashMap<String, String>() {

            {
                put("CODE", "code_s");
                put("COUNTRY_NAME", "name_s");
            }
        });
        TupleStream searchStream = factory.constructStream("search(" + COLLECTIONORALIAS + ", fl=\"code_s,name_s\",q=\"*:*\",sort=\"code_s asc\")");
        TupleStream mergeStream = new MergeStream(new FieldComparator("code_s", ComparatorOrder.ASCENDING), new TupleStream[] { selectStream, searchStream });
        mergeStream.setStreamContext(streamContext);
        tuples = getTuples(mergeStream);
        assertEquals(7, tuples.size());
        assertOrderOf(tuples, "code_s", "AL", "CA", "GB", "NL", "NO", "NP", "US");
        assertOrderOf(tuples, "name_s", "Algeria", "Canada", "Great Britian", "Netherlands", "Norway", "Nepal", "United States");
    } finally {
        solrClientCache.close();
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Statement(java.sql.Statement) Connection(java.sql.Connection) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 74 with UpdateRequest

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

the class JdbcTest method setupCluster.

@BeforeClass
public static void setupCluster() throws Exception {
    configureCluster(2).addConfig("conf", getFile("solrj").toPath().resolve("solr").resolve("configsets").resolve("streaming").resolve("conf")).configure();
    String collection;
    boolean useAlias = random().nextBoolean();
    if (useAlias) {
        collection = COLLECTIONORALIAS + "_collection";
    } else {
        collection = COLLECTIONORALIAS;
    }
    CollectionAdminRequest.createCollection(collection, "conf", 2, 1).process(cluster.getSolrClient());
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(collection, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);
    if (useAlias) {
        CollectionAdminRequest.createAlias(COLLECTIONORALIAS, collection).process(cluster.getSolrClient());
    }
    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1", "testnull_i", null).add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2", "testnull_i", "2").add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3", "testnull_i", null).add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4", "testnull_i", "4").add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5", "testnull_i", null).add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6", "testnull_i", "6").add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7", "testnull_i", null).add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8", "testnull_i", "8").add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9", "testnull_i", null).add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10", "testnull_i", "10").commit(cluster.getSolrClient(), collection);
    zkHost = cluster.getZkServer().getZkAddress();
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) BeforeClass(org.junit.BeforeClass)

Example 75 with UpdateRequest

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

the class StreamExpressionTest method testPercentiles.

@Test
public void testPercentiles() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    int i = 0;
    while (i < 100) {
        i = i + 2;
        updateRequest.add(id, "id_" + (i), "price_f", Integer.toString(i));
    }
    updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expr = "search(" + COLLECTIONORALIAS + ", q=\"*:*\", fl=\"price_f\", sort=\"price_f asc\", rows=\"200\")";
    String cexpr = "let(a=" + expr + ", c=col(a, price_f), e=empiricalDistribution(c), " + "tuple(p1=percentile(e, 88), " + "p2=percentile(e, 2), " + "p3=percentile(e, 99), " + "p4=percentile(e, 77), " + "p5=percentile(e, 98)))";
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
    paramsLoc.set("expr", cexpr);
    paramsLoc.set("qt", "/stream");
    String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString() + "/" + COLLECTIONORALIAS;
    TupleStream solrStream = new SolrStream(url, paramsLoc);
    StreamContext context = new StreamContext();
    solrStream.setStreamContext(context);
    List<Tuple> tuples = getTuples(solrStream);
    assertTrue(tuples.size() == 1);
    double percentile1 = tuples.get(0).getDouble("p1");
    double percentile2 = tuples.get(0).getDouble("p2");
    double percentile3 = tuples.get(0).getDouble("p3");
    double percentile4 = tuples.get(0).getDouble("p4");
    double percentile5 = tuples.get(0).getDouble("p5");
    assertEquals(.88D, percentile1, 0.001);
    assertEquals(.0D, percentile2, 0.001);
    assertEquals(1.0D, percentile3, 0.001);
    assertEquals(.78D, percentile4, 0.001);
    assertEquals(.98D, percentile5, 0.001);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Aggregations

UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)276 Test (org.junit.Test)151 Tuple (org.apache.solr.client.solrj.io.Tuple)114 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)89 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)88 SolrInputDocument (org.apache.solr.common.SolrInputDocument)76 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)64 ArrayList (java.util.ArrayList)38 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)36 IOException (java.io.IOException)30 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)26 SolrParams (org.apache.solr.common.params.SolrParams)26 SolrQuery (org.apache.solr.client.solrj.SolrQuery)24 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)23 AbstractUpdateRequest (org.apache.solr.client.solrj.request.AbstractUpdateRequest)22 HashMap (java.util.HashMap)21 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)21 SolrServerException (org.apache.solr.client.solrj.SolrServerException)20 List (java.util.List)19