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.esri.gpt.catalog.lucene.SpatialClauseAdapter.java

License:Apache License

/**
 * Makes a boolean query based upon a collection of queries and a logical operator.
 * @param queries the query collection//from   w ww  .  j a va 2s.  co m
 * @param occur the logical operator
 * @return the query
 */
private BooleanQuery makeQuery(Query[] queries, BooleanClause.Occur occur) {
    BooleanQuery bq = new BooleanQuery();
    for (Query query : queries) {
        bq.add(query, occur);
    }
    return bq;
}

From source file:com.esri.gpt.catalog.lucene.SpatialClauseAdapter.java

License:Apache License

/**
 * Constructs a query to retrieve documents that do or do not cross the date line
 * and match the supplied spatial query. 
 * @param crossedDateLine <code>true</true> for documents that cross the date line
 * @param query the spatial query//from ww  w .  ja va 2s  . co m
 * @return the query
 */
private Query makeXDL(boolean crossedDateLine, Query query) {
    BooleanQuery bq = new BooleanQuery();
    bq.add(this.makeXDL(crossedDateLine), BooleanClause.Occur.MUST);
    bq.add(query, BooleanClause.Occur.MUST);
    return bq;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Adapts a timeperiod based PropertyClause to the Lucene model.
 * @param activeBooleanQuery the active Lucene boolean query
 * @param activeLogicalClause the active discovery logical clause
 * @param propertyClause the property clause to adapt
 * @throws DiscoveryException if an invalid clause is encountered
 * @throws ParseException if a Lucene query parsing exception occurs
 *///from   w  w  w  . java  2s.  com
protected void adaptPropertyClause(BooleanQuery activeBooleanQuery, LogicalClause activeLogicalClause,
        PropertyClause propertyClause) throws DiscoveryException, ParseException {
    LOGGER.finer("Adapting timeperiod PropertyClause...\n" + propertyClause);

    // determine the discoverable target, set the underlying storable
    Discoverable discoverable = propertyClause.getTarget();
    if (discoverable == null) {
        String sErr = "The PropertyClause.target is null.";
        throw new DiscoveryException(sErr);
    }
    if (discoverable.getStorable() == null) {
        String sErr = "The PropertyClause.target.storeable is null.";
        throw new DiscoveryException(sErr);
    } else {
        Storeable storeable = (Storeable) discoverable.getStorable();
        this.baseFieldName = storeable.getName();
        if (this.baseFieldName.endsWith(".intersects")) {
            this.inclusive = true;
            this.baseFieldName = this.baseFieldName.substring(0, this.baseFieldName.length() - 11);
        } else if (this.baseFieldName.endsWith(".within")) {
            this.inclusive = false;
            this.baseFieldName = this.baseFieldName.substring(0, this.baseFieldName.length() - 7);
        }
        this.intervalMetaFieldName = this.baseFieldName + ".imeta";
        this.multiplicityFieldName = this.baseFieldName + ".num";
        this.summaryMetaFieldName = this.baseFieldName + ".meta";
    }

    // initialize values
    boolean bInclusive = this.inclusive;
    String sLiteral = Val.chkStr(propertyClause.getLiteral());
    String sLower = "";
    String sUpper = "";
    String sErr = null;
    String sErrSfx = " is not supported for timeperiod fields," + " use PropertyIsBetween.";

    if (propertyClause instanceof PropertyIsBetween) {
        PropertyIsBetween between = (PropertyIsBetween) propertyClause;
        sLower = Val.chkStr(between.getLowerBoundary());
        sUpper = Val.chkStr(between.getUpperBoundary());
        this.queryLower = this.parseDateTime(sLower, false);
        this.queryUpper = this.parseDateTime(sUpper, true);

    } else if ((propertyClause instanceof PropertyIsEqualTo)
            || (propertyClause instanceof PropertyIsNotEqualTo)) {
        Query q = null;
        sLower = Val.chkStr(sLiteral);
        sUpper = Val.chkStr(sLiteral);
        this.queryLower = this.parseDateTime(sLower, false);
        if (this.queryLower == null) {
            sErr = "Timeperiod literal cannot be null for PropertyIsEqualTo/PropertyIsNotEqualTo";
        } else {
            this.queryUpper = this.parseDateTime(sUpper, true);
            if (propertyClause instanceof PropertyIsEqualTo) {
                q = this.makeEquals();
            } else {
                q = this.makeNotEquals();
            }
            appendQuery(activeBooleanQuery, activeLogicalClause, q);
            return;
        }

    } else if (propertyClause instanceof PropertyIsGreaterThan) {
        bInclusive = false; // use within logic
        sLower = sLiteral;
        this.queryLower = this.parseDateTime(sLower, false);
        if (this.queryLower != null) {
            this.queryLower = new Long(this.queryLower.longValue() + 1);
        }

    } else if (propertyClause instanceof PropertyIsGreaterThanOrEqualTo) {
        bInclusive = false; // use within logic
        sLower = sLiteral;
        this.queryLower = this.parseDateTime(sLower, false);

    } else if (propertyClause instanceof PropertyIsLessThan) {
        bInclusive = false; // use within logic
        sUpper = sLiteral;
        this.queryUpper = this.parseDateTime(sUpper, false);
        if (this.queryUpper != null) {
            this.queryUpper = new Long(this.queryUpper.longValue() - 1);
        }

    } else if (propertyClause instanceof PropertyIsLessThanOrEqualTo) {
        bInclusive = false; // use within logic
        sUpper = sLiteral;
        this.queryUpper = this.parseDateTime(sUpper, true);

    } else if (propertyClause instanceof PropertyIsNull) {
        appendQuery(activeBooleanQuery, activeLogicalClause, this.makeNull());
        return;

    } else if (propertyClause instanceof PropertyIsLike) {
        sErr = "PropertyIsLike" + sErrSfx;

    } else {
        sErr = "Unrecognized property clause type: " + propertyClause.getClass().getName();
    }
    if (sErr != null) {
        throw new DiscoveryException(sErr);
    }

    // check for upper < lower
    if ((this.queryLower != null) && (this.queryUpper != null)) {
        if (this.queryUpper.longValue() < this.queryLower.longValue()) {
            appendQuery(activeBooleanQuery, activeLogicalClause, new BooleanQuery());
            return;
        }
    }

    // could implement a timeperiod relevance ranking here
    if ((this.queryLower != null) && (this.queryUpper != null)) {
    }

    if (bInclusive) {
        this.determineRelationshipWithNow();
        this.determineMaxIntervalFieldName();
        appendQuery(activeBooleanQuery, activeLogicalClause, makeIntersects());
    } else {
        this.determineRelationshipWithNow();
        appendQuery(activeBooleanQuery, activeLogicalClause, makeWithin());
    }
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for documents that are equal to the 
 * input time period.//w ww  . jav  a  2s .  c  o  m
 * @return the query
 */
private Query makeEquals() {

    /**
     * one determinate and boundaries are equal
     */
    int nStep = this.precisionStep;
    String fSMeta = this.summaryMetaFieldName;
    String fLower = this.getLowerFieldName(0);
    String fUpper = this.getUpperFieldName(0);

    String sMeta = "is1determinate";
    Query qIs1Determinate = new TermQuery(new Term(fSMeta, sMeta));
    Query qDocLowerEq = NumericRangeQuery.newLongRange(fLower, nStep, queryLower, queryLower, true, true);
    Query qDocUpperEq = NumericRangeQuery.newLongRange(fUpper, nStep, queryUpper, queryUpper, true, true);

    BooleanQuery bq = new BooleanQuery();
    bq.add(qIs1Determinate, BooleanClause.Occur.MUST);
    bq.add(qDocLowerEq, BooleanClause.Occur.MUST);
    bq.add(qDocUpperEq, BooleanClause.Occur.MUST);
    return bq;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for documents that intersect the input time period.
 * @return the query//from   ww  w  . j a  va 2 s  . co m
 */
private Query makeIntersects() {
    BooleanQuery bq = new BooleanQuery();
    for (int i = 1; i <= this.maxIntervalFieldName; i++) {
        Query q = this.makeIntersectsInterval(i);
        bq.add(q, BooleanClause.Occur.SHOULD);
    }
    return bq;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for a document interval that intersects 
 * the input time period.//from ww  w  .j  a v a 2 s .c om
 * @param interval the field name index for the interval
 * @return the query
 */
private Query makeIntersectsInterval(int interval) {
    /*
      Intersects:
              
           docMinIn:    fMin >= qMin AND fMin <= qMax
        OR docMaxIn:    fMax >= qMin AND fMax <= qMax
        OR docContains: fMin <= qMin AND fMax >= qMax
    */

    int nStep = this.precisionStep;
    String fMeta = this.intervalMetaFieldName;
    String fLower = this.getLowerFieldName(interval);
    String fUpper = this.getUpperFieldName(interval);

    Query qDocLowerIn = NumericRangeQuery.newLongRange(fLower, nStep, queryLower, queryUpper, true, true);
    Query qDocUpperIn = NumericRangeQuery.newLongRange(fUpper, nStep, queryLower, queryUpper, true, true);

    BooleanQuery qDocContains = new BooleanQuery();
    Query qLowerBeforeL = NumericRangeQuery.newLongRange(fLower, nStep, null, queryLower, true, true);
    Query qLowerBeforeU = NumericRangeQuery.newLongRange(fLower, nStep, null, queryUpper, true, true);
    Query qUpperAfterL = NumericRangeQuery.newLongRange(fUpper, nStep, queryLower, null, true, true);
    Query qUpperAfterU = NumericRangeQuery.newLongRange(fUpper, nStep, queryUpper, null, true, true);
    qDocContains.add(qLowerBeforeL, BooleanClause.Occur.MUST);
    qDocContains.add(qLowerBeforeU, BooleanClause.Occur.MUST);
    qDocContains.add(qUpperAfterL, BooleanClause.Occur.MUST);
    qDocContains.add(qUpperAfterU, BooleanClause.Occur.MUST);

    BooleanQuery qIntervalIn = new BooleanQuery();
    qIntervalIn.add(qDocLowerIn, BooleanClause.Occur.SHOULD);
    qIntervalIn.add(qDocUpperIn, BooleanClause.Occur.SHOULD);
    qIntervalIn.add(qDocContains, BooleanClause.Occur.SHOULD);

    String sMeta = this.getMetaValue("determinate", interval);
    Query qIsDeterminate = new TermQuery(new Term(fMeta, sMeta));

    BooleanQuery bqDeterminate = new BooleanQuery();
    bqDeterminate.add(qIsDeterminate, BooleanClause.Occur.MUST);
    bqDeterminate.add(qIntervalIn, BooleanClause.Occur.MUST);

    // intervals that intersect now
    BooleanQuery bqNow = new BooleanQuery();
    if (this.queryIntersectsNow) {
        // any interval with the following meta terms intersects:
        // now.i now.l.i now.u.i where i is the interval index (1 based)
        String s1 = this.getMetaValue("now", interval);
        String s2 = this.getMetaValue("now.l", interval);
        String s3 = this.getMetaValue("now.u", interval);
        Query q1 = new TermQuery(new Term(fMeta, s1));
        Query q2 = new TermQuery(new Term(fMeta, s2));
        Query q3 = new TermQuery(new Term(fMeta, s3));
        bqNow.add(q1, BooleanClause.Occur.SHOULD);
        bqNow.add(q2, BooleanClause.Occur.SHOULD);
        bqNow.add(q3, BooleanClause.Occur.SHOULD);
    } else if (this.queryIsBeforeNow) {
        // meta term now.u.i and fLower must be <= queryUpper
        String s1 = this.getMetaValue("now.u", interval);
        Query q1 = new TermQuery(new Term(fMeta, s1));
        Query q2 = NumericRangeQuery.newLongRange(fLower, nStep, null, queryUpper, true, true);
        bqNow.add(q1, BooleanClause.Occur.MUST);
        bqNow.add(q2, BooleanClause.Occur.MUST);
    } else if (this.queryIsAfterNow) {
        // meta term now.l.i and fUpper must be >= queryLower
        String s1 = this.getMetaValue("now.l", interval);
        Query q1 = new TermQuery(new Term(fMeta, s1));
        Query q2 = NumericRangeQuery.newLongRange(fUpper, nStep, queryLower, null, true, true);
        bqNow.add(q1, BooleanClause.Occur.MUST);
        bqNow.add(q2, BooleanClause.Occur.MUST);
    }

    BooleanQuery bq = new BooleanQuery();
    bq.add(bqDeterminate, BooleanClause.Occur.SHOULD);
    bq.add(bqNow, BooleanClause.Occur.SHOULD);

    return bq;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for documents that are not
 * equal to the input time period.//from   w  w w.  j a v  a2s  .c o  m
 * @return the query
 */
private Query makeNotEquals() {
    Query qEquals = this.makeEquals();
    BooleanQuery qNotEquals = new BooleanQuery();
    qNotEquals.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    qNotEquals.add(qEquals, BooleanClause.Occur.MUST_NOT);
    return qNotEquals;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for documents that have a null time period.
 * @return the query/*ww  w. j a  v a 2  s. co m*/
 */
private Query makeNull() {
    int nStep = this.precisionStep;
    Query qHasIntervals = NumericRangeQuery.newLongRange(this.multiplicityFieldName, nStep, 1L, null, true,
            true);
    BooleanQuery qNull = new BooleanQuery();
    qNull.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    qNull.add(qHasIntervals, BooleanClause.Occur.MUST_NOT);
    return qNull;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for documents that are within the input time period.
 * @return the query//from   w  ww .j a  v  a2 s .c  o  m
 */
private Query makeWithin() {
    BooleanQuery bq = new BooleanQuery();
    Query q = this.makeWithinInterval(0);
    bq.add(q, BooleanClause.Occur.MUST);
    return bq;
}

From source file:com.esri.gpt.catalog.lucene.TimeperiodClauseAdapter.java

License:Apache License

/**
 * Constructs a query for a document interval that is within 
 * the input time period.//from w w  w  . j a  va  2 s.  c  om
 * @param interval the field name index for the interval
 * @return the query
 */
private Query makeWithinInterval(int interval) {

    // Within: docMin >= qryMin AND docMax <= qryMax

    int nStep = this.precisionStep;
    String fMeta = this.intervalMetaFieldName;
    String fLower = this.getLowerFieldName(interval);
    String fUpper = this.getUpperFieldName(interval);

    Query qDocLowerWithin = NumericRangeQuery.newLongRange(fLower, nStep, queryLower, queryUpper, true, true);
    Query qDocUpperWithin = NumericRangeQuery.newLongRange(fUpper, nStep, queryLower, queryUpper, true, true);

    BooleanQuery qIntervalWithin = new BooleanQuery();
    qIntervalWithin.add(qDocLowerWithin, BooleanClause.Occur.MUST);
    qIntervalWithin.add(qDocUpperWithin, BooleanClause.Occur.MUST);

    String sMeta = this.getMetaValue("determinate", interval);
    Query qIsDeterminate = new TermQuery(new Term(fMeta, sMeta));

    BooleanQuery bqDeterminate = new BooleanQuery();
    bqDeterminate.add(qIsDeterminate, BooleanClause.Occur.MUST);
    bqDeterminate.add(qIntervalWithin, BooleanClause.Occur.MUST);

    // intervals that intersect now
    BooleanQuery bqNow = null;

    if (this.queryIntersectsNow) {

        // meta term now.i and is within
        String s1 = this.getMetaValue("now", interval);
        Query q1 = new TermQuery(new Term(fMeta, s1));

        // meta term now.l.i and fUpper must be <= queryUpper
        String s2 = this.getMetaValue("now.l", interval);
        Query qM2 = new TermQuery(new Term(fMeta, s2));
        Query qI2 = NumericRangeQuery.newLongRange(fUpper, nStep, null, queryUpper, true, true);
        BooleanQuery q2 = new BooleanQuery();
        q2.add(qM2, BooleanClause.Occur.MUST);
        q2.add(qI2, BooleanClause.Occur.MUST);

        // meta term now.u.i and fLower must be >= queryLower
        String s3 = this.getMetaValue("now.u", interval);
        Query qM3 = new TermQuery(new Term(fMeta, s3));
        Query qI3 = NumericRangeQuery.newLongRange(fLower, nStep, queryLower, null, true, true);
        BooleanQuery q3 = new BooleanQuery();
        q3.add(qM3, BooleanClause.Occur.MUST);
        q3.add(qI3, BooleanClause.Occur.MUST);

        bqNow = new BooleanQuery();
        bqNow.add(q1, BooleanClause.Occur.SHOULD);
        bqNow.add(q2, BooleanClause.Occur.SHOULD);
        bqNow.add(q3, BooleanClause.Occur.SHOULD);
    } else if (this.queryIsBeforeNow) {
        // not within
    } else if (this.queryIsAfterNow) {
        // not within
    }

    if (bqNow == null) {
        return bqDeterminate;
    } else {
        BooleanQuery bq = new BooleanQuery();
        bq.add(bqDeterminate, BooleanClause.Occur.SHOULD);
        bq.add(bqNow, BooleanClause.Occur.SHOULD);
        return bq;
    }
}