Search in sources :

Example 21 with QueryNode

use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.

the class BoostQueryNodeBuilder method build.

@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
    BoostQueryNode boostNode = (BoostQueryNode) queryNode;
    QueryNode child = boostNode.getChild();
    if (child == null) {
        return null;
    }
    Query query = (Query) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
    return new BoostQuery(query, boostNode.getValue());
}
Also used : BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 22 with QueryNode

use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.

the class PhraseQueryNodeBuilder method build.

@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
    TokenizedPhraseQueryNode phraseNode = (TokenizedPhraseQueryNode) queryNode;
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    List<QueryNode> children = phraseNode.getChildren();
    if (children != null) {
        for (QueryNode child : children) {
            TermQuery termQuery = (TermQuery) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            FieldQueryNode termNode = (FieldQueryNode) child;
            builder.add(termQuery.getTerm(), termNode.getPositionIncrement());
        }
    }
    return builder.build();
}
Also used : TokenizedPhraseQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TokenizedPhraseQueryNode) TermQuery(org.apache.lucene.search.TermQuery) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) PhraseQuery(org.apache.lucene.search.PhraseQuery) TokenizedPhraseQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TokenizedPhraseQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QueryTreeBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)

Example 23 with QueryNode

use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.

the class QueryNodeOperation method logicalAnd.

/**
   * perform a logical and of 2 QueryNode trees. if q1 and q2 are ANDQueryNode
   * nodes it uses head Node from q1 and adds the children of q2 to q1 if q1 is
   * a AND node and q2 is not, add q2 as a child of the head node of q1 if q2 is
   * a AND node and q1 is not, add q1 as a child of the head node of q2 if q1
   * and q2 are not ANDQueryNode nodes, create a AND node and make q1 and q2
   * children of that node if q1 or q2 is null it returns the not null node if
   * q1 = q2 = null it returns null
   */
public static final QueryNode logicalAnd(QueryNode q1, QueryNode q2) {
    if (q1 == null)
        return q2;
    if (q2 == null)
        return q1;
    ANDOperation op = null;
    if (q1 instanceof AndQueryNode && q2 instanceof AndQueryNode)
        op = ANDOperation.BOTH;
    else if (q1 instanceof AndQueryNode)
        op = ANDOperation.Q1;
    else if (q2 instanceof AndQueryNode)
        op = ANDOperation.Q2;
    else
        op = ANDOperation.NONE;
    try {
        QueryNode result = null;
        switch(op) {
            case NONE:
                List<QueryNode> children = new ArrayList<>();
                children.add(q1.cloneTree());
                children.add(q2.cloneTree());
                result = new AndQueryNode(children);
                return result;
            case Q1:
                result = q1.cloneTree();
                result.add(q2.cloneTree());
                return result;
            case Q2:
                result = q2.cloneTree();
                result.add(q1.cloneTree());
                return result;
            case BOTH:
                result = q1.cloneTree();
                result.add(q2.cloneTree().getChildren());
                return result;
        }
    } catch (CloneNotSupportedException e) {
        throw new QueryNodeError(e);
    }
    return null;
}
Also used : QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) AndQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode) QueryNodeError(org.apache.lucene.queryparser.flexible.core.QueryNodeError) ArrayList(java.util.ArrayList) AndQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode)

Example 24 with QueryNode

use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.

the class SynonymQueryNodeBuilder method build.

@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
    // TODO: use SynonymQuery instead
    SynonymQueryNode node = (SynonymQueryNode) queryNode;
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (QueryNode child : node.getChildren()) {
        Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
        if (obj != null) {
            Query query = (Query) obj;
            builder.add(query, Occur.SHOULD);
        }
    }
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SynonymQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.SynonymQueryNode) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SynonymQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.SynonymQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) QueryTreeBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)

Example 25 with QueryNode

use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.

the class AnalyzerQueryNodeProcessor method postProcessNode.

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
    if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode) && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode) && !(node.getParent() instanceof RangeQueryNode)) {
        FieldQueryNode fieldNode = ((FieldQueryNode) node);
        String text = fieldNode.getTextAsString();
        String field = fieldNode.getFieldAsString();
        CachingTokenFilter buffer = null;
        PositionIncrementAttribute posIncrAtt = null;
        int numTokens = 0;
        int positionCount = 0;
        boolean severalTokensAtSamePosition = false;
        try {
            try (TokenStream source = this.analyzer.tokenStream(field, text)) {
                buffer = new CachingTokenFilter(source);
                buffer.reset();
                if (buffer.hasAttribute(PositionIncrementAttribute.class)) {
                    posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);
                }
                try {
                    while (buffer.incrementToken()) {
                        numTokens++;
                        int positionIncrement = (posIncrAtt != null) ? posIncrAtt.getPositionIncrement() : 1;
                        if (positionIncrement != 0) {
                            positionCount += positionIncrement;
                        } else {
                            severalTokensAtSamePosition = true;
                        }
                    }
                } catch (IOException e) {
                // ignore
                }
                // rewind the buffer stream
                //will never through on subsequent reset calls
                buffer.reset();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (!buffer.hasAttribute(CharTermAttribute.class)) {
                return new NoTokenFoundQueryNode();
            }
            CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);
            if (numTokens == 0) {
                return new NoTokenFoundQueryNode();
            } else if (numTokens == 1) {
                String term = null;
                try {
                    boolean hasNext;
                    hasNext = buffer.incrementToken();
                    assert hasNext == true;
                    term = termAtt.toString();
                } catch (IOException e) {
                // safe to ignore, because we know the number of tokens
                }
                fieldNode.setText(term);
                return fieldNode;
            } else if (severalTokensAtSamePosition || !(node instanceof QuotedFieldQueryNode)) {
                if (positionCount == 1 || !(node instanceof QuotedFieldQueryNode)) {
                    if (positionCount == 1) {
                        // simple case: only one position, with synonyms
                        LinkedList<QueryNode> children = new LinkedList<>();
                        for (int i = 0; i < numTokens; i++) {
                            String term = null;
                            try {
                                boolean hasNext = buffer.incrementToken();
                                assert hasNext == true;
                                term = termAtt.toString();
                            } catch (IOException e) {
                            // safe to ignore, because we know the number of tokens
                            }
                            children.add(new FieldQueryNode(field, term, -1, -1));
                        }
                        return new GroupQueryNode(new SynonymQueryNode(children));
                    } else {
                        // multiple positions
                        QueryNode q = new BooleanQueryNode(Collections.<QueryNode>emptyList());
                        QueryNode currentQuery = null;
                        for (int i = 0; i < numTokens; i++) {
                            String term = null;
                            try {
                                boolean hasNext = buffer.incrementToken();
                                assert hasNext == true;
                                term = termAtt.toString();
                            } catch (IOException e) {
                            // safe to ignore, because we know the number of tokens
                            }
                            if (posIncrAtt != null && posIncrAtt.getPositionIncrement() == 0) {
                                if (!(currentQuery instanceof BooleanQueryNode)) {
                                    QueryNode t = currentQuery;
                                    currentQuery = new SynonymQueryNode(Collections.<QueryNode>emptyList());
                                    ((BooleanQueryNode) currentQuery).add(t);
                                }
                                ((BooleanQueryNode) currentQuery).add(new FieldQueryNode(field, term, -1, -1));
                            } else {
                                if (currentQuery != null) {
                                    if (this.defaultOperator == Operator.OR) {
                                        q.add(currentQuery);
                                    } else {
                                        q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
                                    }
                                }
                                currentQuery = new FieldQueryNode(field, term, -1, -1);
                            }
                        }
                        if (this.defaultOperator == Operator.OR) {
                            q.add(currentQuery);
                        } else {
                            q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
                        }
                        if (q instanceof BooleanQueryNode) {
                            q = new GroupQueryNode(q);
                        }
                        return q;
                    }
                } else {
                    // phrase query:
                    MultiPhraseQueryNode mpq = new MultiPhraseQueryNode();
                    List<FieldQueryNode> multiTerms = new ArrayList<>();
                    int position = -1;
                    int i = 0;
                    int termGroupCount = 0;
                    for (; i < numTokens; i++) {
                        String term = null;
                        int positionIncrement = 1;
                        try {
                            boolean hasNext = buffer.incrementToken();
                            assert hasNext == true;
                            term = termAtt.toString();
                            if (posIncrAtt != null) {
                                positionIncrement = posIncrAtt.getPositionIncrement();
                            }
                        } catch (IOException e) {
                        // safe to ignore, because we know the number of tokens
                        }
                        if (positionIncrement > 0 && multiTerms.size() > 0) {
                            for (FieldQueryNode termNode : multiTerms) {
                                if (this.positionIncrementsEnabled) {
                                    termNode.setPositionIncrement(position);
                                } else {
                                    termNode.setPositionIncrement(termGroupCount);
                                }
                                mpq.add(termNode);
                            }
                            // Only increment once for each "group" of
                            // terms that were in the same position:
                            termGroupCount++;
                            multiTerms.clear();
                        }
                        position += positionIncrement;
                        multiTerms.add(new FieldQueryNode(field, term, -1, -1));
                    }
                    for (FieldQueryNode termNode : multiTerms) {
                        if (this.positionIncrementsEnabled) {
                            termNode.setPositionIncrement(position);
                        } else {
                            termNode.setPositionIncrement(termGroupCount);
                        }
                        mpq.add(termNode);
                    }
                    return mpq;
                }
            } else {
                TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();
                int position = -1;
                for (int i = 0; i < numTokens; i++) {
                    String term = null;
                    int positionIncrement = 1;
                    try {
                        boolean hasNext = buffer.incrementToken();
                        assert hasNext == true;
                        term = termAtt.toString();
                        if (posIncrAtt != null) {
                            positionIncrement = posIncrAtt.getPositionIncrement();
                        }
                    } catch (IOException e) {
                    // safe to ignore, because we know the number of tokens
                    }
                    FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);
                    if (this.positionIncrementsEnabled) {
                        position += positionIncrement;
                        newFieldNode.setPositionIncrement(position);
                    } else {
                        newFieldNode.setPositionIncrement(i);
                    }
                    pq.add(newFieldNode);
                }
                return pq;
            }
        } finally {
            if (buffer != null) {
                try {
                    buffer.close();
                } catch (IOException e) {
                // safe to ignore
                }
            }
        }
    }
    return node;
}
Also used : FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) TokenStream(org.apache.lucene.analysis.TokenStream) SynonymQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.SynonymQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) ArrayList(java.util.ArrayList) GroupQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode) WildcardQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.WildcardQueryNode) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) NoTokenFoundQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.NoTokenFoundQueryNode) RegexpQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) TokenizedPhraseQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TokenizedPhraseQueryNode) RangeQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode) ModifierQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode) MultiPhraseQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.MultiPhraseQueryNode) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) CachingTokenFilter(org.apache.lucene.analysis.CachingTokenFilter) TokenizedPhraseQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TokenizedPhraseQueryNode) RangeQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode) NoTokenFoundQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.NoTokenFoundQueryNode) RegexpQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode) GroupQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) TextableQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TextableQueryNode) MultiPhraseQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.MultiPhraseQueryNode) ModifierQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode) QuotedFieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode) SynonymQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.SynonymQueryNode) WildcardQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.WildcardQueryNode) TextableQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.TextableQueryNode) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)

Aggregations

QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)26 BooleanQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode)12 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)11 GroupQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.GroupQueryNode)11 ModifierQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.ModifierQueryNode)11 OrQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode)10 BoostQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode)9 FuzzyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode)9 QuotedFieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QuotedFieldQueryNode)9 RegexpQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.RegexpQueryNode)9 AndQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode)8 SlopQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.SlopQueryNode)7 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)7 QueryTreeBuilder (org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder)5 ArrayList (java.util.ArrayList)4 Query (org.apache.lucene.search.Query)4 LinkedList (java.util.LinkedList)3 Vector (java.util.Vector)3 TokenizedPhraseQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.TokenizedPhraseQueryNode)3 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)3