Search in sources :

Example 11 with QueryNode

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

the class MultiPhraseQueryNodeBuilder method build.

@Override
public MultiPhraseQuery build(QueryNode queryNode) throws QueryNodeException {
    MultiPhraseQueryNode phraseNode = (MultiPhraseQueryNode) queryNode;
    MultiPhraseQuery.Builder phraseQueryBuilder = new MultiPhraseQuery.Builder();
    List<QueryNode> children = phraseNode.getChildren();
    if (children != null) {
        TreeMap<Integer, List<Term>> positionTermMap = new TreeMap<>();
        for (QueryNode child : children) {
            FieldQueryNode termNode = (FieldQueryNode) child;
            TermQuery termQuery = (TermQuery) termNode.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
            List<Term> termList = positionTermMap.get(termNode.getPositionIncrement());
            if (termList == null) {
                termList = new LinkedList<>();
                positionTermMap.put(termNode.getPositionIncrement(), termList);
            }
            termList.add(termQuery.getTerm());
        }
        for (int positionIncrement : positionTermMap.keySet()) {
            List<Term> termList = positionTermMap.get(positionIncrement);
            phraseQueryBuilder.add(termList.toArray(new Term[termList.size()]), positionIncrement);
        }
    }
    return phraseQueryBuilder.build();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) QueryTreeBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) TreeMap(java.util.TreeMap) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) MultiPhraseQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.MultiPhraseQueryNode) MultiPhraseQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.MultiPhraseQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) List(java.util.List) LinkedList(java.util.LinkedList)

Example 12 with QueryNode

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

the class SpanOrQueryNodeBuilder method build.

@Override
public SpanOrQuery build(QueryNode node) throws QueryNodeException {
    // validates node
    BooleanQueryNode booleanNode = (BooleanQueryNode) node;
    List<QueryNode> children = booleanNode.getChildren();
    SpanQuery[] spanQueries = new SpanQuery[children.size()];
    int i = 0;
    for (QueryNode child : children) {
        spanQueries[i++] = (SpanQuery) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
    }
    return new SpanOrQuery(spanQueries);
}
Also used : BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) BooleanQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BooleanQueryNode) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 13 with QueryNode

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

the class QueryParserHelper method parse.

/**
   * Parses a query string to an object, usually some query object.<br>
   * <br>
   * In this method the three phases are executed: <br>
   * <br>
   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1st - the query string is parsed using the
   * text parser returned by {@link #getSyntaxParser()}, the result is a query
   * node tree <br>
   * <br>
   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2nd - the query node tree is processed by the
   * processor returned by {@link #getQueryNodeProcessor()} <br>
   * <br>
   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3th - a object is built from the query node
   * tree using the builder returned by {@link #getQueryBuilder()}
   * 
   * @param query
   *          the query string
   * @param defaultField
   *          the default field used by the text parser
   * 
   * @return the object built from the query
   * 
   * @throws QueryNodeException
   *           if something wrong happens along the three phases
   */
public Object parse(String query, String defaultField) throws QueryNodeException {
    QueryNode queryTree = getSyntaxParser().parse(query, defaultField);
    QueryNodeProcessor processor = getQueryNodeProcessor();
    if (processor != null) {
        queryTree = processor.process(queryTree);
    }
    return getQueryBuilder().build(queryTree);
}
Also used : QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) QueryNodeProcessor(org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor)

Example 14 with QueryNode

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

the class QueryTreeBuilder method process.

private void process(QueryNode node) throws QueryNodeException {
    if (node != null) {
        QueryBuilder builder = getBuilder(node);
        if (!(builder instanceof QueryTreeBuilder)) {
            List<QueryNode> children = node.getChildren();
            if (children != null) {
                for (QueryNode child : children) {
                    process(child);
                }
            }
        }
        processNode(node, builder);
    }
}
Also used : QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)

Example 15 with QueryNode

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

the class QueryNodeProcessorImpl method processChildren.

/**
   * This method is called every time a child is processed.
   * 
   * @param queryTree
   *          the query node child to be processed
   * @throws QueryNodeException
   *           if something goes wrong during the query node processing
   */
protected void processChildren(QueryNode queryTree) throws QueryNodeException {
    List<QueryNode> children = queryTree.getChildren();
    ChildrenList newChildren;
    if (children != null && children.size() > 0) {
        newChildren = allocateChildrenList();
        try {
            for (QueryNode child : children) {
                child = processIteration(child);
                if (child == null) {
                    throw new NullPointerException();
                }
                newChildren.add(child);
            }
            List<QueryNode> orderedChildrenList = setChildrenOrder(newChildren);
            queryTree.set(orderedChildrenList);
        } finally {
            newChildren.beingUsed = false;
        }
    }
}
Also used : QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)

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