Search in sources :

Example 1 with Mapping

use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.

the class LuceneIndex method mutateStores.

private void mutateStores(Map.Entry<String, Map<String, IndexMutation>> stores, KeyInformation.IndexRetriever informations) throws StorageException, IOException {
    IndexReader reader = null;
    try {
        String storename = stores.getKey();
        IndexWriter writer = getWriter(storename);
        reader = DirectoryReader.open(writer, true);
        IndexSearcher searcher = new IndexSearcher(reader);
        for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
            String docid = entry.getKey();
            IndexMutation mutation = entry.getValue();
            Term docTerm = new Term(DOCID, docid);
            if (mutation.isDeleted()) {
                log.trace("Deleted entire document [{}]", docid);
                writer.deleteDocuments(docTerm);
                continue;
            }
            Document doc = null;
            TopDocs hits = searcher.search(new TermQuery(docTerm), 10);
            Map<String, Shape> geofields = Maps.newHashMap();
            if (hits.scoreDocs.length == 0) {
                log.trace("Creating new document for [{}]", docid);
                doc = new Document();
                Field docidField = new StringField(DOCID, docid, Field.Store.YES);
                doc.add(docidField);
            } else if (hits.scoreDocs.length > 1) {
                throw new IllegalArgumentException("More than one document found for document id: " + docid);
            } else {
                log.trace("Updating existing document for [{}]", docid);
                int docId = hits.scoreDocs[0].doc;
                // retrieve the old document
                doc = searcher.doc(docId);
                for (IndexableField field : doc.getFields()) {
                    if (field.stringValue().startsWith(GEOID)) {
                        geofields.put(field.name(), ctx.readShape(field.stringValue().substring(GEOID.length())));
                    }
                }
            }
            Preconditions.checkNotNull(doc);
            for (String key : mutation.getDeletions()) {
                if (doc.getField(key) != null) {
                    log.trace("Removing field [{}] on document [{}]", key, docid);
                    doc.removeFields(key);
                    geofields.remove(key);
                }
            }
            for (IndexEntry add : mutation.getAdditions()) {
                log.trace("Adding field [{}] on document [{}]", add.key, docid);
                if (doc.getField(add.key) != null)
                    doc.removeFields(add.key);
                if (add.value instanceof Number) {
                    Field field = null;
                    if (AttributeUtil.isWholeNumber((Number) add.value)) {
                        field = new LongField(add.key, ((Number) add.value).longValue(), Field.Store.YES);
                    } else {
                        // double or float
                        field = new DoubleField(add.key, ((Number) add.value).doubleValue(), Field.Store.YES);
                    }
                    doc.add(field);
                } else if (AttributeUtil.isString(add.value)) {
                    String str = (String) add.value;
                    Mapping mapping = Mapping.getMapping(storename, add.key, informations);
                    Field field;
                    switch(mapping) {
                        case DEFAULT:
                        case TEXT:
                            field = new TextField(add.key, str, Field.Store.YES);
                            break;
                        case STRING:
                            field = new StringField(add.key, str, Field.Store.YES);
                            break;
                        default:
                            throw new IllegalArgumentException("Illegal mapping specified: " + mapping);
                    }
                    doc.add(field);
                } else if (add.value instanceof Geoshape) {
                    Shape shape = ((Geoshape) add.value).convert2Spatial4j();
                    geofields.put(add.key, shape);
                    doc.add(new StoredField(add.key, GEOID + ctx.toString(shape)));
                } else
                    throw new IllegalArgumentException("Unsupported type: " + add.value);
            }
            for (Map.Entry<String, Shape> geo : geofields.entrySet()) {
                log.trace("Updating geo-indexes for key {}", geo.getKey());
                for (IndexableField f : getSpatialStrategy(geo.getKey()).createIndexableFields(geo.getValue())) {
                    doc.add(f);
                }
            }
            // write the old document to the index with the modifications
            writer.updateDocument(new Term(DOCID, docid), doc);
        }
        writer.commit();
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
Also used : Shape(com.spatial4j.core.shape.Shape) Mapping(com.thinkaurelius.titan.core.Mapping) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Mapping

use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.

the class LuceneIndex method convertQuery.

private final Filter convertQuery(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);
            Preconditions.checkArgument(value instanceof Number);
            return numericFilter(key, (Cmp) titanPredicate, (Number) value);
        } 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 new TermsFilter(new Term(key, (String) value));
            } else if (titanPredicate == Text.CONTAINS_PREFIX) {
                value = ((String) value).toLowerCase();
                return new PrefixFilter(new Term(key, (String) value));
            } else if (titanPredicate == Text.PREFIX) {
                return new PrefixFilter(new Term(key, (String) value));
            // } else if (titanPredicate == Text.CONTAINS_REGEX) {
            // value = ((String) value).toLowerCase();
            // return new RegexpQuery(new Term(key,(String)value));
            } else if (titanPredicate == Cmp.EQUAL) {
                return new TermsFilter(new Term(key, (String) value));
            } else if (titanPredicate == Cmp.NOT_EQUAL) {
                BooleanFilter q = new BooleanFilter();
                q.add(new TermsFilter(new Term(key, (String) value)), BooleanClause.Occur.MUST_NOT);
                return q;
            } else
                throw new IllegalArgumentException("Relation 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);
            Shape shape = ((Geoshape) value).convert2Spatial4j();
            SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin, shape);
            return getSpatialStrategy(key).makeFilter(args);
        } else
            throw new IllegalArgumentException("Unsupported type: " + value);
    } else if (condition instanceof Not) {
        BooleanFilter q = new BooleanFilter();
        q.add(convertQuery(((Not) condition).getChild(), informations), BooleanClause.Occur.MUST_NOT);
        return q;
    } else if (condition instanceof And) {
        BooleanFilter q = new BooleanFilter();
        for (Condition c : condition.getChildren()) {
            q.add(convertQuery(c, informations), BooleanClause.Occur.MUST);
        }
        return q;
    } else if (condition instanceof Or) {
        BooleanFilter q = new BooleanFilter();
        for (Condition c : condition.getChildren()) {
            q.add(convertQuery(c, informations), BooleanClause.Occur.SHOULD);
        }
        return q;
    } else
        throw new IllegalArgumentException("Invalid condition: " + condition);
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(com.spatial4j.core.shape.Shape) TermsFilter(org.apache.lucene.queries.TermsFilter) Mapping(com.thinkaurelius.titan.core.Mapping) TitanPredicate(com.thinkaurelius.titan.graphdb.query.TitanPredicate)

Example 3 with Mapping

use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.

the class LuceneIndex method register.

@Override
public void register(String store, String key, KeyInformation information, TransactionHandle tx) throws StorageException {
    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);
}
Also used : Mapping(com.thinkaurelius.titan.core.Mapping)

Example 4 with Mapping

use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.

the class ElasticSearchIndex 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:
                return titanPredicate == Text.CONTAINS || titanPredicate == Text.CONTAINS_PREFIX || titanPredicate == Text.CONTAINS_REGEX;
            case STRING:
                return titanPredicate == Cmp.EQUAL || titanPredicate == Cmp.NOT_EQUAL || titanPredicate == Text.REGEX || titanPredicate == Text.PREFIX;
        }
    }
    return false;
}
Also used : Mapping(com.thinkaurelius.titan.core.Mapping)

Example 5 with Mapping

use of com.thinkaurelius.titan.core.Mapping in project titan by thinkaurelius.

the class ElasticSearchIndex 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;
}
Also used : Mapping(com.thinkaurelius.titan.core.Mapping)

Aggregations

Mapping (com.thinkaurelius.titan.core.Mapping)9 Shape (com.spatial4j.core.shape.Shape)2 TitanPredicate (com.thinkaurelius.titan.graphdb.query.TitanPredicate)2 TitanException (com.thinkaurelius.titan.core.TitanException)1 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)1 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)1 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BooleanFilter (org.apache.lucene.queries.BooleanFilter)1 TermsFilter (org.apache.lucene.queries.TermsFilter)1 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)1 ElasticSearchInterruptedException (org.elasticsearch.ElasticSearchInterruptedException)1 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexMissingException (org.elasticsearch.indices.IndexMissingException)1