use of datawave.query.language.parser.QueryParser in project datawave by NationalSecurityAgency.
the class ShardQueryLogic method getJexlQueryString.
public String getJexlQueryString(Query settings) throws ParseException {
// queryString should be JEXl after all query parsers are applied
String queryString;
String originalQuery = settings.getQuery();
originalQuery = this.expandQueryMacros(originalQuery);
// Determine query syntax (i.e. JEXL, LUCENE, etc.)
String querySyntax = settings.findParameter(QueryParameters.QUERY_SYNTAX).getParameterValue();
// enforce mandatoryQuerySyntax if set
if (null != this.mandatoryQuerySyntax) {
if (org.apache.commons.lang.StringUtils.isEmpty(querySyntax)) {
throw new IllegalStateException("Must specify one of the following syntax options: " + this.mandatoryQuerySyntax);
} else {
if (!this.mandatoryQuerySyntax.contains(querySyntax)) {
throw new IllegalStateException("Syntax not supported, must be one of the following: " + this.mandatoryQuerySyntax + ", submitted: " + querySyntax);
}
}
}
QueryParser querySyntaxParser = getParser();
if (org.apache.commons.lang.StringUtils.isBlank(querySyntax)) {
// Falling back to Jexl when one is not set on this class
if (null == querySyntaxParser) {
querySyntax = "JEXL";
}
} else if (!"JEXL".equals(querySyntax)) {
if (null == querySyntaxParsers) {
throw new IllegalStateException("Query syntax parsers not configured");
}
querySyntaxParser = querySyntaxParsers.get(querySyntax);
if (null == querySyntaxParser) {
// No parser was specified, try to default to the parser on the
// class
querySyntaxParser = getParser();
if (null == querySyntaxParser) {
throw new IllegalArgumentException("QueryParser not configured for syntax: " + querySyntax);
}
}
}
if (null == originalQuery) {
throw new IllegalArgumentException("Query cannot be null");
} else {
if ("JEXL".equals(querySyntax)) {
queryString = originalQuery;
} else {
QueryNode node = querySyntaxParser.parse(originalQuery);
queryString = node.getOriginalQuery();
if (log.isTraceEnabled()) {
log.trace("luceneQueryString: " + originalQuery + " --> jexlQueryString: " + queryString);
}
}
}
return queryString;
}
use of datawave.query.language.parser.QueryParser in project datawave by NationalSecurityAgency.
the class DefaultEdgeEventQueryLogicTest method testParseWithLucene.
@Test
public void testParseWithLucene() throws Exception {
Query query = new QueryImpl();
Map<String, QueryParser> parsers = new HashMap<>();
parsers.put("LUCENE", new LuceneToJexlQueryParser());
logic.setQuerySyntaxParsers(parsers);
query.setQuery("SOURCE:sourceValue SINK:targetValue TYPE:TEST1 RELATION:REL1-REL2 ATTRIBUTE1:SOURCE1-SOURCE2");
query.addParameter(QueryParameters.QUERY_SYNTAX, "LUCENE");
String transformed = logic.getEventQuery(query);
assertEquals("(SOURCEFIELD == 'sourceValue' AND TARGETFIELD == 'targetValue' AND ENRICHFIELD == 'enrichValue')", transformed);
}
Aggregations