use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class GraphExpressionTest 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;
StreamContext context = new StreamContext();
SolrClientCache cache = new SolrClientCache();
context.setSolrClientCache(cache);
StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withFunctionName("shortestPath", ShortestPathStream.class);
Map params = new HashMap();
params.put("fq", "predicate_s:knows");
stream = (ShortestPathStream) factory.constructStream("shortestPath(collection1, " + "from=\"jim\", " + "to=\"steve\"," + "edge=\"from_s=to_s\"," + "fq=\"predicate_s:knows\"," + "threads=\"3\"," + "partitionSize=\"3\"," + "maxDepth=\"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
params.put("fq", "predicate_s:knows");
stream = (ShortestPathStream) factory.constructStream("shortestPath(collection1, " + "from=\"jim\", " + "to=\"steve\"," + "edge=\"from_s=to_s\"," + "fq=\"predicate_s:knows\"," + "threads=\"3\"," + "partitionSize=\"1\"," + "maxDepth=\"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
stream = (ShortestPathStream) factory.constructStream("shortestPath(collection1, " + "from=\"jim\", " + "to=\"steve\"," + "edge=\"from_s=to_s\"," + "fq=\"predicate_s:crap\"," + "threads=\"3\"," + "partitionSize=\"3\"," + "maxDepth=\"6\")");
stream.setStreamContext(context);
paths = new HashSet();
tuples = getTuples(stream);
assertTrue(tuples.size() == 0);
//Test with depth 2
stream = (ShortestPathStream) factory.constructStream("shortestPath(collection1, " + "from=\"jim\", " + "to=\"steve\"," + "edge=\"from_s=to_s\"," + "fq=\"predicate_s:knows\"," + "threads=\"3\"," + "partitionSize=\"3\"," + "maxDepth=\"2\")");
stream.setStreamContext(context);
tuples = getTuples(stream);
assertTrue(tuples.size() == 0);
//Take out alex
params.put("fq", "predicate_s:knows NOT to_s:alex");
stream = (ShortestPathStream) factory.constructStream("shortestPath(collection1, " + "from=\"jim\", " + "to=\"steve\"," + "edge=\"from_s=to_s\"," + "fq=\" predicate_s:knows NOT to_s:alex\"," + "threads=\"3\"," + "partitionSize=\"3\"," + "maxDepth=\"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();
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class GraphExpressionTest method testScoreNodesFacetStream.
@Test
public void testScoreNodesFacetStream() throws Exception {
new UpdateRequest().add(id, "0", "basket_s", "basket1", "product_ss", "product1", "product_ss", "product3", "product_ss", "product5", "price_f", "1").add(id, "3", "basket_s", "basket2", "product_ss", "product1", "product_ss", "product6", "product_ss", "product7", "price_f", "1").add(id, "6", "basket_s", "basket3", "product_ss", "product4", "product_ss", "product3", "product_ss", "product1", "price_f", "1").add(id, "9", "basket_s", "basket4", "product_ss", "product4", "product_ss", "product3", "product_ss", "product1", "price_f", "1").commit(cluster.getSolrClient(), COLLECTION);
List<Tuple> tuples = null;
TupleStream stream = null;
StreamContext context = new StreamContext();
SolrClientCache cache = new SolrClientCache();
context.setSolrClientCache(cache);
StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withDefaultZkHost(cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("scoreNodes", ScoreNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("facet", FacetStream.class).withFunctionName("sort", SortStream.class).withFunctionName("count", CountMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
String expr = "sort(by=\"nodeScore desc\",scoreNodes(facet(collection1, q=\"product_ss:product3\", buckets=\"product_ss\", bucketSorts=\"count(*) desc\", bucketSizeLimit=100, count(*))))";
stream = factory.constructStream(expr);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Tuple tuple = tuples.get(0);
assert (tuple.getString("node").equals("product3"));
assert (tuple.getLong("docFreq") == 3);
assert (tuple.getLong("count(*)") == 3);
Tuple tuple0 = tuples.get(1);
assert (tuple0.getString("node").equals("product4"));
assert (tuple0.getLong("docFreq") == 2);
assert (tuple0.getLong("count(*)") == 2);
Tuple tuple1 = tuples.get(2);
assert (tuple1.getString("node").equals("product1"));
assert (tuple1.getLong("docFreq") == 4);
assert (tuple1.getLong("count(*)") == 3);
Tuple tuple2 = tuples.get(3);
assert (tuple2.getString("node").equals("product5"));
assert (tuple2.getLong("docFreq") == 1);
assert (tuple2.getLong("count(*)") == 1);
cache.close();
}
use of org.apache.solr.client.solrj.io.stream.StreamContext in project lucene-solr by apache.
the class GraphExpressionTest method testScoreNodesStream.
@Test
public void testScoreNodesStream() throws Exception {
new UpdateRequest().add(id, "0", "basket_s", "basket1", "product_s", "product1", "price_f", "1").add(id, "1", "basket_s", "basket1", "product_s", "product3", "price_f", "1").add(id, "2", "basket_s", "basket1", "product_s", "product5", "price_f", "100").add(id, "3", "basket_s", "basket2", "product_s", "product1", "price_f", "1").add(id, "4", "basket_s", "basket2", "product_s", "product6", "price_f", "1").add(id, "5", "basket_s", "basket2", "product_s", "product7", "price_f", "1").add(id, "6", "basket_s", "basket3", "product_s", "product4", "price_f", "1").add(id, "7", "basket_s", "basket3", "product_s", "product3", "price_f", "1").add(id, "8", "basket_s", "basket3", "product_s", "product1", "price_f", "1").add(id, "9", "basket_s", "basket4", "product_s", "product4", "price_f", "1").add(id, "10", "basket_s", "basket4", "product_s", "product3", "price_f", "1").add(id, "11", "basket_s", "basket4", "product_s", "product1", "price_f", "1").add(id, "12", "basket_s", "basket5", "product_s", "product1", "price_f", "1").add(id, "13", "basket_s", "basket6", "product_s", "product1", "price_f", "1").add(id, "14", "basket_s", "basket7", "product_s", "product1", "price_f", "1").add(id, "15", "basket_s", "basket4", "product_s", "product1", "price_f", "1").commit(cluster.getSolrClient(), COLLECTION);
List<Tuple> tuples = null;
TupleStream stream = null;
StreamContext context = new StreamContext();
SolrClientCache cache = new SolrClientCache();
context.setSolrClientCache(cache);
StreamFactory factory = new StreamFactory().withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress()).withDefaultZkHost(cluster.getZkServer().getZkAddress()).withFunctionName("gatherNodes", GatherNodesStream.class).withFunctionName("scoreNodes", ScoreNodesStream.class).withFunctionName("search", CloudSolrStream.class).withFunctionName("sort", SortStream.class).withFunctionName("count", CountMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class);
String expr = "gatherNodes(collection1, " + "walk=\"product3->product_s\"," + "gather=\"basket_s\")";
String expr2 = "sort(by=\"nodeScore desc\", " + "scoreNodes(gatherNodes(collection1, " + expr + "," + "walk=\"node->basket_s\"," + "gather=\"product_s\", " + "count(*), " + "avg(price_f), " + "sum(price_f), " + "min(price_f), " + "max(price_f))))";
stream = factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
Tuple tuple0 = tuples.get(0);
assert (tuple0.getString("node").equals("product4"));
assert (tuple0.getLong("docFreq") == 2);
assert (tuple0.getLong("count(*)") == 2);
Tuple tuple1 = tuples.get(1);
assert (tuple1.getString("node").equals("product1"));
assert (tuple1.getLong("docFreq") == 8);
assert (tuple1.getLong("count(*)") == 3);
Tuple tuple2 = tuples.get(2);
assert (tuple2.getString("node").equals("product5"));
assert (tuple2.getLong("docFreq") == 1);
assert (tuple2.getLong("count(*)") == 1);
//Test using a different termFreq field then the default count(*)
expr2 = "sort(by=\"nodeScore desc\", " + "scoreNodes(termFreq=\"avg(price_f)\",gatherNodes(collection1, " + expr + "," + "walk=\"node->basket_s\"," + "gather=\"product_s\", " + "count(*), " + "avg(price_f), " + "sum(price_f), " + "min(price_f), " + "max(price_f))))";
stream = factory.constructStream(expr2);
context = new StreamContext();
context.setSolrClientCache(cache);
stream.setStreamContext(context);
tuples = getTuples(stream);
tuple0 = tuples.get(0);
assert (tuple0.getString("node").equals("product5"));
assert (tuple0.getLong("docFreq") == 1);
assert (tuple0.getDouble("avg(price_f)") == 100);
tuple1 = tuples.get(1);
assert (tuple1.getString("node").equals("product4"));
assert (tuple1.getLong("docFreq") == 2);
assert (tuple1.getDouble("avg(price_f)") == 1);
tuple2 = tuples.get(2);
assert (tuple2.getString("node").equals("product1"));
assert (tuple2.getLong("docFreq") == 8);
assert (tuple2.getDouble("avg(price_f)") == 1);
cache.close();
}
use of org.apache.solr.client.solrj.io.stream.StreamContext 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();
}
Aggregations