Search in sources :

Example 1 with StreamFactory

use of org.apache.solr.client.solrj.io.stream.expr.StreamFactory in project lucene-solr by apache.

the class SolrTable method handleSelectDistinctMapReduce.

private TupleStream handleSelectDistinctMapReduce(final String zkHost, final String collection, final Properties properties, final List<Map.Entry<String, Class>> fields, final String query, final List<Pair<String, String>> orders, final Bucket[] buckets, final String limit) throws IOException {
    int numWorkers = Integer.parseInt(properties.getProperty("numWorkers", "1"));
    String fl = getFields(fields);
    String sort = null;
    StreamEqualitor ecomp = null;
    StreamComparator comp = null;
    if (orders != null && orders.size() > 0) {
        StreamComparator[] adjustedSorts = adjustSorts(orders, buckets);
        // Because of the way adjustSorts works we know that each FieldComparator has a single
        // field name. For this reason we can just look at the leftFieldName
        FieldEqualitor[] fieldEqualitors = new FieldEqualitor[adjustedSorts.length];
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < adjustedSorts.length; i++) {
            FieldComparator fieldComparator = (FieldComparator) adjustedSorts[i];
            fieldEqualitors[i] = new FieldEqualitor(fieldComparator.getLeftFieldName());
            if (i > 0) {
                buf.append(",");
            }
            buf.append(fieldComparator.getLeftFieldName()).append(" ").append(fieldComparator.getOrder().toString());
        }
        sort = buf.toString();
        if (adjustedSorts.length == 1) {
            ecomp = fieldEqualitors[0];
            comp = adjustedSorts[0];
        } else {
            ecomp = new MultipleFieldEqualitor(fieldEqualitors);
            comp = new MultipleFieldComparator(adjustedSorts);
        }
    } else {
        StringBuilder sortBuf = new StringBuilder();
        FieldEqualitor[] equalitors = new FieldEqualitor[buckets.length];
        StreamComparator[] streamComparators = new StreamComparator[buckets.length];
        for (int i = 0; i < buckets.length; i++) {
            equalitors[i] = new FieldEqualitor(buckets[i].toString());
            streamComparators[i] = new FieldComparator(buckets[i].toString(), ComparatorOrder.ASCENDING);
            if (i > 0) {
                sortBuf.append(',');
            }
            sortBuf.append(buckets[i].toString()).append(" asc");
        }
        sort = sortBuf.toString();
        if (equalitors.length == 1) {
            ecomp = equalitors[0];
            comp = streamComparators[0];
        } else {
            ecomp = new MultipleFieldEqualitor(equalitors);
            comp = new MultipleFieldComparator(streamComparators);
        }
    }
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.FL, fl);
    params.set(CommonParams.Q, query);
    params.set(CommonParams.WT, CommonParams.JAVABIN);
    //Always use the /export handler for Distinct Queries because it requires exporting full result sets.
    params.set(CommonParams.QT, "/export");
    if (numWorkers > 1) {
        params.set("partitionKeys", getPartitionKeys(buckets));
    }
    params.set(SORT, sort);
    TupleStream tupleStream = null;
    CloudSolrStream cstream = new CloudSolrStream(zkHost, collection, params);
    tupleStream = new UniqueStream(cstream, ecomp);
    if (numWorkers > 1) {
        // Do the unique in parallel
        // Maintain the sort of the Tuples coming from the workers.
        ParallelStream parallelStream = new ParallelStream(zkHost, collection, tupleStream, numWorkers, comp);
        StreamFactory factory = new StreamFactory().withFunctionName("search", CloudSolrStream.class).withFunctionName("parallel", ParallelStream.class).withFunctionName("unique", UniqueStream.class);
        parallelStream.setStreamFactory(factory);
        tupleStream = parallelStream;
    }
    if (limit != null) {
        tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
    }
    return tupleStream;
}
Also used : StreamComparator(org.apache.solr.client.solrj.io.comp.StreamComparator) MultipleFieldEqualitor(org.apache.solr.client.solrj.io.eq.MultipleFieldEqualitor) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) FieldEqualitor(org.apache.solr.client.solrj.io.eq.FieldEqualitor) MultipleFieldEqualitor(org.apache.solr.client.solrj.io.eq.MultipleFieldEqualitor) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) StreamEqualitor(org.apache.solr.client.solrj.io.eq.StreamEqualitor) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator)

Example 2 with StreamFactory

use of org.apache.solr.client.solrj.io.stream.expr.StreamFactory in project lucene-solr by apache.

the class SolrTable method handleGroupByMapReduce.

private TupleStream handleGroupByMapReduce(String zk, String collection, Properties properties, final List<Map.Entry<String, Class>> fields, final String query, final List<Pair<String, String>> orders, final List<String> _buckets, final List<Pair<String, String>> metricPairs, final String limit, final String havingPredicate) throws IOException {
    Map<String, Class> fmap = new HashMap();
    for (Map.Entry<String, Class> entry : fields) {
        fmap.put(entry.getKey(), entry.getValue());
    }
    int numWorkers = Integer.parseInt(properties.getProperty("numWorkers", "1"));
    Bucket[] buckets = buildBuckets(_buckets, fields);
    Metric[] metrics = buildMetrics(metricPairs, false).toArray(new Metric[0]);
    if (metrics.length == 0) {
        return handleSelectDistinctMapReduce(zk, collection, properties, fields, query, orders, buckets, limit);
    } else {
        for (Metric metric : metrics) {
            Class c = fmap.get(metric.getIdentifier());
            if (Long.class.equals(c)) {
                metric.outputLong = true;
            }
        }
    }
    Set<String> fieldSet = getFieldSet(metrics, fields);
    if (metrics.length == 0) {
        throw new IOException("Group by queries must include atleast one aggregate function.");
    }
    String fl = getFields(fieldSet);
    String sortDirection = getSortDirection(orders);
    String sort = bucketSort(buckets, sortDirection);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.FL, fl);
    params.set(CommonParams.Q, query);
    params.set(CommonParams.WT, CommonParams.JAVABIN);
    //Always use the /export handler for Group By Queries because it requires exporting full result sets.
    params.set(CommonParams.QT, "/export");
    if (numWorkers > 1) {
        params.set("partitionKeys", getPartitionKeys(buckets));
    }
    params.set(SORT, sort);
    TupleStream tupleStream = null;
    CloudSolrStream cstream = new CloudSolrStream(zk, collection, params);
    tupleStream = new RollupStream(cstream, buckets, metrics);
    StreamFactory factory = new StreamFactory().withFunctionName("search", CloudSolrStream.class).withFunctionName("parallel", ParallelStream.class).withFunctionName("rollup", RollupStream.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class).withFunctionName("and", AndEvaluator.class).withFunctionName("or", OrEvaluator.class).withFunctionName("not", NotEvaluator.class).withFunctionName("eq", EqualsEvaluator.class).withFunctionName("gt", GreaterThanEvaluator.class).withFunctionName("lt", LessThanEvaluator.class).withFunctionName("val", RawValueEvaluator.class).withFunctionName("lteq", LessThanEqualToEvaluator.class).withFunctionName("having", HavingStream.class).withFunctionName("gteq", GreaterThanEqualToEvaluator.class);
    if (havingPredicate != null) {
        BooleanEvaluator booleanOperation = (BooleanEvaluator) factory.constructEvaluator(StreamExpressionParser.parse(havingPredicate));
        tupleStream = new HavingStream(tupleStream, booleanOperation);
    }
    if (numWorkers > 1) {
        // Do the rollups in parallel
        // Maintain the sort of the Tuples coming from the workers.
        StreamComparator comp = bucketSortComp(buckets, sortDirection);
        ParallelStream parallelStream = new ParallelStream(zk, collection, tupleStream, numWorkers, comp);
        parallelStream.setStreamFactory(factory);
        tupleStream = parallelStream;
    }
    if (orders != null && orders.size() > 0) {
        if (!sortsEqual(buckets, sortDirection, orders)) {
            int lim = (limit == null) ? 100 : Integer.parseInt(limit);
            StreamComparator comp = getComp(orders);
            //Rank the Tuples
            //If parallel stream is used ALL the Rolled up tuples from the workers will be ranked
            //Providing a true Top or Bottom.
            tupleStream = new RankStream(tupleStream, lim, comp);
        } else {
            // Only need to limit the result, not Rank the result
            if (limit != null) {
                tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
            }
        }
    } else {
        //No order by, check for limit
        if (limit != null) {
            tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
        }
    }
    return tupleStream;
}
Also used : RawValueEvaluator(org.apache.solr.client.solrj.io.eval.RawValueEvaluator) StreamComparator(org.apache.solr.client.solrj.io.comp.StreamComparator) NotEvaluator(org.apache.solr.client.solrj.io.eval.NotEvaluator) GreaterThanEvaluator(org.apache.solr.client.solrj.io.eval.GreaterThanEvaluator) AndEvaluator(org.apache.solr.client.solrj.io.eval.AndEvaluator) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BooleanEvaluator(org.apache.solr.client.solrj.io.eval.BooleanEvaluator) IOException(java.io.IOException) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory)

Example 3 with StreamFactory

use of org.apache.solr.client.solrj.io.stream.expr.StreamFactory in project lucene-solr by apache.

the class TestJavabinTupleStreamParser method testSimple.

public void testSimple() throws IOException {
    List<Map<String, Object>> l = new ArrayList();
    l.add(Utils.makeMap("id", 1, "f", 1.0f, "s", "Some str 1"));
    l.add(Utils.makeMap("id", 2, "f", 2.0f, "s", "Some str 2"));
    l.add(Utils.makeMap("id", 3, "f", 1.0f, "s", "Some str 3"));
    l.add(Utils.makeMap("EOF", true, "RESPONSE_TIME", 206, "sleepMillis", 1000));
    Iterator<Map<String, Object>> iterator = l.iterator();
    TupleStream tupleStream = new TupleStream() {

        @Override
        public void setStreamContext(StreamContext context) {
        }

        @Override
        public List<TupleStream> children() {
            return null;
        }

        @Override
        public void open() throws IOException {
        }

        @Override
        public void close() throws IOException {
        }

        @Override
        public Tuple read() throws IOException {
            if (iterator.hasNext())
                return new Tuple(iterator.next());
            else
                return null;
        }

        @Override
        public StreamComparator getStreamSort() {
            return null;
        }

        @Override
        public Explanation toExplanation(StreamFactory factory) throws IOException {
            return new StreamExplanation(getStreamNodeId().toString()).withFunctionName("Dummy").withImplementingClass(this.getClass().getName()).withExpressionType(Explanation.ExpressionType.STREAM_SOURCE).withExpression("--non-expressible--");
        }
    };
    byte[] bytes = serialize(tupleStream);
    JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), true);
    Map m = parser.next();
    assertEquals(1L, m.get("id"));
    assertEquals(1.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(2L, m.get("id"));
    assertEquals(2.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(3L, m.get("id"));
    assertEquals(1.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(Boolean.TRUE, m.get("EOF"));
    parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);
    m = parser.next();
    assertEquals(1, m.get("id"));
    assertEquals(1.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(2, m.get("id"));
    assertEquals(2.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(3, m.get("id"));
    assertEquals(1.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(Boolean.TRUE, m.get("EOF"));
}
Also used : StreamExplanation(org.apache.solr.client.solrj.io.stream.expr.StreamExplanation) TupleStream(org.apache.solr.client.solrj.io.stream.TupleStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple) JavabinTupleStreamParser(org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser)

Example 4 with StreamFactory

use of org.apache.solr.client.solrj.io.stream.expr.StreamFactory in project lucene-solr by apache.

the class GraphExpressionTest method testGatherNodesFriendsStream.

@Test
public void testGatherNodesFriendsStream() throws Exception {
    new UpdateRequest().add(id, "0", "from_s", "bill", "to_s", "jim", "message_t", "Hello jim").add(id, "1", "from_s", "bill", "to_s", "sam", "message_t", "Hello sam").add(id, "2", "from_s", "bill", "to_s", "max", "message_t", "Hello max").add(id, "3", "from_s", "max", "to_s", "kip", "message_t", "Hello kip").add(id, "4", "from_s", "sam", "to_s", "steve", "message_t", "Hello steve").add(id, "5", "from_s", "jim", "to_s", "ann", "message_t", "Hello steve").commit(cluster.getSolrClient(), COLLECTION);
    List<Tuple> tuples = null;
    GatherNodesStream stream = null;
    StreamContext context = new StreamContext();
    SolrClientCache cache = new SolrClientCache();
    context.setSolrClientCache(cache);
    StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("count", CountMetric.class).withFunctionName("hashJoin", HashJoinStream.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
    String expr = "gatherNodes(collection1, " + "walk=\"bill->from_s\"," + "gather=\"to_s\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 3);
    assertTrue(tuples.get(0).getString("node").equals("jim"));
    assertTrue(tuples.get(1).getString("node").equals("max"));
    assertTrue(tuples.get(2).getString("node").equals("sam"));
    //Test scatter branches, leaves and trackTraversal
    expr = "gatherNodes(collection1, " + "walk=\"bill->from_s\"," + "gather=\"to_s\"," + "scatter=\"branches, leaves\", trackTraversal=\"true\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 4);
    assertTrue(tuples.get(0).getString("node").equals("bill"));
    assertTrue(tuples.get(0).getLong("level").equals(new Long(0)));
    assertTrue(tuples.get(0).getStrings("ancestors").size() == 0);
    assertTrue(tuples.get(1).getString("node").equals("jim"));
    assertTrue(tuples.get(1).getLong("level").equals(new Long(1)));
    List<String> ancestors = tuples.get(1).getStrings("ancestors");
    System.out.println("##################### Ancestors:" + ancestors);
    assert (ancestors.size() == 1);
    assert (ancestors.get(0).equals("bill"));
    assertTrue(tuples.get(2).getString("node").equals("max"));
    assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
    ancestors = tuples.get(2).getStrings("ancestors");
    assert (ancestors.size() == 1);
    assert (ancestors.get(0).equals("bill"));
    assertTrue(tuples.get(3).getString("node").equals("sam"));
    assertTrue(tuples.get(3).getLong("level").equals(new Long(1)));
    ancestors = tuples.get(3).getStrings("ancestors");
    assert (ancestors.size() == 1);
    assert (ancestors.get(0).equals("bill"));
    // Test query root
    expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 3);
    assertTrue(tuples.get(0).getString("node").equals("jim"));
    assertTrue(tuples.get(1).getString("node").equals("max"));
    assertTrue(tuples.get(2).getString("node").equals("sam"));
    // Test query root scatter branches
    expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 4);
    assertTrue(tuples.get(0).getString("node").equals("bill"));
    assertTrue(tuples.get(0).getLong("level").equals(new Long(0)));
    assertTrue(tuples.get(1).getString("node").equals("jim"));
    assertTrue(tuples.get(1).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(2).getString("node").equals("max"));
    assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(3).getString("node").equals("sam"));
    assertTrue(tuples.get(3).getLong("level").equals(new Long(1)));
    expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
    String expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\")";
    stream = (GatherNodesStream) factory.constructStream(expr2);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 3);
    assertTrue(tuples.get(0).getString("node").equals("ann"));
    assertTrue(tuples.get(1).getString("node").equals("kip"));
    assertTrue(tuples.get(2).getString("node").equals("steve"));
    //Test two traversals in the same expression
    String expr3 = "hashJoin(" + expr2 + ", hashed=" + expr2 + ", on=\"node\")";
    HashJoinStream hstream = (HashJoinStream) factory.constructStream(expr3);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    hstream.setStreamContext(context);
    tuples = getTuples(hstream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 3);
    assertTrue(tuples.get(0).getString("node").equals("ann"));
    assertTrue(tuples.get(1).getString("node").equals("kip"));
    assertTrue(tuples.get(2).getString("node").equals("steve"));
    //=================================
    expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\")";
    expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\")";
    stream = (GatherNodesStream) factory.constructStream(expr2);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 7);
    assertTrue(tuples.get(0).getString("node").equals("ann"));
    assertTrue(tuples.get(0).getLong("level").equals(new Long(2)));
    assertTrue(tuples.get(1).getString("node").equals("bill"));
    assertTrue(tuples.get(1).getLong("level").equals(new Long(0)));
    assertTrue(tuples.get(2).getString("node").equals("jim"));
    assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(3).getString("node").equals("kip"));
    assertTrue(tuples.get(3).getLong("level").equals(new Long(2)));
    assertTrue(tuples.get(4).getString("node").equals("max"));
    assertTrue(tuples.get(4).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(5).getString("node").equals("sam"));
    assertTrue(tuples.get(5).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(6).getString("node").equals("steve"));
    assertTrue(tuples.get(6).getLong("level").equals(new Long(2)));
    //Add a cycle from jim to bill
    new UpdateRequest().add(id, "6", "from_s", "jim", "to_s", "bill", "message_t", "Hello steve").add(id, "7", "from_s", "sam", "to_s", "bill", "message_t", "Hello steve").commit(cluster.getSolrClient(), COLLECTION);
    expr = "gatherNodes(collection1, " + "search(collection1, q=\"message_t:jim\", fl=\"from_s\", sort=\"from_s asc\")," + "walk=\"from_s->from_s\"," + "gather=\"to_s\", trackTraversal=\"true\")";
    expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->from_s\"," + "gather=\"to_s\", scatter=\"branches, leaves\", trackTraversal=\"true\")";
    stream = (GatherNodesStream) factory.constructStream(expr2);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 7);
    assertTrue(tuples.get(0).getString("node").equals("ann"));
    assertTrue(tuples.get(0).getLong("level").equals(new Long(2)));
    //Bill should now have one ancestor
    assertTrue(tuples.get(1).getString("node").equals("bill"));
    assertTrue(tuples.get(1).getLong("level").equals(new Long(0)));
    assertTrue(tuples.get(1).getStrings("ancestors").size() == 2);
    List<String> anc = tuples.get(1).getStrings("ancestors");
    Collections.sort(anc);
    assertTrue(anc.get(0).equals("jim"));
    assertTrue(anc.get(1).equals("sam"));
    assertTrue(tuples.get(2).getString("node").equals("jim"));
    assertTrue(tuples.get(2).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(3).getString("node").equals("kip"));
    assertTrue(tuples.get(3).getLong("level").equals(new Long(2)));
    assertTrue(tuples.get(4).getString("node").equals("max"));
    assertTrue(tuples.get(4).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(5).getString("node").equals("sam"));
    assertTrue(tuples.get(5).getLong("level").equals(new Long(1)));
    assertTrue(tuples.get(6).getString("node").equals("steve"));
    assertTrue(tuples.get(6).getLong("level").equals(new Long(2)));
    cache.close();
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) CountMetric(org.apache.solr.client.solrj.io.stream.metrics.CountMetric) MinMetric(org.apache.solr.client.solrj.io.stream.metrics.MinMetric) 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) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) Tuple(org.apache.solr.client.solrj.io.Tuple) HashJoinStream(org.apache.solr.client.solrj.io.stream.HashJoinStream) Test(org.junit.Test)

Example 5 with StreamFactory

use of org.apache.solr.client.solrj.io.stream.expr.StreamFactory in project lucene-solr by apache.

the class GraphExpressionTest method testGatherNodesStream.

@Test
public void testGatherNodesStream() throws Exception {
    new UpdateRequest().add(id, "0", "basket_s", "basket1", "product_s", "product1", "price_f", "20").add(id, "1", "basket_s", "basket1", "product_s", "product3", "price_f", "30").add(id, "2", "basket_s", "basket1", "product_s", "product5", "price_f", "1").add(id, "3", "basket_s", "basket2", "product_s", "product1", "price_f", "2").add(id, "4", "basket_s", "basket2", "product_s", "product6", "price_f", "5").add(id, "5", "basket_s", "basket2", "product_s", "product7", "price_f", "10").add(id, "6", "basket_s", "basket3", "product_s", "product4", "price_f", "20").add(id, "7", "basket_s", "basket3", "product_s", "product3", "price_f", "10").add(id, "8", "basket_s", "basket3", "product_s", "product1", "price_f", "10").add(id, "9", "basket_s", "basket4", "product_s", "product4", "price_f", "40").add(id, "10", "basket_s", "basket4", "product_s", "product3", "price_f", "10").add(id, "11", "basket_s", "basket4", "product_s", "product1", "price_f", "10").commit(cluster.getSolrClient(), COLLECTION);
    List<Tuple> tuples = null;
    Set<String> paths = null;
    GatherNodesStream stream = null;
    StreamContext context = new StreamContext();
    SolrClientCache cache = new SolrClientCache();
    context.setSolrClientCache(cache);
    StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("nodes", GatherNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("count", CountMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
    String expr = "nodes(collection1, " + "walk=\"product1->product_s\"," + "gather=\"basket_s\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 4);
    assertTrue(tuples.get(0).getString("node").equals("basket1"));
    assertTrue(tuples.get(1).getString("node").equals("basket2"));
    assertTrue(tuples.get(2).getString("node").equals("basket3"));
    assertTrue(tuples.get(3).getString("node").equals("basket4"));
    //Test maxDocFreq param
    String docFreqExpr = "gatherNodes(collection1, " + "walk=\"product1, product7->product_s\"," + "maxDocFreq=\"2\"," + "gather=\"basket_s\")";
    stream = (GatherNodesStream) factory.constructStream(docFreqExpr);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 1);
    assertTrue(tuples.get(0).getString("node").equals("basket2"));
    String expr2 = "gatherNodes(collection1, " + expr + "," + "walk=\"node->basket_s\"," + "gather=\"product_s\", count(*), avg(price_f), sum(price_f), min(price_f), max(price_f))";
    stream = (GatherNodesStream) factory.constructStream(expr2);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 5);
    assertTrue(tuples.get(0).getString("node").equals("product3"));
    assertTrue(tuples.get(0).getDouble("count(*)").equals(3.0D));
    assertTrue(tuples.get(1).getString("node").equals("product4"));
    assertTrue(tuples.get(1).getDouble("count(*)").equals(2.0D));
    assertTrue(tuples.get(1).getDouble("avg(price_f)").equals(30.0D));
    assertTrue(tuples.get(1).getDouble("sum(price_f)").equals(60.0D));
    assertTrue(tuples.get(1).getDouble("min(price_f)").equals(20.0D));
    assertTrue(tuples.get(1).getDouble("max(price_f)").equals(40.0D));
    assertTrue(tuples.get(2).getString("node").equals("product5"));
    assertTrue(tuples.get(2).getDouble("count(*)").equals(1.0D));
    assertTrue(tuples.get(3).getString("node").equals("product6"));
    assertTrue(tuples.get(3).getDouble("count(*)").equals(1.0D));
    assertTrue(tuples.get(4).getString("node").equals("product7"));
    assertTrue(tuples.get(4).getDouble("count(*)").equals(1.0D));
    //Test list of root nodes
    expr = "gatherNodes(collection1, " + "walk=\"product4, product7->product_s\"," + "gather=\"basket_s\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 3);
    assertTrue(tuples.get(0).getString("node").equals("basket2"));
    assertTrue(tuples.get(1).getString("node").equals("basket3"));
    assertTrue(tuples.get(2).getString("node").equals("basket4"));
    //Test with negative filter query
    expr = "gatherNodes(collection1, " + "walk=\"product4, product7->product_s\"," + "gather=\"basket_s\", fq=\"-basket_s:basket4\")";
    stream = (GatherNodesStream) factory.constructStream(expr);
    context = new StreamContext();
    context.setSolrClientCache(cache);
    stream.setStreamContext(context);
    tuples = getTuples(stream);
    Collections.sort(tuples, new FieldComparator("node", ComparatorOrder.ASCENDING));
    assertTrue(tuples.size() == 2);
    assertTrue(tuples.get(0).getString("node").equals("basket2"));
    assertTrue(tuples.get(1).getString("node").equals("basket3"));
    cache.close();
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) MinMetric(org.apache.solr.client.solrj.io.stream.metrics.MinMetric) MeanMetric(org.apache.solr.client.solrj.io.stream.metrics.MeanMetric) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) CloudSolrStream(org.apache.solr.client.solrj.io.stream.CloudSolrStream) 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)

Aggregations

StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)69 Tuple (org.apache.solr.client.solrj.io.Tuple)65 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)65 Test (org.junit.Test)64 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)61 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)37 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)14 MeanMetric (org.apache.solr.client.solrj.io.stream.metrics.MeanMetric)10 FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)9 MinMetric (org.apache.solr.client.solrj.io.stream.metrics.MinMetric)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 StreamContext (org.apache.solr.client.solrj.io.stream.StreamContext)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Connection (java.sql.Connection)4 Statement (java.sql.Statement)4 List (java.util.List)4 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)4 AndEvaluator (org.apache.solr.client.solrj.io.eval.AndEvaluator)3