Search in sources :

Example 1 with Type

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

the class FuzzyCondition method query.

/**
     * {@inheritDoc}
     */
@Override
public Query query(Options schema) {
    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }
    if (maxEdits < 0 || maxEdits > 2) {
        throw new IllegalArgumentException("max_edits must be between 0 and 2");
    }
    if (prefixLength < 0) {
        throw new IllegalArgumentException("prefix_length must be positive.");
    }
    if (maxExpansions < 0) {
        throw new IllegalArgumentException("max_expansions must be positive.");
    }
    Properties properties = schema.getProperties(field);
    String message;
    Type fieldType = properties != null ? properties.getType() : Type.text;
    if (fieldType == Type.string || fieldType == Type.text) {
        String analyzedValue = analyze(field, value, schema.analyzer);
        Term term = new Term(field, analyzedValue);
        Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
        return query;
    }
    message = String.format("Fuzzy queries cannot be supported for field type %s", fieldType);
    throw new UnsupportedOperationException(message);
}
Also used : Type(com.tuplejump.stargate.lucene.Type) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) Properties(com.tuplejump.stargate.lucene.Properties)

Example 2 with Type

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

the class FuzzyCondition method getAutomaton.

@Override
public Automaton getAutomaton(Options schema) {
    Properties properties = schema.getProperties(field);
    String message;
    Type fieldType = properties != null ? properties.getType() : Type.text;
    if (fieldType == Type.string || fieldType == Type.text) {
        String analyzedValue = analyze(field, value, schema.analyzer);
        LevenshteinAutomata levenshteinAutomata = new LevenshteinAutomata(analyzedValue, transpositions);
        return levenshteinAutomata.toAutomaton(maxEdits);
    }
    message = String.format("Fuzzy queries cannot be supported for field type %s", fieldType);
    throw new UnsupportedOperationException(message);
}
Also used : Type(com.tuplejump.stargate.lucene.Type) LevenshteinAutomata(org.apache.lucene.util.automaton.LevenshteinAutomata) Properties(com.tuplejump.stargate.lucene.Properties)

Example 3 with Type

use of com.tuplejump.stargate.lucene.Type 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 4 with Type

use of com.tuplejump.stargate.lucene.Type 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 5 with Type

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

the class PhraseCondition method query.

/**
     * {@inheritDoc}
     */
@Override
public Query query(Options schema) {
    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (values == null) {
        throw new IllegalArgumentException("Field values required");
    }
    if (slop == null) {
        throw new IllegalArgumentException("Slop required");
    }
    if (slop < 0) {
        throw new IllegalArgumentException("Slop must be positive");
    }
    Properties properties = schema.getProperties(field);
    Type fieldType = properties != null ? properties.getType() : Type.text;
    if (fieldType.isCharSeq()) {
        Analyzer analyzer = schema.analyzer;
        PhraseQuery.Builder query = new PhraseQuery.Builder();
        query.setSlop(slop);
        int count = 0;
        for (String value : values) {
            if (value != null) {
                String analyzedValue = analyze(field, value, analyzer);
                if (analyzedValue != null) {
                    Term term = new Term(field, analyzedValue);
                    query.add(term, count);
                }
            }
            count++;
        }
        return query.build();
    }
    String message = String.format("Phrase queries cannot be supported until mapping is defined");
    throw new UnsupportedOperationException(message);
}
Also used : Type(com.tuplejump.stargate.lucene.Type) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) Properties(com.tuplejump.stargate.lucene.Properties) Analyzer(org.apache.lucene.analysis.Analyzer)

Aggregations

Type (com.tuplejump.stargate.lucene.Type)11 Properties (com.tuplejump.stargate.lucene.Properties)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 CQL3Type (org.apache.cassandra.cql3.CQL3Type)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 FieldType (org.apache.lucene.document.FieldType)2 Options (com.tuplejump.stargate.lucene.Options)1 ParseException (java.text.ParseException)1 ColumnDefinition (org.apache.cassandra.config.ColumnDefinition)1 PerFieldAnalyzerWrapper (org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 PrefixQuery (org.apache.lucene.search.PrefixQuery)1 RegexpQuery (org.apache.lucene.search.RegexpQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 WildcardQuery (org.apache.lucene.search.WildcardQuery)1