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);
}
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));
}
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);
}
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);
}
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);
}
Aggregations