use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.
the class ElasticSearchIndex method register.
@Override
public void register(String store, String key, KeyInformation information, TransactionHandle tx) throws StorageException {
XContentBuilder mapping = null;
Class<?> dataType = information.getDataType();
Mapping map = Mapping.getMapping(information);
Preconditions.checkArgument(map == Mapping.DEFAULT || AttributeUtil.isString(dataType), "Specified illegal mapping [%s] for data type [%s]", map, dataType);
try {
mapping = XContentFactory.jsonBuilder().startObject().startObject(store).startObject("properties").startObject(key);
if (AttributeUtil.isString(dataType)) {
log.debug("Registering string type for {}", key);
mapping.field("type", "string");
if (map == Mapping.STRING)
mapping.field("index", "not_analyzed");
} else if (dataType == Float.class || dataType == FullFloat.class) {
log.debug("Registering float type for {}", key);
mapping.field("type", "float");
} else if (dataType == Double.class || dataType == FullDouble.class) {
log.debug("Registering double type for {}", key);
mapping.field("type", "double");
} else if (dataType == Byte.class) {
log.debug("Registering byte type for {}", key);
mapping.field("type", "byte");
} else if (dataType == Short.class) {
log.debug("Registering short type for {}", key);
mapping.field("type", "short");
} else if (dataType == Integer.class) {
log.debug("Registering integer type for {}", key);
mapping.field("type", "integer");
} else if (dataType == Long.class) {
log.debug("Registering long type for {}", key);
mapping.field("type", "long");
} else if (dataType == Boolean.class) {
log.debug("Registering boolean type for {}", key);
mapping.field("type", "boolean");
} else if (dataType == Geoshape.class) {
log.debug("Registering geo_point type for {}", key);
mapping.field("type", "geo_point");
}
mapping.endObject().endObject().endObject().endObject();
} catch (IOException e) {
throw new PermanentStorageException("Could not render json for put mapping request", e);
}
try {
PutMappingResponse response = client.admin().indices().preparePutMapping(indexName).setIgnoreConflicts(false).setType(store).setSource(mapping).execute().actionGet();
} catch (Exception e) {
throw convert(e);
}
}
use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.
the class ElasticSearchIndex method getFilter.
public FilterBuilder getFilter(Condition<?> condition, KeyInformation.StoreRetriever informations) {
if (condition instanceof PredicateCondition) {
PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
String key = atom.getKey();
TitanPredicate titanPredicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
Cmp numRel = (Cmp) titanPredicate;
Preconditions.checkArgument(value instanceof Number);
switch(numRel) {
case EQUAL:
return FilterBuilders.inFilter(key, value);
case NOT_EQUAL:
return FilterBuilders.notFilter(FilterBuilders.inFilter(key, value));
case LESS_THAN:
return FilterBuilders.rangeFilter(key).lt(value);
case LESS_THAN_EQUAL:
return FilterBuilders.rangeFilter(key).lte(value);
case GREATER_THAN:
return FilterBuilders.rangeFilter(key).gt(value);
case GREATER_THAN_EQUAL:
return FilterBuilders.rangeFilter(key).gte(value);
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof String) {
Mapping map = Mapping.getMapping(informations.get(key));
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
if (map == Mapping.STRING && titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
if (titanPredicate == Text.CONTAINS) {
value = ((String) value).toLowerCase();
return FilterBuilders.termFilter(key, (String) value);
} else if (titanPredicate == Text.CONTAINS_PREFIX) {
value = ((String) value).toLowerCase();
return FilterBuilders.prefixFilter(key, (String) value);
} else if (titanPredicate == Text.CONTAINS_REGEX) {
value = ((String) value).toLowerCase();
return FilterBuilders.regexpFilter(key, (String) value);
} else if (titanPredicate == Text.PREFIX) {
return FilterBuilders.prefixFilter(key, (String) value);
} else if (titanPredicate == Text.REGEX) {
return FilterBuilders.regexpFilter(key, (String) value);
} else if (titanPredicate == Cmp.EQUAL) {
return FilterBuilders.termFilter(key, (String) value);
} else if (titanPredicate == Cmp.NOT_EQUAL) {
return FilterBuilders.notFilter(FilterBuilders.termFilter(key, (String) value));
} else
throw new IllegalArgumentException("Predicate is not supported for string value: " + titanPredicate);
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(titanPredicate == Geo.WITHIN, "Relation is not supported for geo value: " + titanPredicate);
Geoshape shape = (Geoshape) value;
if (shape.getType() == Geoshape.Type.CIRCLE) {
Geoshape.Point center = shape.getPoint();
return FilterBuilders.geoDistanceFilter(key).lat(center.getLatitude()).lon(center.getLongitude()).distance(shape.getRadius(), DistanceUnit.KILOMETERS);
} else if (shape.getType() == Geoshape.Type.BOX) {
Geoshape.Point southwest = shape.getPoint(0);
Geoshape.Point northeast = shape.getPoint(1);
return FilterBuilders.geoBoundingBoxFilter(key).bottomRight(southwest.getLatitude(), northeast.getLongitude()).topLeft(northeast.getLatitude(), southwest.getLongitude());
} else
throw new IllegalArgumentException("Unsupported or invalid search shape type: " + shape.getType());
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
return FilterBuilders.notFilter(getFilter(((Not) condition).getChild(), informations));
} else if (condition instanceof And) {
AndFilterBuilder b = FilterBuilders.andFilter();
for (Condition c : condition.getChildren()) {
b.add(getFilter(c, informations));
}
return b;
} else if (condition instanceof Or) {
OrFilterBuilder b = FilterBuilders.orFilter();
for (Condition c : condition.getChildren()) {
b.add(getFilter(c, informations));
}
return b;
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
}
use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.
the class LuceneIndex method supports.
@Override
public boolean supports(KeyInformation information, TitanPredicate titanPredicate) {
Class<?> dataType = information.getDataType();
Mapping mapping = Mapping.getMapping(information);
if (mapping != Mapping.DEFAULT && !AttributeUtil.isString(dataType))
return false;
if (Number.class.isAssignableFrom(dataType)) {
if (titanPredicate instanceof Cmp)
return true;
} else if (dataType == Geoshape.class) {
return titanPredicate == Geo.WITHIN;
} else if (AttributeUtil.isString(dataType)) {
switch(mapping) {
case DEFAULT:
case TEXT:
// || titanPredicate == Text.CONTAINS_REGEX;
return titanPredicate == Text.CONTAINS || titanPredicate == Text.CONTAINS_PREFIX;
case STRING:
// || titanPredicate==Text.REGEX
return titanPredicate == Cmp.EQUAL || titanPredicate == Cmp.NOT_EQUAL || titanPredicate == Text.PREFIX;
}
}
return false;
}
use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.
the class LuceneIndex method supports.
@Override
public boolean supports(KeyInformation information) {
Class<?> dataType = information.getDataType();
Mapping mapping = Mapping.getMapping(information);
if (Number.class.isAssignableFrom(dataType) || dataType == Geoshape.class) {
if (mapping == Mapping.DEFAULT)
return true;
} else if (AttributeUtil.isString(dataType)) {
if (mapping == Mapping.DEFAULT || mapping == Mapping.STRING || mapping == Mapping.TEXT)
return true;
}
return false;
}
Aggregations