Search in sources :

Example 1 with NumericConfig

use of org.apache.lucene.queryparser.flexible.standard.config.NumericConfig in project stargate-core by tuplejump.

the class CassandraUtils method getOptions.

public static Options getOptions(Properties mapping, ColumnFamilyStore baseCfs, String colName) {
    Map<String, NumericConfig> numericFieldOptions = new HashMap<>();
    Map<String, FieldType> fieldDocValueTypes = new TreeMap<>();
    Map<String, FieldType> collectionFieldDocValueTypes = new TreeMap<>();
    Map<String, FieldType> fieldTypes = new TreeMap<>();
    Map<String, FieldType[]> collectionFieldTypes = new TreeMap<>();
    Map<String, ColumnDefinition> validators = new TreeMap<>();
    Map<String, ColumnDefinition> clusteringKeysIndexed = new LinkedHashMap<>();
    Map<String, ColumnDefinition> partitionKeysIndexed = new LinkedHashMap<>();
    Set<String> indexedColumnNames;
    //getForRow all the fields options.
    indexedColumnNames = new TreeSet<>();
    indexedColumnNames.addAll(mapping.getFields().keySet());
    Set<String> added = new HashSet<>(indexedColumnNames.size());
    List<ColumnDefinition> partitionKeys = baseCfs.metadata.partitionKeyColumns();
    List<ColumnDefinition> clusteringKeys = baseCfs.metadata.clusteringColumns();
    for (ColumnDefinition colDef : partitionKeys) {
        String columnName = colDef.name.toString();
        if (Options.logger.isDebugEnabled()) {
            Options.logger.debug("Partition key name is {} and index is {}", colName, colDef.position());
        }
        validators.put(columnName, colDef);
        if (indexedColumnNames.contains(columnName)) {
            partitionKeysIndexed.put(colName, colDef);
            addPropertiesAndFieldType(mapping, numericFieldOptions, fieldDocValueTypes, collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes, added, colDef, columnName);
        }
    }
    for (ColumnDefinition colDef : clusteringKeys) {
        String columnName = colDef.name.toString();
        if (Options.logger.isDebugEnabled()) {
            Options.logger.debug("Clustering key name is {} and index is {}", colName, colDef.position() + 1);
        }
        validators.put(columnName, colDef);
        if (indexedColumnNames.contains(columnName)) {
            clusteringKeysIndexed.put(columnName, colDef);
            addPropertiesAndFieldType(mapping, numericFieldOptions, fieldDocValueTypes, collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes, added, colDef, columnName);
        }
    }
    for (String columnName : indexedColumnNames) {
        if (added.add(columnName.toLowerCase())) {
            Properties options = mapping.getFields().get(columnName);
            ColumnDefinition colDef = getColumnDefinition(baseCfs, columnName);
            if (colDef != null) {
                validators.put(columnName, colDef);
                addFieldType(columnName, colDef.type, options, numericFieldOptions, fieldDocValueTypes, collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes);
            } else {
                throw new IllegalArgumentException(String.format("Column Definition for %s not found", columnName));
            }
            if (options.getType() == Type.object) {
                mapping.getFields().putAll(options.getFields());
            }
        }
    }
    Set<ColumnDefinition> otherColumns = baseCfs.metadata.regularColumns();
    for (ColumnDefinition colDef : otherColumns) {
        String columnName = UTF8Type.instance.getString(colDef.name.bytes);
        validators.put(columnName, colDef);
    }
    numericFieldOptions.putAll(mapping.getDynamicNumericConfig());
    Analyzer defaultAnalyzer = mapping.getLuceneAnalyzer();
    Analyzer analyzer = new PerFieldAnalyzerWrapper(defaultAnalyzer, mapping.perFieldAnalyzers());
    Map<String, Type> types = new TreeMap<>();
    Set<String> nestedFields = new TreeSet<>();
    for (Map.Entry<String, ColumnDefinition> entry : validators.entrySet()) {
        CQL3Type cql3Type = entry.getValue().type.asCQL3Type();
        AbstractType inner = getValueValidator(cql3Type.getType());
        if (cql3Type.isCollection()) {
            types.put(entry.getKey(), fromAbstractType(inner.asCQL3Type()));
            nestedFields.add(entry.getKey());
        } else {
            types.put(entry.getKey(), fromAbstractType(cql3Type));
        }
    }
    return new Options(mapping, numericFieldOptions, fieldDocValueTypes, collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes, types, nestedFields, clusteringKeysIndexed, partitionKeysIndexed, indexedColumnNames, analyzer, colName);
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) Options(com.tuplejump.stargate.lucene.Options) Properties(com.tuplejump.stargate.lucene.Properties) Analyzer(org.apache.lucene.analysis.Analyzer) FieldType(org.apache.lucene.document.FieldType) ColumnDefinition(org.apache.cassandra.config.ColumnDefinition) PerFieldAnalyzerWrapper(org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper) Type(com.tuplejump.stargate.lucene.Type) FieldType(org.apache.lucene.document.FieldType) CQL3Type(org.apache.cassandra.cql3.CQL3Type) NumericConfig(org.apache.lucene.queryparser.flexible.standard.config.NumericConfig)

Example 2 with NumericConfig

use of org.apache.lucene.queryparser.flexible.standard.config.NumericConfig in project stargate-core by tuplejump.

the class MatchCondition method getAutomaton.

@Override
public Automaton getAutomaton(Options schema) {
    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");
    }
    try {
        NumericConfig numericConfig = schema.numericFieldOptions.get(field);
        Properties properties = schema.getProperties(field);
        Type fieldType = properties != null ? properties.getType() : Type.text;
        if (fieldType.isCharSeq()) {
            String analyzedValue = analyze(field, value.toString(), schema.analyzer);
            if (analyzedValue == null) {
                throw new IllegalArgumentException("Value discarded by analyzer");
            }
            return Automata.makeString(analyzedValue);
        } else if (fieldType == Type.integer) {
            assert numericConfig != null;
            Integer value = null;
            value = numericConfig.getNumberFormat().parse(this.value.toString()).intValue();
            return Automata.makeString(value.toString());
        } else if (fieldType == Type.bigint || fieldType == Type.date) {
            assert numericConfig != null;
            Long value = numericConfig.getNumberFormat().parse(this.value.toString()).longValue();
            return Automata.makeString(value.toString());
        } else if (fieldType == Type.decimal) {
            assert numericConfig != null;
            Float value = numericConfig.getNumberFormat().parse(this.value.toString()).floatValue();
            return Automata.makeString(value.toString());
        } else if (fieldType == Type.bigdecimal) {
            assert numericConfig != null;
            Double value = numericConfig.getNumberFormat().parse(this.value.toString()).doubleValue();
            return Automata.makeString(value.toString());
        } else {
            String message = String.format("Match pattern queries are not supported by %s mapper", fieldType);
            throw new UnsupportedOperationException(message);
        }
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : Type(com.tuplejump.stargate.lucene.Type) NumericConfig(org.apache.lucene.queryparser.flexible.standard.config.NumericConfig) ParseException(java.text.ParseException) Properties(com.tuplejump.stargate.lucene.Properties)

Example 3 with NumericConfig

use of org.apache.lucene.queryparser.flexible.standard.config.NumericConfig 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 4 with NumericConfig

use of org.apache.lucene.queryparser.flexible.standard.config.NumericConfig in project stargate-core by tuplejump.

the class RangeCondition method query.

/**
     * {@inheritDoc}
     */
@Override
public Query query(Options schema) throws Exception {
    Query query;
    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    NumericConfig numericConfig = schema.numericFieldOptions.get(field);
    Properties properties = schema.getProperties(field);
    Type fieldType = properties != null ? properties.getType() : Type.text;
    //TODO Range on TimeUUID type
    if (fieldType.isCharSeq()) {
        String lowerVal = null, upperVal = null;
        if (this.lower != null) {
            lowerVal = analyze(field, this.lower.toString(), schema.analyzer);
        }
        if (this.upper != null) {
            upperVal = analyze(field, this.upper.toString(), schema.analyzer);
        }
        query = TermRangeQuery.newStringRange(field, lowerVal, upperVal, includeLower, includeUpper);
    } else if (fieldType == Type.integer) {
        assert numericConfig != null;
        Integer lower = this.lower == null ? Integer.MIN_VALUE : numericConfig.getNumberFormat().parse(this.lower.toString()).intValue();
        Integer upper = this.upper == null ? Integer.MAX_VALUE : numericConfig.getNumberFormat().parse(this.upper.toString()).intValue();
        query = NumericRangeQuery.newIntRange(field, lower, upper, includeLower, includeUpper);
    } else if (fieldType == Type.bigint) {
        assert numericConfig != null;
        Long lower = this.lower == null ? Long.MIN_VALUE : numericConfig.getNumberFormat().parse(this.lower.toString()).longValue();
        Long upper = this.upper == null ? Long.MAX_VALUE : numericConfig.getNumberFormat().parse(this.upper.toString()).longValue();
        query = NumericRangeQuery.newLongRange(field, lower, upper, includeLower, includeUpper);
    } else if (fieldType == Type.decimal) {
        assert numericConfig != null;
        Float lower = this.lower == null ? Float.MIN_VALUE : numericConfig.getNumberFormat().parse(this.lower.toString()).floatValue();
        Float upper = this.upper == null ? Float.MAX_VALUE : numericConfig.getNumberFormat().parse(this.upper.toString()).floatValue();
        query = NumericRangeQuery.newFloatRange(field, lower, upper, includeLower, includeUpper);
    } else if (fieldType == Type.bigdecimal) {
        assert numericConfig != null;
        Double lower = this.lower == null ? Double.MIN_VALUE : numericConfig.getNumberFormat().parse(this.lower.toString()).doubleValue();
        Double upper = this.upper == null ? Double.MAX_VALUE : numericConfig.getNumberFormat().parse(this.upper.toString()).doubleValue();
        query = NumericRangeQuery.newDoubleRange(field, lower, upper, includeLower, includeUpper);
    } else if (fieldType == Type.date) {
        Long lower;
        Long upper;
        if ("millis".equals(format)) {
            lower = this.lower == null ? Long.MIN_VALUE : Long.valueOf(this.lower.toString());
            upper = this.upper == null ? Long.MAX_VALUE : Long.valueOf(this.upper.toString());
        } else {
            FormatDateTimeFormatter formatter = Dates.forPattern(format, Locale.getDefault());
            DateTimeFormatter parser = formatter.parser();
            lower = this.lower == null ? Long.MIN_VALUE : parser.parseMillis(this.lower.toString());
            upper = this.upper == null ? Long.MAX_VALUE : parser.parseMillis(this.upper.toString());
        }
        query = NumericRangeQuery.newLongRange(field, lower, upper, includeLower, includeUpper);
    } else {
        String message = String.format("Range queries are not supported by %s mapper", fieldType);
        throw new UnsupportedOperationException(message);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) NumericConfig(org.apache.lucene.queryparser.flexible.standard.config.NumericConfig) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Aggregations

NumericConfig (org.apache.lucene.queryparser.flexible.standard.config.NumericConfig)4 Properties (com.tuplejump.stargate.lucene.Properties)3 Type (com.tuplejump.stargate.lucene.Type)3 NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)2 Query (org.apache.lucene.search.Query)2 Options (com.tuplejump.stargate.lucene.Options)1 ParseException (java.text.ParseException)1 ColumnDefinition (org.apache.cassandra.config.ColumnDefinition)1 CQL3Type (org.apache.cassandra.cql3.CQL3Type)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 PerFieldAnalyzerWrapper (org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper)1 FieldType (org.apache.lucene.document.FieldType)1 Term (org.apache.lucene.index.Term)1 TermQuery (org.apache.lucene.search.TermQuery)1 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1