SOLR-8925: Integrate the GraphTermsQuery into the gatherNodes expression

This commit is contained in:
jbernste 2016-04-26 14:25:35 -04:00
parent d66f5515e6
commit 9ce830d8f2
4 changed files with 50 additions and 14 deletions

View File

@ -133,7 +133,7 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
collectTermContext(reader, contexts, termContexts, this.queryTerms); collectTermContext(reader, contexts, termContexts, this.queryTerms);
for(int i=0; i<termContexts.length; i++) { for(int i=0; i<termContexts.length; i++) {
TermContext termContext = termContexts[i]; TermContext termContext = termContexts[i];
if(termContext != null && termContext.docFreq() < this.maxDocFreq) { if(termContext != null && termContext.docFreq() <= this.maxDocFreq) {
this.finalContexts.add(termContext); this.finalContexts.add(termContext);
this.finalTerms.add(queryTerms[i]); this.finalTerms.add(queryTerms[i]);
} }

View File

@ -125,7 +125,7 @@ public class TestGraphTermsQParserPlugin extends SolrTestCaseJ4 {
//Test with int field //Test with int field
params = new ModifiableSolrParams(); params = new ModifiableSolrParams();
params.add("q", "{!graphTerms f=test_ti maxDocFreq=3}5,10"); params.add("q", "{!graphTerms f=test_ti maxDocFreq=2}5,10");
params.add("sort", "id asc"); params.add("sort", "id asc");
assertQ(req(params, "indent", "on"), "*[count(//doc)=2]", assertQ(req(params, "indent", "on"), "*[count(//doc)=2]",
"//result/doc[1]/float[@name='id'][.='6.0']", "//result/doc[1]/float[@name='id'][.='6.0']",

View File

@ -66,6 +66,7 @@ public class GatherNodesStream extends TupleStream implements Expressible {
private Iterator<Tuple> out; private Iterator<Tuple> out;
private Traversal traversal; private Traversal traversal;
private List<Metric> metrics; private List<Metric> metrics;
private int maxDocFreq;
public GatherNodesStream(String zkHost, public GatherNodesStream(String zkHost,
String collection, String collection,
@ -76,7 +77,8 @@ public class GatherNodesStream extends TupleStream implements Expressible {
Map queryParams, Map queryParams,
List<Metric> metrics, List<Metric> metrics,
boolean trackTraversal, boolean trackTraversal,
Set<Traversal.Scatter> scatter) { Set<Traversal.Scatter> scatter,
int maxDocFreq) {
init(zkHost, init(zkHost,
collection, collection,
@ -87,7 +89,8 @@ public class GatherNodesStream extends TupleStream implements Expressible {
queryParams, queryParams,
metrics, metrics,
trackTraversal, trackTraversal,
scatter); scatter,
maxDocFreq);
} }
public GatherNodesStream(StreamExpression expression, StreamFactory factory) throws IOException { public GatherNodesStream(StreamExpression expression, StreamFactory factory) throws IOException {
@ -185,10 +188,11 @@ public class GatherNodesStream extends TupleStream implements Expressible {
useDefaultTraversal = true; useDefaultTraversal = true;
} }
StreamExpressionNamedParameter scopeExpression = factory.getNamedOperand(expression, "localScope"); StreamExpressionNamedParameter docFreqExpression = factory.getNamedOperand(expression, "maxDocFreq");
int docFreq = -1;
if(trackExpression != null) { if(docFreqExpression != null) {
trackTraversal = Boolean.parseBoolean(((StreamExpressionValue) trackExpression.getParameter()).getValue()); docFreq = Integer.parseInt(((StreamExpressionValue) docFreqExpression.getParameter()).getValue());
} }
Map<String,String> params = new HashMap<String,String>(); Map<String,String> params = new HashMap<String,String>();
@ -197,6 +201,7 @@ public class GatherNodesStream extends TupleStream implements Expressible {
!namedParam.getName().equals("gather") && !namedParam.getName().equals("gather") &&
!namedParam.getName().equals("walk") && !namedParam.getName().equals("walk") &&
!namedParam.getName().equals("scatter") && !namedParam.getName().equals("scatter") &&
!namedParam.getName().equals("maxDocFreq") &&
!namedParam.getName().equals("trackTraversal")) !namedParam.getName().equals("trackTraversal"))
{ {
params.put(namedParam.getName(), namedParam.getParameter().toString().trim()); params.put(namedParam.getName(), namedParam.getParameter().toString().trim());
@ -228,7 +233,8 @@ public class GatherNodesStream extends TupleStream implements Expressible {
params, params,
metrics, metrics,
trackTraversal, trackTraversal,
scatter); scatter,
docFreq);
} }
private void init(String zkHost, private void init(String zkHost,
@ -240,7 +246,8 @@ public class GatherNodesStream extends TupleStream implements Expressible {
Map queryParams, Map queryParams,
List<Metric> metrics, List<Metric> metrics,
boolean trackTraversal, boolean trackTraversal,
Set<Traversal.Scatter> scatter) { Set<Traversal.Scatter> scatter,
int maxDocFreq) {
this.zkHost = zkHost; this.zkHost = zkHost;
this.collection = collection; this.collection = collection;
this.tupleStream = tupleStream; this.tupleStream = tupleStream;
@ -251,6 +258,7 @@ public class GatherNodesStream extends TupleStream implements Expressible {
this.metrics = metrics; this.metrics = metrics;
this.trackTraversal = trackTraversal; this.trackTraversal = trackTraversal;
this.scatter = scatter; this.scatter = scatter;
this.maxDocFreq = maxDocFreq;
} }
@Override @Override
@ -298,6 +306,9 @@ public class GatherNodesStream extends TupleStream implements Expressible {
expression.addParameter(new StreamExpressionNamedParameter("zkHost", zkHost)); expression.addParameter(new StreamExpressionNamedParameter("zkHost", zkHost));
expression.addParameter(new StreamExpressionNamedParameter("gather", zkHost)); expression.addParameter(new StreamExpressionNamedParameter("gather", zkHost));
if(maxDocFreq > -1) {
expression.addParameter(new StreamExpressionNamedParameter("maxDocFreq", Integer.toString(maxDocFreq)));
}
expression.addParameter(new StreamExpressionNamedParameter("walk", traverseFrom+"->"+traverseTo)); expression.addParameter(new StreamExpressionNamedParameter("walk", traverseFrom+"->"+traverseTo));
expression.addParameter(new StreamExpressionNamedParameter("trackTraversal", Boolean.toString(trackTraversal))); expression.addParameter(new StreamExpressionNamedParameter("trackTraversal", Boolean.toString(trackTraversal)));
@ -410,7 +421,7 @@ public class GatherNodesStream extends TupleStream implements Expressible {
} }
if(queryParams.containsKey("fl")) { if(queryParams.containsKey("fl")) {
String flString = (String)queryParams.get("fl"); String flString = queryParams.get("fl");
String[] flArray = flString.split(","); String[] flArray = flString.split(",");
for(String f : flArray) { for(String f : flArray) {
flSet.add(f.trim()); flSet.add(f.trim());
@ -433,14 +444,22 @@ public class GatherNodesStream extends TupleStream implements Expressible {
StringBuffer nodeQuery = new StringBuffer(); StringBuffer nodeQuery = new StringBuffer();
boolean comma = false;
for(String node : nodes) { for(String node : nodes) {
nodeQuery.append(node).append(" "); if(comma) {
nodeQuery.append(",");
}
nodeQuery.append(node);
comma = true;
} }
String q = traverseTo + ":(" + nodeQuery.toString().trim() + ")"; if(maxDocFreq > -1) {
String docFreqParam = " maxDocFreq="+maxDocFreq;
joinParams.put("q", "{!graphTerms f=" + traverseTo + docFreqParam + "}" + nodeQuery.toString());
} else {
joinParams.put("q", "{!terms f=" + traverseTo+"}" + nodeQuery.toString());
}
joinParams.put("q", q);
TupleStream stream = null; TupleStream stream = null;
try { try {
stream = new UniqueStream(new CloudSolrStream(zkHost, collection, joinParams), new MultipleFieldEqualitor(new FieldEqualitor(gather), new FieldEqualitor(traverseTo))); stream = new UniqueStream(new CloudSolrStream(zkHost, collection, joinParams), new MultipleFieldEqualitor(new FieldEqualitor(gather), new FieldEqualitor(traverseTo)));

View File

@ -326,6 +326,23 @@ public class GraphExpressionTest extends AbstractFullDistribZkTestBase {
assertTrue(tuples.get(2).getString("node").equals("basket3")); assertTrue(tuples.get(2).getString("node").equals("basket3"));
assertTrue(tuples.get(3).getString("node").equals("basket4")); 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, " + String expr2 = "gatherNodes(collection1, " +
expr+","+ expr+","+
"walk=\"node->basket_s\"," + "walk=\"node->basket_s\"," +