Search in sources :

Example 1 with DatawaveJexlContext

use of datawave.query.jexl.DatawaveJexlContext in project datawave by NationalSecurityAgency.

the class JexlContextCreator method apply.

@Override
public Tuple3<Key, Document, DatawaveJexlContext> apply(Tuple3<Key, Document, Map<String, Object>> from) {
    final DatawaveJexlContext context = this.newDatawaveJexlContext(from);
    // We can only recurse over Documents to add them into the DatawaveJexlContext because
    // we need to have fielded values to place them into the Map.
    from.second().visit(variables, context);
    // absorb the supplied map into the context
    for (Entry<String, Object> entry : from.third().entrySet()) {
        if (context.has(entry.getKey())) {
            throw new IllegalStateException("Conflict when merging Jexl contexts!");
        } else {
            context.set(entry.getKey(), entry.getValue());
        }
    }
    // add any additional entries
    for (Entry<String, Object> entry : additionalEntries.entrySet()) {
        if (context.has(entry.getKey())) {
            throw new IllegalStateException("Conflict when merging Jexl contexts!");
        } else {
            context.set(entry.getKey(), entry.getValue());
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("Constructed context from index and attribute Documents: " + context);
    }
    return Tuples.tuple(from.first(), from.second(), context);
}
Also used : DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext)

Example 2 with DatawaveJexlContext

use of datawave.query.jexl.DatawaveJexlContext in project datawave by NationalSecurityAgency.

the class QueryPruningVisitorTest method falseDoubleAndRewriteTest.

@Test
public void falseDoubleAndRewriteTest() throws ParseException {
    String query = "FIELD1 == 'x' && _NOFIELD_ == 'y' && FIELD2 == 'y'";
    ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);
    JexlNode reduced = QueryPruningVisitor.reduce(script, true);
    JexlEvaluation jexlEvaluation = new JexlEvaluation(JexlStringBuildingVisitor.buildQuery(reduced), new DefaultArithmetic());
    boolean jexlState = jexlEvaluation.apply(new Tuple3<>(new Key(), new Document(), new DatawaveJexlContext()));
    Assert.assertFalse(jexlState);
    Assert.assertEquals("false", JexlStringBuildingVisitor.buildQuery(reduced));
    Assert.assertEquals("false", JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));
    Assert.assertTrue(logAppender.getMessages().size() == 2);
    Assert.assertEquals("Pruning FIELD1 == 'x' && _NOFIELD_ == 'y' && FIELD2 == 'y' to false", logAppender.getMessages().get(0));
    Assert.assertEquals("Query before prune: FIELD1 == 'x' && _NOFIELD_ == 'y' && FIELD2 == 'y'\nQuery after prune: false", logAppender.getMessages().get(1));
}
Also used : DefaultArithmetic(datawave.query.jexl.DefaultArithmetic) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlEvaluation(datawave.query.function.JexlEvaluation) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Document(datawave.query.attributes.Document) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 3 with DatawaveJexlContext

use of datawave.query.jexl.DatawaveJexlContext in project datawave by NationalSecurityAgency.

the class JexlEvaluationTest method testRegexUnion.

@Test
public void testRegexUnion() {
    String query = "FOO == 'bar' || FOO =~ 'baz.*'";
    Document d = new Document();
    d.put("FOO", new Content("bar", new Key("shard", "datatype\0uid"), true));
    d.put("FOO", new Content("bazaar", new Key("shard", "datatype\0uid"), true));
    JexlEvaluation evaluation = new JexlEvaluation(query);
    DatawaveJexlContext context = new DatawaveJexlContext();
    d.visit(Collections.singleton("FOO"), context);
    boolean result = evaluation.apply(new Tuple3<>(new Key("shard", "datatype\0uid"), d, context));
    assertTrue(result);
}
Also used : Content(datawave.query.attributes.Content) Document(datawave.query.attributes.Document) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 4 with DatawaveJexlContext

use of datawave.query.jexl.DatawaveJexlContext in project datawave by NationalSecurityAgency.

the class JexlEvaluationTest method testContentPhraseFunction.

@Test
public void testContentPhraseFunction() {
    String query = "FOO == 'bar' && TOKFIELD == 'big' && TOKFIELD == 'red' && TOKFIELD == 'dog' && content:phrase(termOffsetMap, 'big', 'red', 'dog')";
    Map<String, TermFrequencyList> map = new HashMap<>();
    map.put("big", buildTfList("TOKFIELD", 1));
    map.put("red", buildTfList("TOKFIELD", 2));
    map.put("dog", buildTfList("TOKFIELD", 3));
    DatawaveJexlContext context = new DatawaveJexlContext();
    context.set(Constants.TERM_OFFSET_MAP_JEXL_VARIABLE_NAME, map);
    Key docKey = new Key("shard", "datatype\0uid");
    Document d = new Document();
    d.put("FOO", new Content("bar", docKey, true));
    d.put("TOKFIELD", new Content("big", docKey, true));
    d.put("TOKFIELD", new Content("red", docKey, true));
    d.put("TOKFIELD", new Content("dog", docKey, true));
    d.visit(Arrays.asList("FOO", "TOKFIELD"), context);
    JexlEvaluation evaluation = new JexlEvaluation(query, new HitListArithmetic());
    Tuple3<Key, Document, DatawaveJexlContext> tuple = new Tuple3<>(docKey, d, context);
    boolean result = evaluation.apply(tuple);
    assertTrue(result);
    // assert that "big red dog" came back in the hit terms
    boolean foundPhrase = false;
    Attributes attrs = (Attributes) d.get("HIT_TERM");
    for (Attribute<?> attr : attrs.getAttributes()) {
        if (attr.getData().equals("TOKFIELD:big red dog")) {
            foundPhrase = true;
        }
    }
    assertEquals(5, attrs.size());
    assertTrue(foundPhrase);
}
Also used : TermFrequencyList(datawave.query.jexl.functions.TermFrequencyList) HitListArithmetic(datawave.query.jexl.HitListArithmetic) HashMap(java.util.HashMap) Attributes(datawave.query.attributes.Attributes) Document(datawave.query.attributes.Document) Content(datawave.query.attributes.Content) Tuple3(datawave.query.util.Tuple3) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 5 with DatawaveJexlContext

use of datawave.query.jexl.DatawaveJexlContext in project datawave by NationalSecurityAgency.

the class JexlEvaluationTest method testRegexIntersection.

@Test
public void testRegexIntersection() {
    String query = "FOO == 'bar' && FOO =~ 'baz.*'";
    Document d = new Document();
    d.put("FOO", new Content("bar", new Key("shard", "datatype\0uid"), true));
    d.put("FOO", new Content("bazaar", new Key("shard", "datatype\0uid"), true));
    JexlEvaluation evaluation = new JexlEvaluation(query);
    DatawaveJexlContext context = new DatawaveJexlContext();
    d.visit(Collections.singleton("FOO"), context);
    boolean result = evaluation.apply(new Tuple3<>(new Key("shard", "datatype\0uid"), d, context));
    assertTrue(result);
}
Also used : Content(datawave.query.attributes.Content) Document(datawave.query.attributes.Document) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Aggregations

DatawaveJexlContext (datawave.query.jexl.DatawaveJexlContext)11 Key (org.apache.accumulo.core.data.Key)10 Document (datawave.query.attributes.Document)8 Test (org.junit.Test)7 Content (datawave.query.attributes.Content)5 JexlEvaluation (datawave.query.function.JexlEvaluation)3 DefaultArithmetic (datawave.query.jexl.DefaultArithmetic)3 Tuple3 (datawave.query.util.Tuple3)3 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)3 JexlNode (org.apache.commons.jexl2.parser.JexlNode)3 HitListArithmetic (datawave.query.jexl.HitListArithmetic)2 Attributes (datawave.query.attributes.Attributes)1 IndexOnlyFunctionIterator (datawave.query.iterator.IndexOnlyFunctionIterator)1 DelayedNonEventIndexContext (datawave.query.jexl.DelayedNonEventIndexContext)1 TermFrequencyList (datawave.query.jexl.functions.TermFrequencyList)1 HashMap (java.util.HashMap)1 Range (org.apache.accumulo.core.data.Range)1