Search in sources :

Example 1 with KeyInformation

use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.

the class LuceneCustomAnalyzer method getWrappedAnalyzer.

@Override
protected final Analyzer getWrappedAnalyzer(String fieldName) {
    final KeyInformation keyInformation = informations.get(store, fieldName);
    if (keyInformation == null || !String.class.isAssignableFrom(keyInformation.getDataType())) {
        return analyzerFor(STANDARD_ANALYZER);
    }
    final Parameter[] parameters = keyInformation.getParameters();
    // if mapping isn't present in parameters, we use Mapping.DEFAULT
    final Mapping mapping = ParameterType.MAPPING.findParameter(parameters, Mapping.DEFAULT);
    // everything else falls through a StandardAnalyzer as was the case before
    return analyzerFor(analyzerNameFor(parameters, mapping, KEYWORD_ANALYZER, STANDARD_ANALYZER));
}
Also used : Parameter(org.janusgraph.core.schema.Parameter) Mapping(org.janusgraph.core.schema.Mapping) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 2 with KeyInformation

use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.

the class ElasticSearchIndex method query.

@Override
public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    final ElasticSearchRequest sr = new ElasticSearchRequest();
    final Map<String, Object> esQuery = getFilter(query.getCondition(), informations.get(query.getStore()));
    sr.setQuery(compat.prepareQuery(esQuery));
    if (!query.getOrder().isEmpty()) {
        final List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (final IndexQuery.OrderEntry orderEntry : orders) {
            final String order = orderEntry.getOrder().name();
            final KeyInformation information = informations.get(query.getStore()).get(orderEntry.getKey());
            final Mapping mapping = Mapping.getMapping(information);
            final Class<?> datatype = orderEntry.getDatatype();
            sr.addSort(orderEntry.getKey(), order.toLowerCase(), convertToEsDataType(datatype, mapping));
        }
    }
    sr.setFrom(0);
    if (query.hasLimit()) {
        sr.setSize(Math.min(query.getLimit(), batchSize));
    } else {
        sr.setSize(batchSize);
    }
    ElasticSearchResponse response;
    try {
        final String indexStoreName = getIndexStoreName(query.getStore());
        final String indexType = useMultitypeIndex ? query.getStore() : null;
        response = client.search(indexStoreName, indexType, compat.createRequestBody(sr, NULL_PARAMETERS), sr.getSize() >= batchSize);
        log.debug("First Executed query [{}] in {} ms", query.getCondition(), response.getTook());
        final ElasticSearchScroll resultIterator = new ElasticSearchScroll(client, response, sr.getSize());
        final Stream<RawQuery.Result<String>> toReturn = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false);
        return (query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn).map(RawQuery.Result::getResult);
    } catch (final IOException | UncheckedIOException e) {
        throw new PermanentBackendException(e);
    }
}
Also used : IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) Mapping(org.janusgraph.core.schema.Mapping) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 3 with KeyInformation

use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.

the class ElasticSearchIndex method getDeletionScript.

private String getDeletionScript(KeyInformation.IndexRetriever information, String storeName, IndexMutation mutation) throws PermanentBackendException {
    final StringBuilder script = new StringBuilder();
    final String INDEX_NAME = "index";
    int i = 0;
    for (final IndexEntry deletion : mutation.getDeletions()) {
        final KeyInformation keyInformation = information.get(storeName).get(deletion.field);
        switch(keyInformation.getCardinality()) {
            case SINGLE:
                script.append("ctx._source.remove(\"").append(deletion.field).append("\");");
                if (hasDualStringMapping(information.get(storeName, deletion.field))) {
                    script.append("ctx._source.remove(\"").append(getDualMappingName(deletion.field)).append("\");");
                }
                break;
            case SET:
            case LIST:
                final String jsValue = convertToJsType(deletion.value, compat.scriptLang(), Mapping.getMapping(keyInformation));
                String index = INDEX_NAME + i++;
                script.append("def ").append(index).append(" = ctx._source[\"").append(deletion.field).append("\"].indexOf(").append(jsValue).append("); ctx._source[\"").append(deletion.field).append("\"].remove(").append(index).append(");");
                if (hasDualStringMapping(information.get(storeName, deletion.field))) {
                    index = INDEX_NAME + i++;
                    script.append("def ").append(index).append(" = ctx._source[\"").append(getDualMappingName(deletion.field)).append("\"].indexOf(").append(jsValue).append("); ctx._source[\"").append(getDualMappingName(deletion.field)).append("\"].remove(").append(index).append(");");
                }
                break;
        }
    }
    return script.toString();
}
Also used : IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 4 with KeyInformation

use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.

the class ElasticSearchIndex method getAdditionScript.

private String getAdditionScript(KeyInformation.IndexRetriever information, String storeName, IndexMutation mutation) throws PermanentBackendException {
    final StringBuilder script = new StringBuilder();
    for (final IndexEntry e : mutation.getAdditions()) {
        final KeyInformation keyInformation = information.get(storeName).get(e.field);
        switch(keyInformation.getCardinality()) {
            case SET:
            case LIST:
                script.append("if(ctx._source[\"").append(e.field).append("\"] == null) ctx._source[\"").append(e.field).append("\"] = [];");
                script.append("ctx._source[\"").append(e.field).append("\"].add(").append(convertToJsType(e.value, compat.scriptLang(), Mapping.getMapping(keyInformation))).append(");");
                if (hasDualStringMapping(keyInformation)) {
                    script.append("if(ctx._source[\"").append(getDualMappingName(e.field)).append("\"] == null) ctx._source[\"").append(getDualMappingName(e.field)).append("\"] = [];");
                    script.append("ctx._source[\"").append(getDualMappingName(e.field)).append("\"].add(").append(convertToJsType(e.value, compat.scriptLang(), Mapping.getMapping(keyInformation))).append(");");
                }
                break;
            default:
                break;
        }
    }
    return script.toString();
}
Also used : IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 5 with KeyInformation

use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.

the class ElasticSearchIndex method getNewDocument.

public Map<String, Object> getNewDocument(final List<IndexEntry> additions, KeyInformation.StoreRetriever information) throws BackendException {
    // JSON writes duplicate fields one after another, which forces us
    // at this stage to make de-duplication on the IndexEntry list. We don't want to pay the
    // price map storage on the Mutation level because none of other backends need that.
    final Multimap<String, IndexEntry> unique = LinkedListMultimap.create();
    for (final IndexEntry e : additions) {
        unique.put(e.field, e);
    }
    final Map<String, Object> doc = new HashMap<>();
    for (final Map.Entry<String, Collection<IndexEntry>> add : unique.asMap().entrySet()) {
        final KeyInformation keyInformation = information.get(add.getKey());
        final Object value;
        switch(keyInformation.getCardinality()) {
            case SINGLE:
                value = convertToEsType(Iterators.getLast(add.getValue().iterator()).value, Mapping.getMapping(keyInformation));
                break;
            case SET:
            case LIST:
                value = add.getValue().stream().map(v -> convertToEsType(v.value, Mapping.getMapping(keyInformation))).filter(v -> {
                    Preconditions.checkArgument(!(v instanceof byte[]), "Collections not supported for " + add.getKey());
                    return true;
                }).toArray();
                break;
            default:
                value = null;
                break;
        }
        doc.put(add.getKey(), value);
        if (hasDualStringMapping(information.get(add.getKey())) && keyInformation.getDataType() == String.class) {
            doc.put(getDualMappingName(add.getKey()), value);
        }
    }
    return doc;
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) INDEX_MAX_RESULT_SET_SIZE(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_MAX_RESULT_SET_SIZE) StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) Spliterators(java.util.Spliterators) LoggerFactory(org.slf4j.LoggerFactory) ConfigOption(org.janusgraph.diskstorage.configuration.ConfigOption) Geoshape(org.janusgraph.core.attribute.Geoshape) AbstractESCompat(org.janusgraph.diskstorage.es.compat.AbstractESCompat) BaseTransaction(org.janusgraph.diskstorage.BaseTransaction) IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) Cardinality(org.janusgraph.core.Cardinality) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) AttributeUtil(org.janusgraph.graphdb.database.serialize.AttributeUtil) Map(java.util.Map) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) SerializationFeature(org.apache.tinkerpop.shaded.jackson.databind.SerializationFeature) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) ES6Compat(org.janusgraph.diskstorage.es.compat.ES6Compat) And(org.janusgraph.graphdb.query.condition.And) Mapping(org.janusgraph.core.schema.Mapping) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ES_GEO_COORDS_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_GEO_COORDS_KEY) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) INDEX_NAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NAME) Parameter(org.janusgraph.core.schema.Parameter) Stream(java.util.stream.Stream) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) HttpAuthTypes(org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes) Spliterator(java.util.Spliterator) PreInitializeConfigOptions(org.janusgraph.graphdb.configuration.PreInitializeConfigOptions) Not(org.janusgraph.graphdb.query.condition.Not) IntStream(java.util.stream.IntStream) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) ConfigNamespace(org.janusgraph.diskstorage.configuration.ConfigNamespace) Condition(org.janusgraph.graphdb.query.condition.Condition) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ImmutableList(com.google.common.collect.ImmutableList) Cmp(org.janusgraph.core.attribute.Cmp) IndexFeatures(org.janusgraph.diskstorage.indexing.IndexFeatures) Or(org.janusgraph.graphdb.query.condition.Or) JanusGraphException(org.janusgraph.core.JanusGraphException) ES1Compat(org.janusgraph.diskstorage.es.compat.ES1Compat) StreamSupport(java.util.stream.StreamSupport) Geo(org.janusgraph.core.attribute.Geo) BackendException(org.janusgraph.diskstorage.BackendException) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Logger(org.slf4j.Logger) Configuration(org.janusgraph.diskstorage.configuration.Configuration) BaseTransactionConfigurable(org.janusgraph.diskstorage.BaseTransactionConfigurable) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) ES_TYPE_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_TYPE_KEY) IOException(java.io.IOException) ES_DOC_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_DOC_KEY) DefaultTransaction(org.janusgraph.diskstorage.util.DefaultTransaction) Text(org.janusgraph.core.attribute.Text) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) ES2Compat(org.janusgraph.diskstorage.es.compat.ES2Compat) ConfigOption.disallowEmpty(org.janusgraph.diskstorage.configuration.ConfigOption.disallowEmpty) Preconditions(com.google.common.base.Preconditions) ParameterType(org.janusgraph.graphdb.types.ParameterType) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) INDEX_NS(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NS) ES5Compat(org.janusgraph.diskstorage.es.compat.ES5Compat) IndexMutation(org.janusgraph.diskstorage.indexing.IndexMutation) HashMap(java.util.HashMap) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Aggregations

KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)7 IndexEntry (org.janusgraph.diskstorage.indexing.IndexEntry)5 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 HashMap (java.util.HashMap)3 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 Mapping (org.janusgraph.core.schema.Mapping)2 BackendException (org.janusgraph.diskstorage.BackendException)2 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)2 IndexMutation (org.janusgraph.diskstorage.indexing.IndexMutation)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)1 Multimap (com.google.common.collect.Multimap)1 Instant (java.time.Instant)1