Search in sources :

Example 6 with StreamOperation

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

the class OperationsTest method replaceValueNullWithString.

@Test
public void replaceValueNullWithString() throws Exception {
    Tuple tuple;
    StreamOperation operation;
    operation = new ReplaceOperation("fieldA", StreamExpressionParser.parse("replace(null, withValue=foo)"), factory);
    // replace
    values.clear();
    values.put("fieldB", "bar");
    values.put("fieldC", 123);
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("foo", tuple.get("fieldA"));
    // don't replace
    values.clear();
    values.put("fieldA", "exists");
    values.put("fieldB", "bar");
    values.put("fieldC", 123);
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("exists", tuple.get("fieldA"));
}
Also used : ReplaceOperation(org.apache.solr.client.solrj.io.ops.ReplaceOperation) StreamOperation(org.apache.solr.client.solrj.io.ops.StreamOperation) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 7 with StreamOperation

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

the class ConcatOperationTest method concatMultipleFields.

@Test
public void concatMultipleFields() throws Exception {
    Tuple tuple;
    StreamOperation operation;
    operation = new ConcatOperation(new String[] { "fieldA", "fieldB" }, "fieldABConcat", "-");
    values.clear();
    values.put("fieldA", "bar");
    values.put("fieldB", "baz");
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("bar", tuple.get("fieldA"));
    Assert.assertNotNull(tuple.get("fieldB"));
    Assert.assertEquals("baz", tuple.get("fieldB"));
    Assert.assertNotNull(tuple.get("fieldABConcat"));
    Assert.assertEquals("bar-baz", tuple.get("fieldABConcat"));
    // do the same in oposite order
    operation = new ConcatOperation(new String[] { "fieldB", "fieldA" }, "fieldABConcat", "-");
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("bar", tuple.get("fieldA"));
    Assert.assertNotNull(tuple.get("fieldB"));
    Assert.assertEquals("baz", tuple.get("fieldB"));
    Assert.assertNotNull(tuple.get("fieldABConcat"));
    Assert.assertEquals("baz-bar", tuple.get("fieldABConcat"));
}
Also used : ConcatOperation(org.apache.solr.client.solrj.io.ops.ConcatOperation) StreamOperation(org.apache.solr.client.solrj.io.ops.StreamOperation) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 8 with StreamOperation

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

the class ConcatOperationTest method concatSingleFieldExpression.

///////////////////////////
@Test
public void concatSingleFieldExpression() throws Exception {
    Tuple tuple;
    StreamOperation operation;
    operation = new ConcatOperation(StreamExpressionParser.parse("concat(fields=\"fieldA\", as=\"fieldAConcat\", delim=\"-\")"), factory);
    values.clear();
    values.put("fieldA", "bar");
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("bar", tuple.get("fieldA"));
    Assert.assertNotNull(tuple.get("fieldAConcat"));
    Assert.assertEquals("bar", tuple.get("fieldAConcat"));
}
Also used : ConcatOperation(org.apache.solr.client.solrj.io.ops.ConcatOperation) StreamOperation(org.apache.solr.client.solrj.io.ops.StreamOperation) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Example 9 with StreamOperation

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

the class SelectStream method read.

public Tuple read() throws IOException {
    Tuple original = stream.read();
    if (original.EOF) {
        return original;
    }
    // create a copy with the limited set of fields
    Tuple workingToReturn = new Tuple(new HashMap<>());
    Tuple workingForEvaluators = new Tuple(new HashMap<>());
    //Clear the TupleContext before running the evaluators.
    //The TupleContext allows evaluators to cache values within the scope of a single tuple.
    //For example a LocalDateTime could be parsed by one evaluator and used by other evaluators within the scope of the tuple.
    //This avoids the need to create multiple LocalDateTime instances for the same tuple to satisfy a select expression.
    streamContext.getTupleContext().clear();
    for (Object fieldName : original.fields.keySet()) {
        workingForEvaluators.put(fieldName, original.get(fieldName));
        if (selectedFields.containsKey(fieldName)) {
            workingToReturn.put(selectedFields.get(fieldName), original.get(fieldName));
        }
    }
    // apply all operations
    for (StreamOperation operation : operations) {
        operation.operate(workingToReturn);
        operation.operate(workingForEvaluators);
    }
    // Apply all evaluators
    for (Map.Entry<StreamEvaluator, String> selectedEvaluator : selectedEvaluators.entrySet()) {
        workingToReturn.put(selectedEvaluator.getValue(), selectedEvaluator.getKey().evaluate(workingForEvaluators));
    }
    return workingToReturn;
}
Also used : StreamOperation(org.apache.solr.client.solrj.io.ops.StreamOperation) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple) StreamEvaluator(org.apache.solr.client.solrj.io.eval.StreamEvaluator)

Example 10 with StreamOperation

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

the class ConcatOperationTest method concatWithNullValues.

@Test
public void concatWithNullValues() throws Exception {
    Tuple tuple;
    StreamOperation operation;
    operation = new ConcatOperation(new String[] { "fieldA", "fieldB" }, "fieldABConcat", "-");
    values.clear();
    values.put("fieldA", "bar");
    tuple = new Tuple(values);
    operation.operate(tuple);
    Assert.assertNotNull(tuple.get("fieldA"));
    Assert.assertEquals("bar", tuple.get("fieldA"));
    Assert.assertNull(tuple.get("fieldB"));
    Assert.assertNotNull(tuple.get("fieldABConcat"));
    Assert.assertEquals("bar-null", tuple.get("fieldABConcat"));
}
Also used : ConcatOperation(org.apache.solr.client.solrj.io.ops.ConcatOperation) StreamOperation(org.apache.solr.client.solrj.io.ops.StreamOperation) Tuple(org.apache.solr.client.solrj.io.Tuple) Test(org.junit.Test)

Aggregations

Tuple (org.apache.solr.client.solrj.io.Tuple)16 StreamOperation (org.apache.solr.client.solrj.io.ops.StreamOperation)16 Test (org.junit.Test)15 ConcatOperation (org.apache.solr.client.solrj.io.ops.ConcatOperation)8 ReplaceOperation (org.apache.solr.client.solrj.io.ops.ReplaceOperation)7 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StreamEvaluator (org.apache.solr.client.solrj.io.eval.StreamEvaluator)1