Search in sources :

Example 1 with EntityIndex

use of io.openk9.entity.manager.model.index.EntityIndex in project openk9 by smclab.

the class CreateEntitiesRunnable method _disambiguate.

private Optional<EntityGraph> _disambiguate(EntityGraphService entityGraphService, List<EntityIndex> candidates, List<EntityIndex> entityRequestList, Entity currentEntityRequest, String[] uniqueEntities, int minHops, int maxHops) {
    String currentEntityRequestType = currentEntityRequest.getType();
    List<EntityGraph> result = List.of();
    if (_containsValue(uniqueEntities, currentEntityRequestType)) {
        if (candidates.size() == 1) {
            if (_log.isDebugEnabled()) {
                _log.debug("disambiguating entity with type " + currentEntityRequestType);
            }
            EntityIndex candidate = candidates.get(0);
            EntityGraph entityGraph = EntityGraph.of(candidate.getId(), candidate.getGraphId(), candidate.getTenantId(), candidate.getName(), candidate.getType());
            result = List.of(entityGraph);
        } else if (candidates.size() > 1) {
            result = _getEntityGraphs(entityGraphService, entityRequestList, minHops, maxHops, currentEntityRequestType, result);
        }
    } else {
        if (!candidates.isEmpty()) {
            result = _getEntityGraphs(entityGraphService, entityRequestList, minHops, maxHops, currentEntityRequestType, result);
        }
    }
    if (_log.isDebugEnabled()) {
        _log.debug("_disambiguate: " + result + " current: " + currentEntityRequest);
    }
    return result.stream().filter(Objects::nonNull).filter(entityGraph -> candidates.stream().anyMatch(entityIndex -> entityGraph.getId().equals(entityIndex.getId()))).findFirst();
}
Also used : EntityGraph(io.openk9.entity.manager.model.graph.EntityGraph) Arrays(java.util.Arrays) Member(com.hazelcast.cluster.Member) FutureUtil(io.openk9.entity.manager.util.FutureUtil) EntityKey(io.openk9.entity.manager.cache.model.EntityKey) SymbolicName(org.neo4j.cypherdsl.core.SymbolicName) Future(java.util.concurrent.Future) Map(java.util.Map) ToString(lombok.ToString) Property(org.neo4j.cypherdsl.core.Property) Cypher(org.neo4j.cypherdsl.core.Cypher) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) EntityGraph(io.openk9.entity.manager.model.graph.EntityGraph) Collection(java.util.Collection) CDI(javax.enterprise.inject.spi.CDI) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Builder(lombok.Builder) Optional(java.util.Optional) Cypher.literalOf(org.neo4j.cypherdsl.core.Cypher.literalOf) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) AssociableEntityKey(io.openk9.entity.manager.cache.model.AssociableEntityKey) EntityGraphConfig(io.openk9.entity.manager.config.EntityGraphConfig) Logger(org.jboss.logging.Logger) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Statement(org.neo4j.cypherdsl.core.Statement) Schedulers(reactor.core.scheduler.Schedulers) EntityNameCleanerProvider(io.openk9.entity.manager.cleaner.EntityNameCleanerProvider) Node(org.neo4j.cypherdsl.core.Node) HazelcastInstance(com.hazelcast.core.HazelcastInstance) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) EntityNameCleaner(io.openk9.entity.manager.cleaner.EntityNameCleaner) Entity(io.openk9.entity.manager.cache.model.Entity) Functions(org.neo4j.cypherdsl.core.Functions) Mono(reactor.core.publisher.Mono) IExecutorService(com.hazelcast.core.IExecutorService) GetEntitiesCallable(io.openk9.entity.manager.action.GetEntitiesCallable) AliasedExpression(org.neo4j.cypherdsl.core.AliasedExpression) Predicates(com.hazelcast.query.Predicates) EntityService(io.openk9.entity.manager.service.index.EntityService) Data(lombok.Data) AllArgsConstructor(lombok.AllArgsConstructor) EntityGraphService(io.openk9.entity.manager.service.graph.EntityGraphService) Collections(java.util.Collections) MapUtil(io.openk9.entity.manager.util.MapUtil) IMap(com.hazelcast.map.IMap) Objects(java.util.Objects) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) ToString(lombok.ToString)

Example 2 with EntityIndex

use of io.openk9.entity.manager.model.index.EntityIndex in project openk9 by smclab.

the class CreateEntitiesRunnable method _getEntityGraphs.

private List<EntityGraph> _getEntityGraphs(EntityGraphService entityGraphService, List<EntityIndex> entityRequestList, int minHops, int maxHops, String currentEntityRequestType, List<EntityGraph> result) {
    if (_log.isDebugEnabled()) {
        _log.debug("disambiguating with search entity with type " + currentEntityRequestType);
    }
    Statement[] statements = new Statement[entityRequestList.size()];
    for (int i = 0; i < entityRequestList.size(); i++) {
        EntityIndex entityRequest = entityRequestList.get(i);
        Node nodeEntity = Cypher.node(entityRequest.getType()).named("entity");
        AliasedExpression entityAliased = nodeEntity.as("entity");
        SymbolicName path = Cypher.name("path");
        Property idProperty = entityAliased.getDelegate().property("id");
        Statement statement = Cypher.match(nodeEntity).where(idProperty.eq(literalOf(entityRequest.getId()))).call("apoc.path.expand").withArgs(entityAliased.getDelegate(), literalOf(null), literalOf("-date"), literalOf(minHops), literalOf(maxHops)).yield(path).returning(Functions.last(Functions.nodes(path)).as("node"), Functions.size(Functions.nodes(path)).subtract(literalOf(1)).as("hops")).build();
        statements[i] = statement;
    }
    if (statements.length == 1) {
        Statement entityRequestListStatement = Cypher.call(statements[0]).returning("node", "hops").orderBy(Cypher.name("hops")).build();
        result = entityGraphService.search(entityRequestListStatement);
    } else if (statements.length > 1) {
        Statement entityRequestListStatement = Cypher.call(Cypher.unionAll(statements)).returning("node", "hops").orderBy(Cypher.name("hops")).build();
        result = entityGraphService.search(entityRequestListStatement);
    }
    return result;
}
Also used : Statement(org.neo4j.cypherdsl.core.Statement) Node(org.neo4j.cypherdsl.core.Node) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) SymbolicName(org.neo4j.cypherdsl.core.SymbolicName) Property(org.neo4j.cypherdsl.core.Property) AliasedExpression(org.neo4j.cypherdsl.core.AliasedExpression)

Example 3 with EntityIndex

use of io.openk9.entity.manager.model.index.EntityIndex in project openk9 by smclab.

the class EntityService method searchByNameAndType.

public EntityIndex searchByNameAndType(long tenantId, String name, String type) {
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.matchQuery("name", name));
    boolQueryBuilder.must(QueryBuilders.matchQuery("type", type));
    List<EntityIndex> search = search(tenantId, boolQueryBuilder, 0, 1);
    if (search.isEmpty()) {
        return null;
    }
    return search.get(0);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex)

Example 4 with EntityIndex

use of io.openk9.entity.manager.model.index.EntityIndex in project openk9 by smclab.

the class CreateEntitiesRunnable method getEntityCandidates.

private EntityCandidates getEntityCandidates(EntityNameCleanerProvider entityNameCleanerProvider, EntityService entityService, EntityMember ingestionIdEntityMember, Entity ingestionIdEntity) {
    EntityNameCleaner entityNameCleaner = entityNameCleanerProvider.get(ingestionIdEntity.getType());
    QueryBuilder queryBuilder = entityNameCleaner.cleanEntityName(ingestionIdEntity.getTenantId(), ingestionIdEntity.getName());
    List<EntityIndex> candidates = entityService.search(ingestionIdEntity.getTenantId(), queryBuilder, 0, 10);
    return EntityCandidates.of(ingestionIdEntityMember, candidates);
}
Also used : EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) EntityNameCleaner(io.openk9.entity.manager.cleaner.EntityNameCleaner)

Example 5 with EntityIndex

use of io.openk9.entity.manager.model.index.EntityIndex in project openk9 by smclab.

the class CreateEntitiesRunnable method cleanCandidates.

private List<EntityIndex> cleanCandidates(Entity entityRequest, List<EntityIndex> candidates, EntityNameCleanerProvider entityNameCleanerProvider, float scoreThreshold) {
    if (!candidates.isEmpty()) {
        EntityIndex documentEntityResponse = candidates.get(0);
        double bestScore;
        if (candidates.size() > 1) {
            double[] scores = candidates.stream().mapToDouble(EntityIndex::getScore).toArray();
            bestScore = _softmax(documentEntityResponse.getScore(), scores);
        } else {
            bestScore = _levenshteinDistance(entityNameCleanerProvider.get(documentEntityResponse.getType()).cleanEntityName(documentEntityResponse.getName()), entityNameCleanerProvider.get(entityRequest.getType()).cleanEntityName(entityRequest.getName()));
        }
        if (bestScore > scoreThreshold) {
            return Collections.singletonList(documentEntityResponse);
        }
    }
    return candidates;
}
Also used : EntityIndex(io.openk9.entity.manager.model.index.EntityIndex)

Aggregations

EntityIndex (io.openk9.entity.manager.model.index.EntityIndex)6 EntityNameCleaner (io.openk9.entity.manager.cleaner.EntityNameCleaner)2 ArrayList (java.util.ArrayList)2 AliasedExpression (org.neo4j.cypherdsl.core.AliasedExpression)2 Node (org.neo4j.cypherdsl.core.Node)2 Property (org.neo4j.cypherdsl.core.Property)2 Statement (org.neo4j.cypherdsl.core.Statement)2 SymbolicName (org.neo4j.cypherdsl.core.SymbolicName)2 Member (com.hazelcast.cluster.Member)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 IExecutorService (com.hazelcast.core.IExecutorService)1 IMap (com.hazelcast.map.IMap)1 Predicates (com.hazelcast.query.Predicates)1 GetEntitiesCallable (io.openk9.entity.manager.action.GetEntitiesCallable)1 AssociableEntityKey (io.openk9.entity.manager.cache.model.AssociableEntityKey)1 Entity (io.openk9.entity.manager.cache.model.Entity)1 EntityKey (io.openk9.entity.manager.cache.model.EntityKey)1 EntityNameCleanerProvider (io.openk9.entity.manager.cleaner.EntityNameCleanerProvider)1 EntityGraphConfig (io.openk9.entity.manager.config.EntityGraphConfig)1