All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stratio.cassandra.lucene.search.condition.BitemporalCondition Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stratio.cassandra.lucene.search.condition;

import com.google.common.base.MoreObjects;
import com.stratio.cassandra.lucene.schema.mapping.BitemporalMapper;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;

import static com.stratio.cassandra.lucene.schema.mapping.BitemporalMapper.BitemporalDateTime;
import static org.apache.lucene.search.BooleanClause.Occur.MUST;
import static org.apache.lucene.search.BooleanClause.Occur.SHOULD;
import static org.apache.lucene.search.NumericRangeQuery.newLongRange;

/**
 * A {@link Condition} implementation that matches bi-temporal (four) fields within two range of values.
 *
 * @author Eduardo Alonso {@literal }
 */
public class BitemporalCondition extends SingleMapperCondition {

    /** The default from value for vtFrom and ttFrom. */
    private static final Long DEFAULT_FROM = 0L;

    /** The default to value for vtTo and ttTo. */
    private static final Long DEFAULT_TO = Long.MAX_VALUE;

    /** The Valid Time Start. */
    final Object vtFrom;

    /** The Valid Time End. */
    final Object vtTo;

    /** The Transaction Time Start. */
    final Object ttFrom;

    /** The Transaction Time End. */
    final Object ttTo;

    /**
     * Constructs a query selecting all fields that intersects with valid time and transaction time ranges including
     * limits.
     *
     * @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal
     * weightings) have their score multiplied by {@code boost}.
     * @param field the name of the field to be matched
     * @param vtFrom the valid time start
     * @param vtTo the valid time end
     * @param ttFrom the transaction time start
     * @param ttTo the transaction time end
     */
    public BitemporalCondition(Float boost, String field, Object vtFrom, Object vtTo, Object ttFrom, Object ttTo) {
        super(boost, field, BitemporalMapper.class);
        this.vtFrom = vtFrom;
        this.vtTo = vtTo;
        this.ttFrom = ttFrom;
        this.ttTo = ttTo;
    }

    /** {@inheritDoc} */
    @Override
    public Query doQuery(BitemporalMapper mapper, Analyzer analyzer) {

        Long vtFromTime = parseTime(mapper, DEFAULT_FROM, vtFrom);
        Long vtToTime = parseTime(mapper, DEFAULT_TO, vtTo);
        Long ttFromTime = parseTime(mapper, DEFAULT_FROM, ttFrom);
        Long ttToTime = parseTime(mapper, DEFAULT_TO, ttTo);

        Long minTime = BitemporalDateTime.MIN.toTimestamp();
        Long maxTime = BitemporalDateTime.MAX.toTimestamp();

        BooleanQuery.Builder builder = new BooleanQuery.Builder();

        if (!((vtFromTime.equals(0L)) && (vtToTime.equals(Long.MAX_VALUE)))) {

            BooleanQuery.Builder validBuilder = new BooleanQuery.Builder();
            validBuilder.add(newLongRange(field + BitemporalMapper.VT_FROM_FIELD_SUFFIX,
                                          vtFromTime,
                                          vtToTime,
                                          true,
                                          true), SHOULD);
            validBuilder.add(newLongRange(field + BitemporalMapper.VT_TO_FIELD_SUFFIX,
                                          vtFromTime,
                                          vtToTime,
                                          true,
                                          true), SHOULD);

            BooleanQuery.Builder containsValidBuilder = new BooleanQuery.Builder();
            containsValidBuilder.add(newLongRange(field + BitemporalMapper.VT_FROM_FIELD_SUFFIX,
                                                  minTime,
                                                  vtFromTime,
                                                  true,
                                                  true), MUST);
            containsValidBuilder.add(newLongRange(field + BitemporalMapper.VT_TO_FIELD_SUFFIX,
                                                  vtToTime,
                                                  maxTime,
                                                  true,
                                                  true), MUST);
            validBuilder.add(containsValidBuilder.build(), SHOULD);
            builder.add(validBuilder.build(), MUST);
        }

        if (!((ttFromTime.equals(0L)) && (ttToTime.equals(Long.MAX_VALUE)))) {

            BooleanQuery.Builder transactionBuilder = new BooleanQuery.Builder();
            transactionBuilder.add(newLongRange(field + BitemporalMapper.TT_FROM_FIELD_SUFFIX,
                                                ttFromTime,
                                                ttToTime,
                                                true,
                                                true), SHOULD);
            transactionBuilder.add(newLongRange(field + BitemporalMapper.TT_TO_FIELD_SUFFIX,
                                                ttFromTime,
                                                ttToTime,
                                                true,
                                                true), SHOULD);

            BooleanQuery.Builder containsTransactionBuilder = new BooleanQuery.Builder();
            containsTransactionBuilder.add(newLongRange(field + BitemporalMapper.TT_FROM_FIELD_SUFFIX,
                                                        minTime,
                                                        ttFromTime,
                                                        true,
                                                        true), MUST);
            containsTransactionBuilder.add(newLongRange(field + BitemporalMapper.TT_TO_FIELD_SUFFIX,
                                                        ttToTime,
                                                        maxTime,
                                                        true,
                                                        true), MUST);
            transactionBuilder.add(containsTransactionBuilder.build(), SHOULD);
            builder.add(transactionBuilder.build(), MUST);
        }

        return builder.build();
    }

    private static Long parseTime(BitemporalMapper mapper, Long defaultTime, Object value) {
        return value == null
               ? new BitemporalDateTime(defaultTime).toTimestamp()
               : mapper.parseBitemporalDate(value).toTimestamp();
    }

    /** {@inheritDoc} */
    @Override
    public MoreObjects.ToStringHelper toStringHelper() {
        return toStringHelper(this).add("vtFrom", vtFrom)
                                   .add("vtTo", vtTo)
                                   .add("ttFrom", ttFrom)
                                   .add("ttTo", ttTo);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy