Package org.apache.lucene.search

Examples of org.apache.lucene.search.BooleanQuery


      assertTrue(collator.compare(value, startPoint) >= 0);
      assertTrue(collator.compare(value, endPoint) <= 0);
    }
   
    // negative test
    BooleanQuery bq = new BooleanQuery();
    bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
    bq.add(query, Occur.MUST_NOT);
    docs = is.search(bq, is.getIndexReader().maxDoc());
    for (ScoreDoc doc : docs.scoreDocs) {
      String value = is.doc(doc.doc).get("field");
      assertTrue(collator.compare(value, startPoint) < 0 || collator.compare(value, endPoint) > 0);
    }
View Full Code Here


    }

    //---------------------< QueryNodeVisitor interface >-----------------------

    public Object visit(QueryRootNode node, Object data) {
        BooleanQuery root = new BooleanQuery();

        Query wrapped = root;
        if (node.getLocationNode() != null) {
            wrapped = (Query) node.getLocationNode().accept(this, root);
        }
View Full Code Here

        return wrapped;
    }

    public Object visit(OrQueryNode node, Object data) {
        BooleanQuery orQuery = new BooleanQuery();
        Object[] result = node.acceptOperands(this, null);
        for (int i = 0; i < result.length; i++) {
            Query operand = (Query) result[i];
            orQuery.add(operand, Occur.SHOULD);
        }
        return orQuery;
    }
View Full Code Here

    public Object visit(AndQueryNode node, Object data) {
        Object[] result = node.acceptOperands(this, null);
        if (result.length == 0) {
            return null;
        }
        BooleanQuery andQuery = new BooleanQuery();
        for (int i = 0; i < result.length; i++) {
            Query operand = (Query) result[i];
            andQuery.add(operand, Occur.MUST);
        }
        return andQuery;
    }
View Full Code Here

        Object[] result = node.acceptOperands(this, null);
        if (result.length == 0) {
            return data;
        }
        // join the results
        BooleanQuery b = new BooleanQuery();
        for (int i = 0; i < result.length; i++) {
            b.add((Query) result[i], Occur.SHOULD);
        }
        // negate
        return new NotQuery(b);
    }
View Full Code Here

        } catch (RepositoryException e) {
            exceptions.add(e);
        }
        if (terms.size() == 0) {
            // exception occured
            return new BooleanQuery();
        } else if (terms.size() == 1) {
            return new TermQuery((Term) terms.get(0));
        } else {
            BooleanQuery b = new BooleanQuery();
            for (Iterator it = terms.iterator(); it.hasNext();) {
                b.add(new TermQuery((Term) it.next()), Occur.SHOULD);
            }
            return b;
        }
    }
View Full Code Here

                    // if path references node that's elements.length - 1
                    if (name != null
                            && ((node.getReferencesProperty() && i == elements.length - 2)
                                || (!node.getReferencesProperty() && i == elements.length - 1))) {
                        Query q = new TermQuery(new Term(FieldNames.LABEL, name));
                        BooleanQuery and = new BooleanQuery();
                        and.add(q, Occur.MUST);
                        and.add(context, Occur.MUST);
                        context = and;
                    } else if ((node.getReferencesProperty() && i < elements.length - 2)
                            || (!node.getReferencesProperty() && i < elements.length - 1)) {
                        // otherwise do a parent axis step
                        context = new ParentAxisQuery(context, name);
View Full Code Here

                    try {
                        name = NameFormat.format(nameTest, nsMappings);
                    } catch (NoPrefixDeclaredException e) {
                        exceptions.add(e);
                    }
                    BooleanQuery and = new BooleanQuery();
                    and.add(new TermQuery(new Term(FieldNames.PARENT, "")), Occur.MUST);
                    and.add(new TermQuery(new Term(FieldNames.LABEL, name)), Occur.MUST);
                    context = and;
                }
                LocationStepQueryNode[] tmp = new LocationStepQueryNode[steps.length - 1];
                System.arraycopy(steps, 1, tmp, 0, steps.length - 1);
                steps = tmp;
            } else {
                // path is 1) relative or 2) descendant-or-self
                // use root node as context
                context = new TermQuery(new Term(FieldNames.PARENT, ""));
            }
        } else {
            exceptions.add(new InvalidQueryException("Number of location steps must be > 0"));
        }
        // loop over steps
        for (int i = 0; i < steps.length; i++) {
            context = (Query) steps[i].accept(this, context);
        }
        if (data instanceof BooleanQuery) {
            BooleanQuery constraint = (BooleanQuery) data;
            if (constraint.getClauses().length > 0) {
                constraint.add(context, Occur.MUST);
                context = constraint;
            }
        }
        return context;
    }
View Full Code Here

        return context;
    }

    public Object visit(LocationStepQueryNode node, Object data) {
        Query context = (Query) data;
        BooleanQuery andQuery = new BooleanQuery();

        if (context == null) {
            exceptions.add(new IllegalArgumentException("Unsupported query"));
        }

        // predicate on step?
        Object[] predicates = node.acceptOperands(this, data);
        for (int i = 0; i < predicates.length; i++) {
            andQuery.add((Query) predicates[i], Occur.MUST);
        }

        // check for position predicate
        QueryNode[] pred = node.getPredicates();
        for (int i = 0; i < pred.length; i++) {
            if (pred[i].getType() == QueryNode.TYPE_RELATION) {
                RelationQueryNode pos = (RelationQueryNode) pred[i];
                if (pos.getValueType() == QueryConstants.TYPE_POSITION) {
                    node.setIndex(pos.getPositionValue());
                }
            }
        }

        TermQuery nameTest = null;
        if (node.getNameTest() != null) {
            try {
                String internalName = NameFormat.format(node.getNameTest(), nsMappings);
                nameTest = new TermQuery(new Term(FieldNames.LABEL, internalName));
            } catch (NoPrefixDeclaredException e) {
                // should never happen
                exceptions.add(e);
            }
        }

        if (node.getIncludeDescendants()) {
            if (nameTest != null) {
                andQuery.add(new DescendantSelfAxisQuery(context, nameTest), Occur.MUST);
            } else {
                // descendant-or-self with nametest=*
                if (predicates.length > 0) {
                    // if we have a predicate attached, the condition acts as
                    // the sub query.

                    // only use descendant axis if path is not //*
                    // otherwise the query for the predicate can be used itself
                    PathQueryNode pathNode = (PathQueryNode) node.getParent();
                    if (pathNode.getPathSteps()[0] != node) {
                        Query subQuery = new DescendantSelfAxisQuery(context, andQuery, false);
                        andQuery = new BooleanQuery();
                        andQuery.add(subQuery, Occur.MUST);
                    }
                } else {
                    // todo this will traverse the whole index, optimize!
                    Query subQuery = null;
                    try {
                        subQuery = new MatchAllQuery(NameFormat.format(QName.JCR_PRIMARYTYPE, nsMappings));
                    } catch (NoPrefixDeclaredException e) {
                        // will never happen, prefixes are created when unknown
                    }
                    // only use descendant axis if path is not //*
                    PathQueryNode pathNode = (PathQueryNode) node.getParent();
                    if (pathNode.getPathSteps()[0] != node) {
                        context = new DescendantSelfAxisQuery(context, subQuery);
                        andQuery.add(new ChildAxisQuery(sharedItemMgr, context, null, node.getIndex()), Occur.MUST);
                    } else {
                        andQuery.add(subQuery, Occur.MUST);
                    }
                }
            }
        } else {
            // name test
            if (nameTest != null) {
                andQuery.add(new ChildAxisQuery(sharedItemMgr, context, nameTest.getTerm().text(), node.getIndex()), Occur.MUST);
            } else {
                // select child nodes
                andQuery.add(new ChildAxisQuery(sharedItemMgr, context, null, node.getIndex()), Occur.MUST);
            }
        }

        return andQuery;
    }
View Full Code Here

            context = new DerefQuery(context, refProperty, nameTest);

            // attach predicates
            Object[] predicates = node.acceptOperands(this, data);
            if (predicates.length > 0) {
                BooleanQuery andQuery = new BooleanQuery();
                for (int i = 0; i < predicates.length; i++) {
                    andQuery.add((Query) predicates[i], Occur.MUST);
                }
                andQuery.add(context, Occur.MUST);
                context = andQuery;
            }

        } catch (NoPrefixDeclaredException e) {
            // should never happen
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.BooleanQuery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.