Search in sources :

Example 1 with Search

use of io.openk9.search.client.api.Search 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 2 with Search

use of io.openk9.search.client.api.Search 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 3 with Search

use of io.openk9.search.client.api.Search in project openk9 by smclab.

the class EntityService method search.

public List<EntityIndex> search(long tenantId, QueryBuilder queryBuilder, int from, int size) {
    SearchRequest searchRequest = new SearchRequest(tenantId + "-entity");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.size(from);
    searchSourceBuilder.size(size);
    searchRequest.source(searchSourceBuilder);
    try {
        SearchResponse searchResponse = _restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        List<EntityIndex> results = new ArrayList<>(hits.getHits().length);
        for (SearchHit hit : hits.getHits()) {
            String sourceAsString = hit.getSourceAsString();
            JsonObject json = new JsonObject(sourceAsString);
            EntityIndex entityIndex = json.mapTo(EntityIndex.class);
            entityIndex.setScore(hit.getScore());
            results.add(entityIndex);
        }
        return results;
    } catch (Exception e) {
        _logger.error(e.getMessage());
    }
    return List.of();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with Search

use of io.openk9.search.client.api.Search in project openk9 by smclab.

the class BaseAggregatorAnnotator method annotate_.

@Override
public List<CategorySemantics> annotate_(long tenantId, String... tokens) {
    List<String> normalizedKeywords = tenantKeywordsMap.getOrDefault(tenantId, tenantKeywordsMap.get(-1L));
    if (normalizedKeywords == null) {
        return List.of();
    }
    RestHighLevelClient restHighLevelClient = restHighLevelClientProvider.get();
    String token;
    if (tokens.length == 1) {
        token = tokens[0];
    } else {
        token = String.join(" ", tokens);
    }
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (String keyword : normalizedKeywords) {
        boolQueryBuilder.should(query(keyword, token));
    }
    builder.must(boolQueryBuilder);
    SearchRequest searchRequest;
    if (tenantId == -1) {
        searchRequest = new SearchRequest("*-*-data");
    } else {
        searchRequest = new SearchRequest(tenantId + "-*-data");
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0);
    searchSourceBuilder.query(builder);
    for (String keyword : normalizedKeywords) {
        searchSourceBuilder.aggregation(AggregationBuilders.terms(keyword).field(keyword).size(10));
    }
    searchRequest.source(searchSourceBuilder);
    if (_log.isDebugEnabled()) {
        _log.debug(builder.toString());
    }
    List<Tuple> scoreKeys = new ArrayList<>();
    try {
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        for (Aggregation aggregation : search.getAggregations()) {
            Terms terms = (Terms) aggregation;
            for (Terms.Bucket bucket : terms.getBuckets()) {
                String keyAsString = bucket.getKeyAsString();
                if (token.equalsIgnoreCase(keyAsString)) {
                    return List.of(_createCategorySemantics(terms.getName(), keyAsString));
                }
                scoreKeys.add(Tuple.of((Supplier<Double>) () -> _levenshteinDistance(token, keyAsString), keyAsString, terms.getName()));
            }
        }
    } catch (IOException e) {
        _log.error(e.getMessage(), e);
    }
    if (scoreKeys.isEmpty()) {
        return List.of();
    }
    scoreKeys.sort(Collections.reverseOrder(Comparator.comparingDouble(t -> ((Supplier<Double>) t.get(0)).get())));
    String key = (String) scoreKeys.get(0).get(1);
    String name = (String) scoreKeys.get(0).get(2);
    return List.of(_createCategorySemantics(name, key));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ArrayList(java.util.ArrayList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Supplier(java.util.function.Supplier) Tuple(io.openk9.search.api.query.parser.Tuple)

Example 5 with Search

use of io.openk9.search.client.api.Search in project openk9 by smclab.

the class BaseNerAnnotator method annotate_.

@Override
public List<CategorySemantics> annotate_(long tenantId, String... tokens) {
    _log.debug(Arrays.toString(tokens));
    if (_containsStopword(tokens)) {
        return List.of();
    }
    RestHighLevelClient restHighLevelClient = restHighLevelClientProvider.get();
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    builder.must(QueryBuilders.matchQuery("type.keyword", category));
    for (String token : tokens) {
        if (!stopWords.contains(token)) {
            builder.must(query("name", token));
        }
    }
    SearchRequest searchRequest;
    if (tenantId == -1) {
        searchRequest = new SearchRequest("*-entity");
    } else {
        searchRequest = new SearchRequest(tenantId + "-entity");
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(_annotatorConfig.nerSize());
    searchSourceBuilder.query(builder);
    searchRequest.source(searchSourceBuilder);
    List<CategorySemantics> list = new ArrayList<>();
    if (_log.isDebugEnabled()) {
        _log.debug(builder.toString());
    }
    try {
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits()) {
            Map<String, Object> senamtics = hit.getSourceAsMap();
            list.add(CategorySemantics.of("$" + senamtics.get("type"), Map.of("tokenType", "ENTITY", "entityType", senamtics.get("type"), "entityName", senamtics.get("name"), "tenantId", senamtics.get("tenantId"), "value", senamtics.get("id"), "score", hit.getScore())));
        }
        if (_log.isDebugEnabled()) {
            _log.debug(list.toString());
        }
    } catch (IOException e) {
        _log.error(e.getMessage(), e);
    }
    return list;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) CategorySemantics(io.openk9.search.api.query.parser.CategorySemantics) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)4 EntityIndex (io.openk9.entity.manager.model.index.EntityIndex)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 SearchHit (org.elasticsearch.search.SearchHit)2 ReactorStopWatch (io.openk9.common.api.reactor.util.ReactorStopWatch)1 EntityGraphRepository (io.openk9.entity.manager.api.EntityGraphRepository)1 EntityNameCleaner (io.openk9.entity.manager.api.EntityNameCleaner)1 EntityNameCleanerProvider (io.openk9.entity.manager.api.EntityNameCleanerProvider)1 Entity (io.openk9.entity.manager.model.Entity)1 EntityRequest (io.openk9.entity.manager.model.payload.EntityRequest)1 IndexWriterEntityClient (io.openk9.index.writer.entity.client.api.IndexWriterEntityClient)1 DocumentEntityRequest (io.openk9.index.writer.entity.model.DocumentEntityRequest)1 DocumentEntityResponse (io.openk9.index.writer.entity.model.DocumentEntityResponse)1 JsonFactory (io.openk9.json.api.JsonFactory)1 GraphClient (io.openk9.relationship.graph.api.client.GraphClient)1