Search in sources :

Example 61 with Tuple

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

the class StreamExpressionTest method testCellStream.

@Test
public void testCellStream() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.add(id, "hello", "test_t", "l b c d c e");
    updateRequest.add(id, "hello1", "test_t", "l b c d c");
    updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expr = "search(" + COLLECTIONORALIAS + ", q=\"*:*\", fl=\"id,test_t\", sort=\"id desc\")";
    String cat = "cell(results," + expr + ")";
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
    paramsLoc.set("expr", cat);
    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);
    List<Map> results = (List<Map>) tuples.get(0).get("results");
    assertTrue(results.get(0).get("id").equals("hello1"));
    assertTrue(results.get(0).get("test_t").equals("l b c d c"));
    assertTrue(results.get(1).get("id").equals("hello"));
    assertTrue(results.get(1).get("test_t").equals("l b c d c e"));
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 62 with Tuple

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

the class StreamExpressionTest method testParallelUpdateStream.

@Test
public void testParallelUpdateStream() throws Exception {
    CollectionAdminRequest.createCollection("parallelDestinationCollection", "conf", 2, 1).process(cluster.getSolrClient());
    AbstractDistribZkTestBase.waitForRecoveriesToFinish("parallelDestinationCollection", cluster.getSolrClient().getZkStateReader(), false, true, TIMEOUT);
    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "0", "s_multi", "aaaa", "s_multi", "bbbb", "i_multi", "4", "i_multi", "7").add(id, "2", "a_s", "hello2", "a_i", "2", "a_f", "0", "s_multi", "aaaa1", "s_multi", "bbbb1", "i_multi", "44", "i_multi", "77").add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3", "s_multi", "aaaa2", "s_multi", "bbbb2", "i_multi", "444", "i_multi", "777").add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4", "s_multi", "aaaa3", "s_multi", "bbbb3", "i_multi", "4444", "i_multi", "7777").add(id, "1", "a_s", "hello1", "a_i", "1", "a_f", "1", "s_multi", "aaaa4", "s_multi", "bbbb4", "i_multi", "44444", "i_multi", "77777").commit(cluster.getSolrClient(), "collection1");
    StreamExpression expression;
    TupleStream stream;
    Tuple t;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    String zkHost = cluster.getZkServer().getZkAddress();
    StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withCollectionZkHost("parallelDestinationCollection", cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("update", UpdateStream.class).withFunctionName("parallel", ParallelStream.class);
    try {
        //Copy all docs to destinationCollection
        String updateExpression = "update(parallelDestinationCollection, batchSize=2, search(collection1, q=*:*, fl=\"id,a_s,a_i,a_f,s_multi,i_multi\", sort=\"a_f asc, a_i asc\", partitionKeys=\"a_f\"))";
        TupleStream parallelUpdateStream = factory.constructStream("parallel(collection1, " + updateExpression + ", workers=\"2\", zkHost=\"" + zkHost + "\", sort=\"batchNumber asc\")");
        parallelUpdateStream.setStreamContext(streamContext);
        List<Tuple> tuples = getTuples(parallelUpdateStream);
        cluster.getSolrClient().commit("parallelDestinationCollection");
        //Ensure that all UpdateStream tuples indicate the correct number of copied/indexed docs
        long count = 0;
        for (Tuple tuple : tuples) {
            count += tuple.getLong("batchIndexed");
        }
        assert (count == 5);
        //Ensure that destinationCollection actually has the new docs.
        expression = StreamExpressionParser.parse("search(parallelDestinationCollection, q=*:*, fl=\"id,a_s,a_i,a_f,s_multi,i_multi\", sort=\"a_i asc\")");
        stream = new CloudSolrStream(expression, factory);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);
        assertEquals(5, tuples.size());
        Tuple tuple = tuples.get(0);
        assert (tuple.getLong("id") == 0);
        assert (tuple.get("a_s").equals("hello0"));
        assert (tuple.getLong("a_i") == 0);
        assert (tuple.getDouble("a_f") == 0.0);
        assertList(tuple.getStrings("s_multi"), "aaaa", "bbbb");
        assertList(tuple.getLongs("i_multi"), Long.parseLong("4"), Long.parseLong("7"));
        tuple = tuples.get(1);
        assert (tuple.getLong("id") == 1);
        assert (tuple.get("a_s").equals("hello1"));
        assert (tuple.getLong("a_i") == 1);
        assert (tuple.getDouble("a_f") == 1.0);
        assertList(tuple.getStrings("s_multi"), "aaaa4", "bbbb4");
        assertList(tuple.getLongs("i_multi"), Long.parseLong("44444"), Long.parseLong("77777"));
        tuple = tuples.get(2);
        assert (tuple.getLong("id") == 2);
        assert (tuple.get("a_s").equals("hello2"));
        assert (tuple.getLong("a_i") == 2);
        assert (tuple.getDouble("a_f") == 0.0);
        assertList(tuple.getStrings("s_multi"), "aaaa1", "bbbb1");
        assertList(tuple.getLongs("i_multi"), Long.parseLong("44"), Long.parseLong("77"));
        tuple = tuples.get(3);
        assert (tuple.getLong("id") == 3);
        assert (tuple.get("a_s").equals("hello3"));
        assert (tuple.getLong("a_i") == 3);
        assert (tuple.getDouble("a_f") == 3.0);
        assertList(tuple.getStrings("s_multi"), "aaaa2", "bbbb2");
        assertList(tuple.getLongs("i_multi"), Long.parseLong("444"), Long.parseLong("777"));
        tuple = tuples.get(4);
        assert (tuple.getLong("id") == 4);
        assert (tuple.get("a_s").equals("hello4"));
        assert (tuple.getLong("a_i") == 4);
        assert (tuple.getDouble("a_f") == 4.0);
        assertList(tuple.getStrings("s_multi"), "aaaa3", "bbbb3");
        assertList(tuple.getLongs("i_multi"), Long.parseLong("4444"), Long.parseLong("7777"));
    } finally {
        CollectionAdminRequest.deleteCollection("parallelDestinationCollection").process(cluster.getSolrClient());
        solrClientCache.close();
    }
}
Also used : StreamExpression(org.apache.solr.client.solrj.io.stream.expr.StreamExpression) 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 63 with Tuple

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

the class StreamExpressionTest method testListStream.

@Test
public void testListStream() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.add(id, "hello", "test_t", "l b c d c");
    updateRequest.add(id, "hello1", "test_t", "l b c d c");
    updateRequest.add(id, "hello2", "test_t", "l b c d c");
    updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expr1 = "search(" + COLLECTIONORALIAS + ", q=\"id:hello\",  fl=id, sort=\"id desc\")";
    String expr2 = "search(" + COLLECTIONORALIAS + ", q=\"id:hello1\", fl=id, sort=\"id desc\")";
    String expr3 = "search(" + COLLECTIONORALIAS + ", q=\"id:hello2\", fl=id, sort=\"id desc\")";
    String cat = "list(" + expr1 + "," + expr2 + "," + expr3 + ")";
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
    paramsLoc.set("expr", cat);
    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() == 3);
    String s = (String) tuples.get(0).get("id");
    assertTrue(s.equals("hello"));
    s = (String) tuples.get(1).get("id");
    assertTrue(s.equals("hello1"));
    s = (String) tuples.get(2).get("id");
    assertTrue(s.equals("hello2"));
}
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)

Example 64 with Tuple

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

the class StreamExpressionTest method testNormalize.

@Test
public void testNormalize() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.add(id, "1", "price_f", "100.0", "col_s", "a", "order_i", "1");
    updateRequest.add(id, "2", "price_f", "200.0", "col_s", "a", "order_i", "2");
    updateRequest.add(id, "3", "price_f", "300.0", "col_s", "a", "order_i", "3");
    updateRequest.add(id, "4", "price_f", "100.0", "col_s", "a", "order_i", "4");
    updateRequest.add(id, "5", "price_f", "200.0", "col_s", "a", "order_i", "5");
    updateRequest.add(id, "6", "price_f", "400.0", "col_s", "a", "order_i", "6");
    updateRequest.add(id, "7", "price_f", "600.0", "col_s", "a", "order_i", "7");
    updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    String expr1 = "search(" + COLLECTIONORALIAS + ", q=\"col_s:a\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
    String cexpr = "let(a=" + expr1 + ", c=col(a, price_f), tuple(n=normalize(c), c=c))";
    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);
    Tuple tuple = tuples.get(0);
    List<Double> col = (List<Double>) tuple.get("c");
    List<Double> normalized = (List<Double>) tuple.get("n");
    assertTrue(col.size() == normalized.size());
    double total = 0.0D;
    for (double d : normalized) {
        total += d;
    }
    double mean = total / normalized.size();
    assert (Math.round(mean) == 0);
    double sd = 0;
    for (int i = 0; i < normalized.size(); i++) {
        sd += Math.pow(normalized.get(i) - mean, 2) / normalized.size();
    }
    double standardDeviation = Math.sqrt(sd);
    assertTrue(Math.round(standardDeviation) == 1);
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 65 with Tuple

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

the class StreamExpressionTest method testParallelHavingStream.

@Test
public void testParallelHavingStream() throws Exception {
    SolrClientCache solrClientCache = new SolrClientCache();
    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1", "subject", "blah blah blah 0").add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2", "subject", "blah blah blah 2").add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3", "subject", "blah blah blah 3").add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4", "subject", "blah blah blah 4").add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5", "subject", "blah blah blah 1").add(id, "5", "a_s", "hello3", "a_i", "5", "a_f", "6", "subject", "blah blah blah 5").add(id, "6", "a_s", "hello4", "a_i", "6", "a_f", "7", "subject", "blah blah blah 6").add(id, "7", "a_s", "hello3", "a_i", "7", "a_f", "8", "subject", "blah blah blah 7").add(id, "8", "a_s", "hello3", "a_i", "8", "a_f", "9", "subject", "blah blah blah 8").add(id, "9", "a_s", "hello0", "a_i", "9", "a_f", "10", "subject", "blah blah blah 9").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
    TupleStream stream;
    List<Tuple> tuples;
    StreamFactory factory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress()).withFunctionName("search", CloudSolrStream.class).withFunctionName("having", HavingStream.class).withFunctionName("rollup", RollupStream.class).withFunctionName("sum", SumMetric.class).withFunctionName("and", AndEvaluator.class).withFunctionName("or", OrEvaluator.class).withFunctionName("not", NotEvaluator.class).withFunctionName("gt", GreaterThanEvaluator.class).withFunctionName("lt", LessThanEvaluator.class).withFunctionName("eq", EqualsEvaluator.class).withFunctionName("lteq", LessThanEqualToEvaluator.class).withFunctionName("gteq", GreaterThanEqualToEvaluator.class).withFunctionName("val", RawValueEvaluator.class).withFunctionName("parallel", ParallelStream.class);
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\", having(search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=id), eq(a_i, 9)))");
    StreamContext context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 1);
    Tuple t = tuples.get(0);
    assertTrue(t.getString("id").equals("9"));
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\", having(search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=id), and(eq(a_i, 9),lt(a_i, 10))))");
    context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 1);
    t = tuples.get(0);
    assertTrue(t.getString("id").equals("9"));
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\",having(search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=id), or(eq(a_i, 9),eq(a_i, 8))))");
    context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 2);
    t = tuples.get(0);
    assertTrue(t.getString("id").equals("8"));
    t = tuples.get(1);
    assertTrue(t.getString("id").equals("9"));
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\", having(search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=id), and(eq(a_i, 9),not(eq(a_i, 9)))))");
    context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 0);
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\",having(search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=id), and(lteq(a_i, 9), gteq(a_i, 8))))");
    context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 2);
    t = tuples.get(0);
    assertTrue(t.getString("id").equals("8"));
    t = tuples.get(1);
    assertTrue(t.getString("id").equals("9"));
    stream = factory.constructStream("parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"a_f asc\", having(rollup(over=a_f, sum(a_i), search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc\", partitionKeys=a_f)), and(eq(sum(a_i), 9),eq(sum(a_i),9))))");
    context = new StreamContext();
    context.setSolrClientCache(solrClientCache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    assert (tuples.size() == 1);
    t = tuples.get(0);
    assertTrue(t.getDouble("a_f") == 10.0D);
    solrClientCache.close();
}
Also used : RawValueEvaluator(org.apache.solr.client.solrj.io.eval.RawValueEvaluator) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) NotEvaluator(org.apache.solr.client.solrj.io.eval.NotEvaluator) AndEvaluator(org.apache.solr.client.solrj.io.eval.AndEvaluator) LessThanEqualToEvaluator(org.apache.solr.client.solrj.io.eval.LessThanEqualToEvaluator) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache) LessThanEvaluator(org.apache.solr.client.solrj.io.eval.LessThanEvaluator) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Aggregations

Tuple (org.apache.solr.client.solrj.io.Tuple)382 Test (org.junit.Test)259 StreamEvaluator (org.apache.solr.client.solrj.io.eval.StreamEvaluator)135 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)114 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)92 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)88 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)64 HashMap (java.util.HashMap)54 ArrayList (java.util.ArrayList)47 SolrParams (org.apache.solr.common.params.SolrParams)43 Map (java.util.Map)41 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)36 IOException (java.io.IOException)34 List (java.util.List)28 FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)23 StreamOperation (org.apache.solr.client.solrj.io.ops.StreamOperation)16 MultipleFieldComparator (org.apache.solr.client.solrj.io.comp.MultipleFieldComparator)15 SolrStream (org.apache.solr.client.solrj.io.stream.SolrStream)15 MeanMetric (org.apache.solr.client.solrj.io.stream.metrics.MeanMetric)15 MinMetric (org.apache.solr.client.solrj.io.stream.metrics.MinMetric)14