Search in sources :

Example 1 with QueryExpression

use of org.polymap.recordstore.QueryExpression in project polymap4-core by Polymap4.

the class NumericValueCoder method searchQuery.

public Query searchQuery(QueryExpression exp) {
    // EQUALS
    if (exp instanceof QueryExpression.Equal) {
        Equal equalExp = (QueryExpression.Equal) exp;
        if (equalExp.value instanceof Number) {
            String key = equalExp.key;
            Number value = (Number) equalExp.value;
            if (equalExp.value instanceof Integer) {
                return NumericRangeQuery.newIntRange(key, value.intValue(), value.intValue(), true, true);
            } else if (equalExp.value instanceof Long) {
                return NumericRangeQuery.newLongRange(key, value.longValue(), value.longValue(), true, true);
            } else if (equalExp.value instanceof Float) {
                return NumericRangeQuery.newFloatRange(key, value.floatValue(), value.floatValue(), true, true);
            } else if (equalExp.value instanceof Double) {
                return NumericRangeQuery.newDoubleRange(key, value.doubleValue(), value.doubleValue(), true, true);
            } else {
                throw new RuntimeException("Unknown Number type: " + value.getClass());
            }
        }
    } else // GREATER
    if (exp instanceof QueryExpression.Greater) {
        Greater greaterExp = (QueryExpression.Greater) exp;
        if (greaterExp.value instanceof Number) {
            String key = greaterExp.key;
            Number value = (Number) greaterExp.value;
            if (greaterExp.value instanceof Integer) {
                return NumericRangeQuery.newIntRange(key, value.intValue(), null, false, false);
            } else if (greaterExp.value instanceof Long) {
                return NumericRangeQuery.newLongRange(key, value.longValue(), null, false, false);
            } else if (greaterExp.value instanceof Float) {
                return NumericRangeQuery.newFloatRange(key, value.floatValue(), null, false, false);
            } else if (greaterExp.value instanceof Double) {
                return NumericRangeQuery.newDoubleRange(key, value.doubleValue(), null, false, false);
            } else {
                throw new RuntimeException("Unknown Number type: " + value.getClass());
            }
        }
    } else // GREATER OR EQUAL
    if (exp instanceof QueryExpression.GreaterOrEqual) {
        GreaterOrEqual greaterExp = (QueryExpression.GreaterOrEqual) exp;
        if (greaterExp.value instanceof Number) {
            String key = greaterExp.key;
            Number value = (Number) greaterExp.value;
            if (greaterExp.value instanceof Integer) {
                return NumericRangeQuery.newIntRange(key, value.intValue(), null, true, false);
            } else if (greaterExp.value instanceof Long) {
                return NumericRangeQuery.newLongRange(key, value.longValue(), null, true, false);
            } else if (greaterExp.value instanceof Float) {
                return NumericRangeQuery.newFloatRange(key, value.floatValue(), null, true, false);
            } else if (greaterExp.value instanceof Double) {
                return NumericRangeQuery.newDoubleRange(key, value.doubleValue(), null, true, false);
            } else {
                throw new RuntimeException("Unknown Number type: " + value.getClass());
            }
        }
    } else // LESS
    if (exp instanceof QueryExpression.Less) {
        Less lessExp = (QueryExpression.Less) exp;
        if (lessExp.value instanceof Number) {
            String key = lessExp.key;
            Number value = (Number) lessExp.value;
            if (lessExp.value instanceof Integer) {
                return NumericRangeQuery.newIntRange(key, null, value.intValue(), false, false);
            } else if (lessExp.value instanceof Long) {
                return NumericRangeQuery.newLongRange(key, null, value.longValue(), false, false);
            } else if (lessExp.value instanceof Float) {
                return NumericRangeQuery.newFloatRange(key, null, value.floatValue(), false, false);
            } else if (lessExp.value instanceof Double) {
                return NumericRangeQuery.newDoubleRange(key, null, value.doubleValue(), false, false);
            } else {
                throw new RuntimeException("Unknown Number type: " + value.getClass());
            }
        }
    } else // LESS or equal
    if (exp instanceof QueryExpression.LessOrEqual) {
        LessOrEqual lessExp = (QueryExpression.LessOrEqual) exp;
        if (lessExp.value instanceof Number) {
            String key = lessExp.key;
            Number value = (Number) lessExp.value;
            if (lessExp.value instanceof Integer) {
                return NumericRangeQuery.newIntRange(key, null, value.intValue(), false, true);
            } else if (lessExp.value instanceof Long) {
                return NumericRangeQuery.newLongRange(key, null, value.longValue(), false, true);
            } else if (lessExp.value instanceof Float) {
                return NumericRangeQuery.newFloatRange(key, null, value.floatValue(), false, true);
            } else if (lessExp.value instanceof Double) {
                return NumericRangeQuery.newDoubleRange(key, null, value.doubleValue(), false, true);
            } else {
                throw new RuntimeException("Unknown Number type: " + value.getClass());
            }
        }
    } else // MATCHES
    if (exp instanceof QueryExpression.Match) {
        Match matchExp = (Match) exp;
        if (matchExp.value instanceof Number) {
            throw new UnsupportedOperationException("MATCHES not supported for Number values.");
        }
    }
    return null;
}
Also used : Less(org.polymap.recordstore.QueryExpression.Less) LessOrEqual(org.polymap.recordstore.QueryExpression.LessOrEqual) GreaterOrEqual(org.polymap.recordstore.QueryExpression.GreaterOrEqual) Match(org.polymap.recordstore.QueryExpression.Match) Greater(org.polymap.recordstore.QueryExpression.Greater) Match(org.polymap.recordstore.QueryExpression.Match) BigInteger(java.math.BigInteger) Equal(org.polymap.recordstore.QueryExpression.Equal) LessOrEqual(org.polymap.recordstore.QueryExpression.LessOrEqual) GreaterOrEqual(org.polymap.recordstore.QueryExpression.GreaterOrEqual) Greater(org.polymap.recordstore.QueryExpression.Greater) QueryExpression(org.polymap.recordstore.QueryExpression) Less(org.polymap.recordstore.QueryExpression.Less)

Example 2 with QueryExpression

use of org.polymap.recordstore.QueryExpression in project polymap4-core by Polymap4.

the class LuceneRecordStore method find.

@Override
public ResultSet find(RecordQuery query) throws Exception {
    assert !isClosed() : "Store is closed already.";
    // SimpleQuery
    if (query instanceof SimpleQuery) {
        Query luceneQuery = null;
        Collection<QueryExpression> expressions = ((SimpleQuery) query).expressions();
        if (expressions.isEmpty()) {
            luceneQuery = new MatchAllDocsQuery();
        } else {
            luceneQuery = new BooleanQuery();
            for (QueryExpression exp : expressions) {
                ((BooleanQuery) luceneQuery).add(valueCoders.searchQuery(exp), BooleanClause.Occur.MUST);
            }
        }
        return new LuceneRecordQuery(this, luceneQuery).setMaxResults(query.getMaxResults()).setFirstResult(query.getFirstResult()).sort(query.getSortKey(), query.getSortOrder(), query.getSortType()).execute();
    } else // other
    {
        return query.execute();
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SimpleQuery(org.polymap.recordstore.SimpleQuery) Query(org.apache.lucene.search.Query) RecordQuery(org.polymap.recordstore.RecordQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SimpleQuery(org.polymap.recordstore.SimpleQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryExpression(org.polymap.recordstore.QueryExpression) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

QueryExpression (org.polymap.recordstore.QueryExpression)2 BigInteger (java.math.BigInteger)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 Equal (org.polymap.recordstore.QueryExpression.Equal)1 Greater (org.polymap.recordstore.QueryExpression.Greater)1 GreaterOrEqual (org.polymap.recordstore.QueryExpression.GreaterOrEqual)1 Less (org.polymap.recordstore.QueryExpression.Less)1 LessOrEqual (org.polymap.recordstore.QueryExpression.LessOrEqual)1 Match (org.polymap.recordstore.QueryExpression.Match)1 RecordQuery (org.polymap.recordstore.RecordQuery)1 SimpleQuery (org.polymap.recordstore.SimpleQuery)1