Search in sources :

Example 1 with JexlSelectorNode

use of datawave.query.language.parser.jexl.JexlSelectorNode in project datawave by NationalSecurityAgency.

the class RegexpQueryNodeBuilder method build.

public JexlNode build(QueryNode queryNode) throws QueryNodeException {
    JexlNode returnNode = null;
    if (queryNode instanceof RegexpQueryNode) {
        RegexpQueryNode regexpQueryNode = (RegexpQueryNode) queryNode;
        String field = regexpQueryNode.getFieldAsString();
        UnescapedCharSequence ecs = (UnescapedCharSequence) regexpQueryNode.getText();
        if (field == null || field.isEmpty()) {
            returnNode = new JexlSelectorNode(JexlSelectorNode.Type.REGEX, "", ecs.toString());
        } else {
            returnNode = new JexlSelectorNode(JexlSelectorNode.Type.REGEX, field, ecs.toString());
        }
    }
    return returnNode;
}
Also used : JexlSelectorNode(datawave.query.language.parser.jexl.JexlSelectorNode) UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) JexlNode(datawave.query.language.parser.jexl.JexlNode) RegexpQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode)

Example 2 with JexlSelectorNode

use of datawave.query.language.parser.jexl.JexlSelectorNode in project datawave by NationalSecurityAgency.

the class FieldQueryNodeBuilder method build.

public JexlNode build(QueryNode queryNode) throws QueryNodeException {
    JexlNode returnNode = null;
    if (queryNode instanceof QuotedFieldQueryNode) {
        List<String> phraseWordList = new ArrayList<>();
        FieldQueryNode quotedFieldNode = (QuotedFieldQueryNode) queryNode;
        String field = quotedFieldNode.getFieldAsString();
        String selector = quotedFieldNode.getTextAsString();
        // check if spaces were escaped
        FieldQueryNode fieldQueryNode = (QuotedFieldQueryNode) queryNode;
        UnescapedCharSequence origChars = (UnescapedCharSequence) fieldQueryNode.getText();
        int nextWordStart = 0;
        ArrayList<String> words = new ArrayList<>();
        for (int x = 0; x < origChars.length(); x++) {
            if (origChars.charAt(x) == ' ' && !origChars.wasEscaped(x)) {
                words.add(selector.substring(nextWordStart, x));
                nextWordStart = x + 1;
            } else if (x == origChars.length() - 1) {
                // last word in the list
                words.add(selector.substring(nextWordStart));
            }
        }
        replaceEscapeCharsWithSpace(words);
        for (String s : words) {
            String currWord = s.trim();
            if (!currWord.isEmpty()) {
                phraseWordList.add(currWord);
            }
        }
        if (phraseWordList.size() == 1) {
            // if only one term in quotes, just use a SelectorNode
            String firstWord = phraseWordList.get(0);
            if (field == null || field.isEmpty()) {
                returnNode = new JexlSelectorNode(JexlSelectorNode.Type.EXACT, "", firstWord);
            } else {
                returnNode = new JexlSelectorNode(JexlSelectorNode.Type.EXACT, field, firstWord);
            }
        } else {
            // if more than one term in quotes, use an AdjNode
            returnNode = new JexlPhraseNode(field, phraseWordList);
        }
    } else if (queryNode instanceof FuzzyQueryNode) {
        throw new UnsupportedOperationException(queryNode.getClass().getName() + " not implemented");
    } else {
        // queryNode instanceof WildcardQueryNode
        // queryNode instanceof QuotedFieldQueryNode
        // queryNode instanceof FieldQueryNode
        FieldQueryNode fieldNode = (FieldQueryNode) queryNode;
        boolean hasUnescapedWildcard = WildcardFieldedTerm.hasUnescapedWildcard(fieldNode, Sets.newHashSet(' ', '/'));
        String field = fieldNode.getFieldAsString();
        String selector;
        JexlSelectorNode.Type type;
        if (hasUnescapedWildcard) {
            // When we have a regular expression, we want to maintain the original escaped characters in the term
            selector = EscapedNodes.getEscapedTerm(fieldNode, new EscapeQuerySyntaxImpl());
            type = JexlSelectorNode.Type.WILDCARDS;
        } else {
            selector = fieldNode.getTextAsString();
            type = JexlSelectorNode.Type.EXACT;
        }
        if (field == null || field.isEmpty()) {
            returnNode = new JexlSelectorNode(type, "", selector);
        } else {
            returnNode = new JexlSelectorNode(type, field, selector);
        }
    }
    return returnNode;
}
Also used : FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) ArrayList(java.util.ArrayList) JexlPhraseNode(datawave.query.language.parser.jexl.JexlPhraseNode) EscapeQuerySyntaxImpl(datawave.query.language.parser.lucene.EscapeQuerySyntaxImpl) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) JexlSelectorNode(datawave.query.language.parser.jexl.JexlSelectorNode) UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) JexlNode(datawave.query.language.parser.jexl.JexlNode)

Example 3 with JexlSelectorNode

use of datawave.query.language.parser.jexl.JexlSelectorNode in project datawave by NationalSecurityAgency.

the class SlopQueryNodeBuilder method build.

public JexlNode build(QueryNode queryNode) throws QueryNodeException {
    JexlNode returnNode = null;
    SlopQueryNode phraseSlopNode = (SlopQueryNode) queryNode;
    JexlNode node = (JexlNode) phraseSlopNode.getChild().getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
    if (node instanceof JexlPhraseNode) {
        JexlPhraseNode phraseNode = (JexlPhraseNode) node;
        returnNode = new JexlWithinNode(phraseNode.getField(), phraseNode.getWordList(), phraseSlopNode.getValue());
    } else if (node instanceof JexlSelectorNode) {
        // if phrase only contained one word, a JexlSelectorNode would be created
        // and then a SlopQueryNode / within makes no sense
        returnNode = node;
    } else {
        throw new UnsupportedOperationException(node.getClass().getName() + " found as a child of a SlopQueryNode -- not implemented");
    }
    return returnNode;
}
Also used : JexlSelectorNode(datawave.query.language.parser.jexl.JexlSelectorNode) SlopQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode) JexlNode(datawave.query.language.parser.jexl.JexlNode) JexlPhraseNode(datawave.query.language.parser.jexl.JexlPhraseNode) JexlWithinNode(datawave.query.language.parser.jexl.JexlWithinNode)

Aggregations

JexlNode (datawave.query.language.parser.jexl.JexlNode)3 JexlSelectorNode (datawave.query.language.parser.jexl.JexlSelectorNode)3 JexlPhraseNode (datawave.query.language.parser.jexl.JexlPhraseNode)2 UnescapedCharSequence (org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence)2 JexlWithinNode (datawave.query.language.parser.jexl.JexlWithinNode)1 EscapeQuerySyntaxImpl (datawave.query.language.parser.lucene.EscapeQuerySyntaxImpl)1 ArrayList (java.util.ArrayList)1 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)1 FuzzyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode)1 QuotedFieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode)1 SlopQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode)1 RegexpQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode)1