Search in sources :

Example 11 with Properties

use of com.tuplejump.stargate.lucene.Properties in project stargate-core by tuplejump.

the class CassandraUtils method addFieldType.

private static void addFieldType(String columnName, AbstractType validator, Properties properties, Map<String, NumericConfig> numericFieldOptions, Map<String, FieldType> fieldDocValueTypes, Map<String, FieldType> collectionFieldDocValueTypes, Map<String, FieldType> fieldTypes, Map<String, FieldType[]> collectionFieldTypes) {
    if (validator.isCollection()) {
        if (validator instanceof MapType) {
            properties.setType(Type.map);
            MapType mapType = (MapType) validator;
            AbstractType keyValidator = mapType.getKeysType();
            AbstractType valueValidator = mapType.getValuesType();
            Properties keyProps = properties.getFields().get("_key");
            Properties valueProps = properties.getFields().get("_value");
            if (keyProps == null) {
                keyProps = new Properties();
                keyProps.setAnalyzer(properties.getAnalyzer());
                properties.getFields().put("_key", keyProps);
            }
            if (valueProps == null) {
                valueProps = new Properties();
                valueProps.setAnalyzer(properties.getAnalyzer());
                properties.getFields().put("_value", valueProps);
            }
            setFromAbstractType(keyProps, keyValidator);
            setFromAbstractType(valueProps, valueValidator);
            FieldType keyFieldType = fieldType(keyProps, keyValidator);
            FieldType valueFieldType = fieldType(valueProps, valueValidator);
            if (valueProps.getStriped() == Properties.Striped.only || valueProps.getStriped() == Properties.Striped.also) {
                FieldType docValueType = LuceneUtils.docValueTypeFrom(valueFieldType);
                collectionFieldDocValueTypes.put(columnName, docValueType);
            }
            if (!(valueProps.getStriped() == Properties.Striped.only))
                collectionFieldTypes.put(columnName, new FieldType[] { keyFieldType, valueFieldType });
        } else if (validator instanceof ListType || validator instanceof SetType) {
            AbstractType elementValidator;
            if (validator instanceof SetType) {
                SetType setType = (SetType) validator;
                elementValidator = setType.getElementsType();
            } else {
                ListType listType = (ListType) validator;
                elementValidator = listType.getElementsType();
            }
            setFromAbstractType(properties, elementValidator);
            FieldType elementFieldType = fieldType(properties, elementValidator);
            if (properties.getStriped() == Properties.Striped.only || properties.getStriped() == Properties.Striped.also) {
                FieldType docValueType = LuceneUtils.docValueTypeFrom(elementFieldType);
                collectionFieldDocValueTypes.put(columnName, docValueType);
            }
            if (!(properties.getStriped() == Properties.Striped.only))
                collectionFieldTypes.put(columnName, new FieldType[] { elementFieldType });
        }
    } else {
        setFromAbstractType(properties, validator);
        FieldType fieldType = fieldType(properties, validator);
        if (fieldType.numericType() != null) {
            numericFieldOptions.put(columnName, LuceneUtils.numericConfig(fieldType));
        }
        if (properties.getStriped() == Properties.Striped.only || properties.getStriped() == Properties.Striped.also) {
            FieldType docValueType = LuceneUtils.docValueTypeFrom(fieldType);
            fieldDocValueTypes.put(columnName, docValueType);
        }
        if (properties.getStriped() != Properties.Striped.only)
            fieldTypes.put(columnName, fieldType);
    }
}
Also used : Properties(com.tuplejump.stargate.lucene.Properties) FieldType(org.apache.lucene.document.FieldType)

Example 12 with Properties

use of com.tuplejump.stargate.lucene.Properties in project stargate-core by tuplejump.

the class MatchCondition method query.

/**
     * {@inheritDoc}
     */
@Override
public Query query(Options schema) throws Exception {
    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value instanceof String && ((String) value).trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }
    NumericConfig numericConfig = schema.numericFieldOptions.get(field);
    Properties properties = schema.getProperties(field);
    Type fieldType = properties != null ? properties.getType() : Type.text;
    Query query;
    if (fieldType.isCharSeq() || fieldType == Type.bool) {
        String analyzedValue = analyze(field, value.toString(), schema.analyzer);
        if (analyzedValue == null) {
            throw new IllegalArgumentException("Value discarded by analyzer");
        }
        Term term = new Term(field, analyzedValue);
        query = new TermQuery(term);
    } else if (fieldType == Type.integer) {
        assert numericConfig != null;
        Integer value = numericConfig.getNumberFormat().parse(this.value.toString()).intValue();
        query = NumericRangeQuery.newIntRange(field, value, value, true, true);
    } else if (fieldType == Type.bigint || fieldType == Type.date) {
        assert numericConfig != null;
        Long value = numericConfig.getNumberFormat().parse(this.value.toString()).longValue();
        query = NumericRangeQuery.newLongRange(field, value, value, true, true);
    } else if (fieldType == Type.decimal) {
        assert numericConfig != null;
        Float value = numericConfig.getNumberFormat().parse(this.value.toString()).floatValue();
        query = NumericRangeQuery.newFloatRange(field, value, value, true, true);
    } else if (fieldType == Type.bigdecimal) {
        assert numericConfig != null;
        Double value = numericConfig.getNumberFormat().parse(this.value.toString()).doubleValue();
        query = NumericRangeQuery.newDoubleRange(field, value, value, true, true);
    } else {
        String message = String.format("Match queries are not supported by %s field type", fieldType);
        throw new UnsupportedOperationException(message);
    }
    return query;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Properties(com.tuplejump.stargate.lucene.Properties) Type(com.tuplejump.stargate.lucene.Type) NumericConfig(org.apache.lucene.queryparser.flexible.standard.config.NumericConfig)

Example 13 with Properties

use of com.tuplejump.stargate.lucene.Properties in project stargate-core by tuplejump.

the class PrefixCondition method query.

/**
     * {@inheritDoc}
     */
@Override
public Query query(Options schema) {
    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null) {
        throw new IllegalArgumentException("Field value required");
    }
    Query query;
    Properties properties = schema.getProperties(field);
    Type fieldType = properties != null ? properties.getType() : Type.text;
    if (fieldType.isCharSeq()) {
        Term term = new Term(field, value);
        query = new PrefixQuery(term);
    } else {
        String message = String.format("Prefix queries are not supported by %s mapper", fieldType);
        throw new UnsupportedOperationException(message);
    }
    return query;
}
Also used : Type(com.tuplejump.stargate.lucene.Type) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term) Properties(com.tuplejump.stargate.lucene.Properties)

Aggregations

Properties (com.tuplejump.stargate.lucene.Properties)13 Type (com.tuplejump.stargate.lucene.Type)9 Term (org.apache.lucene.index.Term)6 Query (org.apache.lucene.search.Query)5 NumericConfig (org.apache.lucene.queryparser.flexible.standard.config.NumericConfig)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 FieldType (org.apache.lucene.document.FieldType)2 JsonField (argo.jdom.JsonField)1 JsonNode (argo.jdom.JsonNode)1 Options (com.tuplejump.stargate.lucene.Options)1 JsonDocument (com.tuplejump.stargate.lucene.json.JsonDocument)1 StreamingJsonDocument (com.tuplejump.stargate.lucene.json.StreamingJsonDocument)1 ParseException (java.text.ParseException)1 ColumnDefinition (org.apache.cassandra.config.ColumnDefinition)1 CQL3Type (org.apache.cassandra.cql3.CQL3Type)1 PerFieldAnalyzerWrapper (org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper)1 Field (org.apache.lucene.document.Field)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)1