Example usage for org.apache.lucene.search BooleanQuery BooleanQuery

List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search BooleanQuery BooleanQuery.

Prototype

BooleanQuery

Source Link

Usage

From source file:com.memonews.solr.search.HighlightQParserPlugin.java

License:Apache License

@Override
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive,
        boolean endInclusive) throws ParseException {
    if (fieldNames.contains(field)) {
        return new BooleanQuery();
    }//from w  ww .  j av a  2s  . c  o m
    return super.getRangeQuery(field, part1, part2, startInclusive, endInclusive);
}

From source file:com.memonews.solr.search.HighlightQParserPlugin.java

License:Apache License

@Override
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
    if (fieldNames.contains(field)) {
        return new BooleanQuery();
    }//from  w  w  w. j a va  2s. co m
    return super.getPrefixQuery(field, termStr);
}

From source file:com.memonews.solr.search.HighlightQParserPlugin.java

License:Apache License

@Override
protected Query getWildcardQuery(String field, String termStr) throws ParseException {
    if (fieldNames.contains(field)) {
        return new BooleanQuery();
    }//from  w w  w  . j  av  a 2  s  .  c  om
    return super.getWildcardQuery(field, termStr);
}

From source file:com.memonews.solr.search.HighlightQParserPlugin.java

License:Apache License

@Override
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
    if (fieldNames.contains(field)) {
        return new BooleanQuery();
    }/*w  w w.  java2 s  .com*/
    return super.getRegexpQuery(field, termStr);
}

From source file:com.mhs.qsol.proximity.distribute.BasicDistributable.java

License:Apache License

public Query distribute(Distributable distrib, ProxInfo proxInfo) {
    BooleanQuery boolQuery = new BooleanQuery();
    List<Distributable> children = distrib.getChildren();

    if (children == null) {
        Query query = distrib.distribute(this.query, proxInfo);

        if (logger.isLoggable(Level.FINE)) {
            logger.fine("return query:" + query);
        }//  w w w .j a v  a  2 s. c  o  m

        return query;
    }

    Query query;
    Query cacheQuery2 = null;
    int size = children.size();
    List<Occur> connectors = distrib.getConnectors();

    for (int i = 0; i < size; i++) {
        Occur con = connectors.get(i);

        // if we have already computed query2 looking for a possible SpanOr
        // use
        if (cacheQuery2 != null) {
            query = cacheQuery2;
        } else {
            query = children.get(i).distribute(this.query, proxInfo);
        }

        // must make sure both clauses are spans and connector is | if you
        // want to optimize to SpanOr
        if (children.size() > (i + 1) && (con == Occur.SHOULD) && query instanceof SpanQuery) {
            cacheQuery2 = children.get(i + 1).distribute(this.query, proxInfo);

            if (cacheQuery2 instanceof SpanQuery) {
                if (children.size() == 2) {
                    return new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 });
                } else {
                    query = new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 });
                    cacheQuery2 = null;
                    i++;
                }
            } else {
                cacheQuery2 = null;
            }
        }

        boolQuery.add(query, con);
    }

    if (logger.isLoggable(Level.FINE)) {
        logger.fine("distribute(Distributable) - to distrib:" + distrib + " and :" + this.query);
        logger.fine("boolquery:" + boolQuery.toString());
    }

    return boolQuery;
}

From source file:com.mhs.qsol.proximity.distribute.GroupDistributable.java

License:Apache License

public Query distribute(Distributable distrib, ProxInfo proxInfo) {
    if (distrib == null) {
        throw new IllegalArgumentException("distrib is null");
    }//from www.j  av  a2 s . co m

    // compacts distrib.
    List<Distributable> children = distrib.getChildren();

    if ((children != null) && (children.size() == 1)) {
        distrib = children.get(0);
        distrib.setParent(null);
    }

    // compacts distribs.
    if (distribs.size() == 1) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("compacting distribs");
        }

        Distributable returnDistrib = distribs.get(0);

        returnDistrib.setParent(null);

        if (logger.isLoggable(Level.FINE)) {
            logger.fine("compacting distribs:" + returnDistrib);
            logger.fine("other distrib:" + "sec" + distrib.toString());
        }

        return returnDistrib.distribute(distrib, proxInfo);
    }

    BooleanQuery boolQuery = new BooleanQuery();

    Query query;
    Query cacheQuery2 = null;

    for (int i = 0; i < distribs.size(); i++) {
        Occur con = connector.get(i);

        // if we have already computed query2 looking for a possible SpanOr use
        if (cacheQuery2 != null) {
            query = cacheQuery2;
        } else {
            if (logger.isLoggable(Level.FINE)) {
                logger.fine("distrib: " + distribs.get(i) + " WITH " + distrib);
            }

            query = distribs.get(i).distribute(distrib, proxInfo);

            if (logger.isLoggable(Level.FINE)) {
                logger.fine("thequery: " + query);
            }

            // logger.fine("query: " + query);
        }

        // must make sure both clauses are spans and connector is | if you want to
        // optimize to SpanOr
        if (!((i + 1) == distribs.size()) && (con == Occur.SHOULD) && query instanceof SpanQuery) {
            cacheQuery2 = distribs.get(i + 1).distribute(distrib, proxInfo);

            if (cacheQuery2 instanceof SpanQuery) {
                // if just a single 'or' set in this group
                if (distribs.size() == 2) {
                    return new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 });
                } else {
                    query = new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 });
                    cacheQuery2 = null;
                    i++;
                }
            } else {
                cacheQuery2 = null;
            }
        }

        // TODO: update this
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("distribute(Distributable) - adding connector:" + con);
        }

        if (logger.isLoggable(Level.FINE)) {
            logger.fine("distribute(Distributable) - to distrib:" + distribs + " and :" + distrib);
            logger.fine("query:" + query);
        }

        boolQuery.add(query, con);
    }

    return boolQuery;
}

From source file:com.mhs.qsol.proximity.distribute.GroupDistributable.java

License:Apache License

public Query distribute(SpanQuery spanQuery, ProxInfo proxInfo) {
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("distributing term: " + spanQuery + " with:" + distribs);
    }/*from w  w  w  . jav  a  2s.  c om*/

    BooleanQuery boolQuery = new BooleanQuery();
    int size = distribs.size();

    for (int i = 0; i < size; i++) {
        Query query = distribs.get(i).distribute(spanQuery, proxInfo);
        boolQuery.add(query, connector.get(i));
    }

    // we are not returning a span
    return boolQuery;
}

From source file:com.mhs.qsol.proximity.ProximityBuilder.java

License:Apache License

public void constructProximityQuery(String proximityToken, String field) {
    boolean ordered = false;

    Matcher m = PROXIMITY.matcher(proximityToken);
    m.matches();/*from  www .  java2s  .c om*/

    String inorder = m.group(1);

    if (inorder.length() > 0) {
        ordered = true;
    }

    String distance = m.group(2);
    String pType = m.group(3);

    if (distance.length() == 0) {
        distance = "1";
    }

    if (pType.length() == 0) {
        proxType = ProxType.WORD;
    } else if (pType.compareToIgnoreCase("p") == 0) {
        proxType = ProxType.PARAGRAPH;
    } else if (pType.compareToIgnoreCase("s") == 0) {
        proxType = ProxType.SENTENCE;
    }

    if (logger.isLoggable(Level.FINE)) {
        logger.fine("constructProximityQuery(String, String) - distance:" + distance);
    }

    Distributable newest = distribClauses.get(distribClauses.size() - 1);
    BooleanQuery boolQuery = null;

    ProxInfo proxInfo = new ProxInfo(distance, ordered, proxType, sentMarker, paraMarker);

    proxInfo.fieldBreakMarker = this.fieldBreakMarker;

    if (distribClauses.size() > 2) {
        boolQuery = new BooleanQuery();

        for (int i = 0; i < (distribClauses.size() - 1); i++) {
            boolQuery.add(distribClauses.get(i).distribute(newest, proxInfo), Occur.MUST);
        }

        if (wholeQuery == null) {
            wholeQuery = boolQuery;
            moreThanOne = true;
        } else {

            if (moreThanOne) {
                ((BooleanQuery) wholeQuery).add(boolQuery, Occur.MUST);
            } else {
                BooleanQuery newBoolQuery = new BooleanQuery();
                newBoolQuery.add(wholeQuery, Occur.MUST);
                newBoolQuery.add(boolQuery, Occur.MUST);

                wholeQuery = newBoolQuery;
                moreThanOne = true;
            }
        }

        return;
    }

    if (logger.isLoggable(Level.FINE)) {
        logger.fine("DISTRIBUTING: " + distribClauses.get(0).toString() + "\nWITH\n" + newest);
    }

    Query distribQuery = distribClauses.get(0).distribute(newest, proxInfo);

    if (logger.isLoggable(Level.FINE)) {
        logger.fine("distrib query: " + distribQuery);
    }

    if (wholeQuery == null) {
        wholeQuery = distribQuery;
        moreThanOne = false;
    } else {

        if (moreThanOne) {
            ((BooleanQuery) wholeQuery).add(distribQuery, Occur.MUST);
        } else {
            BooleanQuery newBoolQuery = new BooleanQuery();
            newBoolQuery.add(wholeQuery, Occur.MUST);
            newBoolQuery.add(distribQuery, Occur.MUST);

            wholeQuery = newBoolQuery;
            moreThanOne = true;
        }

    }
}

From source file:com.mhs.qsol.QsolToQueryVisitor.java

License:Apache License

/**
 * f0 -> <FIELDSTART> f1 -> CheckOrd1Search() f2 -> ")"
 *///from  w w  w  . j  a v  a2  s  . co m
public Query visit(FieldSearch n, Query query) {
    Query returnQuery = null;

    // get comma delimited field list
    String fieldList = n.f0.toString();

    String[] fields = fieldList.split(",");

    BooleanQuery fieldSearch = new BooleanQuery();
    // save the query field
    String oldField = field;
    // apply the search to each field
    for (int x = 0; x < fields.length; x++) {
        field = fields[x];

        String mapTo;

        if ((mapTo = fieldMap.get(field)) != null) {
            field = mapTo;
        }

        // if the field is a registered date field, build date
        if (dateFields.contains(field)) {
            StringBuilder date = new StringBuilder();

            BasicSearch tokens = n.f1.f0.f0.f0.f0;

            // (SearchToken) n.f2.f0.f0.f0.f0.f0.choice;
            int size = tokens.f0.size();

            for (int i = 0; i < size; i++) {
                // NodeChoice choice = (NodeChoice) tokens.f0.elementAt(i);
                // date.append(choice.choice.toString());
                SearchToken choice = (SearchToken) ((BasicSearchType) tokens.f0.elementAt(i)).f0.choice;
                date.append(choice.f0.choice);

                if (i != (size - 1)) {
                    date.append(" ");
                }
            }

            returnQuery = dateParser.buildDateQuery(field, date.toString(), locale);
        } else {

            returnQuery = n.f1.accept(this, query);

        }

        if (returnQuery != null) {
            fieldSearch.add(returnQuery, BooleanClause.Occur.SHOULD);
        }
    }
    field = oldField;
    if (fieldSearch.getClauses().length == 0) {
        return null;
    }
    return fieldSearch;
}

From source file:com.mhs.qsol.QsolToQueryVisitor.java

License:Apache License

/**
 * f0 -> <operator> f1 -> CheckNextOp() f2 -> ( CurrentOp() )?
 *//*  w  ww  .  ja  v a 2s  . c o m*/
private Query visitBooleanOp(VisitOp op, Query query, List<Occur> occurs) {
    // run down right side of OPERATOR
    Query returnQuery = op.visitf1(this, query);

    // check for stop word removal
    if (returnQuery == null) {
        // if another of the same op follows
        if (op.isF2Present()) {

            returnQuery = op.visitf2(this, query);

            return returnQuery;
        } else {

            return query;
        }
    }

    if (query == null) {
        // if another of the same op follows
        if (op.isF2Present()) {
            query = op.visitf2(this, returnQuery);

            return query;
        } else {

            return returnQuery;
        }
    }

    // done stop word check
    BooleanQuery boolQuery = null;

    // if in an op chain i.e. mark & horse & beer : the second & is in an op
    // chain
    if (opChain[op.getOpNum() - 1] && query instanceof BooleanQuery) {
        boolQuery = (BooleanQuery) query;
        boolQuery.add(returnQuery, occurs.get(1));
    } else {
        boolQuery = new BooleanQuery();

        boolQuery.add(query, occurs.get(0));

        boolQuery.add(returnQuery, occurs.get(1));
    }

    // if another of the same op follows set that we are in an op chain
    if (op.isF2Present()) {
        opChain[op.getOpNum() - 1] = true;

        Query nextQuery = op.visitf2(this, boolQuery);
        opChain[op.getOpNum() - 1] = false;

        return nextQuery;
    }

    return boolQuery;
}