Search in sources :

Example 1 with QueryParser

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;
}
Also used : QueryParser(datawave.query.language.parser.QueryParser) QueryNode(datawave.query.language.tree.QueryNode)

Example 2 with QueryParser

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);
}
Also used : QueryImpl(datawave.webservice.query.QueryImpl) QueryParser(datawave.query.language.parser.QueryParser) LuceneToJexlQueryParser(datawave.query.language.parser.jexl.LuceneToJexlQueryParser) Query(datawave.webservice.query.Query) HashMap(java.util.HashMap) LuceneToJexlQueryParser(datawave.query.language.parser.jexl.LuceneToJexlQueryParser) Test(org.junit.Test)

Aggregations

QueryParser (datawave.query.language.parser.QueryParser)2 LuceneToJexlQueryParser (datawave.query.language.parser.jexl.LuceneToJexlQueryParser)1 QueryNode (datawave.query.language.tree.QueryNode)1 Query (datawave.webservice.query.Query)1 QueryImpl (datawave.webservice.query.QueryImpl)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1