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);
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations