Search in sources :

Example 6 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ValidationRepositoryContext method commit.

void commit() {
    if (!newObjects.isEmpty() || !objectsToDelete.isEmpty()) {
        final Set<String> ruleIdsAffectedByDeletion = Sets.newHashSet();
        service(ValidationRepository.class).write(writer -> {
            writer.putAll(newObjects);
            final Multimap<String, ComponentIdentifier> addToWhitelist = HashMultimap.create();
            newObjects.stream().filter(ValidationWhiteList.class::isInstance).map(ValidationWhiteList.class::cast).forEach(whitelist -> addToWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!addToWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : addToWhitelist.keySet()) {
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, addToWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", true)));
            }
            final Multimap<String, ComponentIdentifier> removeFromWhitelist = HashMultimap.create();
            ValidationRequests.whiteList().prepareSearch().all().filterByIds(ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class))).build().execute(this).forEach(whitelist -> removeFromWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!removeFromWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : removeFromWhitelist.keySet()) {
                    ruleIdsAffectedByDeletion.add(ruleId);
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, removeFromWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", false)));
            }
            final Map<Class<?>, Set<String>> docsToDelete = newHashMap();
            objectsToDelete.asMap().forEach((type, ids) -> docsToDelete.put(type, ImmutableSet.copyOf(ids)));
            writer.removeAll(docsToDelete);
            writer.commit();
            return null;
        });
        if (!newObjects.isEmpty()) {
            final Set<String> addedWhiteLists = newHashSet();
            final Set<String> affectedRuleIds = newHashSet();
            newObjects.forEach((doc) -> {
                if (doc instanceof ValidationWhiteList) {
                    ValidationWhiteList whitelistedDoc = (ValidationWhiteList) doc;
                    affectedRuleIds.add(whitelistedDoc.getRuleId());
                    addedWhiteLists.add(whitelistedDoc.getId());
                }
            });
            if (!addedWhiteLists.isEmpty()) {
                WhiteListNotification.added(addedWhiteLists, affectedRuleIds).publish(service(IEventBus.class));
            }
        }
        if (!objectsToDelete.isEmpty()) {
            final Set<String> removedWhiteLists = ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class));
            if (!removedWhiteLists.isEmpty()) {
                WhiteListNotification.removed(removedWhiteLists, ruleIdsAffectedByDeletion).publish(service(IEventBus.class));
            }
        }
    }
}
Also used : Set(java.util.Set) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ValidationWhiteList(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Example 7 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SearchJobRequest method prepareQuery.

@Override
protected Expression prepareQuery(ServiceProvider context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addIdFilter(queryBuilder, RemoteJobEntry.Expressions::ids);
    if (containsKey(OptionKey.KEY)) {
        queryBuilder.filter(RemoteJobEntry.Expressions.keys(getCollection(OptionKey.KEY, String.class)));
    }
    if (containsKey(OptionKey.TERM)) {
        String searchTerm = getString(OptionKey.TERM);
        queryBuilder.must(Expressions.builder().should(Expressions.prefixMatch(RemoteJobEntry.Fields.USER, searchTerm)).should(Expressions.prefixMatch(RemoteJobEntry.Fields.STATE, searchTerm.toUpperCase())).should(Expressions.matchTextAll(RemoteJobEntry.Fields.DESCRIPTION_PREFIX, searchTerm)).build());
    }
    if (containsKey(OptionKey.USER)) {
        queryBuilder.filter(users(getCollection(OptionKey.USER, String.class)));
    }
    if (containsKey(OptionKey.STATE)) {
        queryBuilder.filter(states(getCollection(OptionKey.STATE, RemoteJobState.class)));
    }
    if (options().containsKey("type")) {
        queryBuilder.filter(Expressions.nestedMatch("parameters", Expressions.matchAny("", options().getCollection("type", String.class))));
    }
    return queryBuilder.build();
}
Also used : Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 8 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SnomedDescendantsExpander method expand.

@Override
protected void expand(List<SnomedConcept> results, final Set<String> conceptIds, Options descendantExpandOptions, boolean direct) {
    try {
        final int limit = getLimit(descendantExpandOptions);
        final ExpressionBuilder expression = Expressions.builder();
        expression.filter(active());
        final ExpressionBuilder descendantFilter = Expressions.builder();
        if (stated) {
            descendantFilter.should(statedParents(conceptIds));
            if (!direct) {
                descendantFilter.should(statedAncestors(conceptIds));
            }
        } else {
            descendantFilter.should(parents(conceptIds));
            if (!direct) {
                descendantFilter.should(ancestors(conceptIds));
            }
        }
        expression.filter(descendantFilter.build());
        final Query<SnomedConceptDocument> query = Query.select(SnomedConceptDocument.class).where(expression.build()).limit((conceptIds.size() == 1 && limit == 0) ? limit : Integer.MAX_VALUE).build();
        final RevisionSearcher searcher = context().service(RevisionSearcher.class);
        final Hits<SnomedConceptDocument> hits = searcher.search(query);
        if (hits.getTotal() < 1) {
            final SnomedConcepts descendants = new SnomedConcepts(0, 0);
            for (SnomedConcept concept : results) {
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        // XXX won't work if number of results is greater than one, either use custom ConceptSearch or figure out how to expand descendants effectively
        if (conceptIds.size() == 1 && limit == 0) {
            for (SnomedConcept concept : results) {
                final SnomedConcepts descendants = new SnomedConcepts(0, hits.getTotal());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        final Multimap<String, String> descendantsByAncestor = TreeMultimap.create();
        for (SnomedConceptDocument hit : hits) {
            final Set<String> parentsAndAncestors = newHashSet();
            if (stated) {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedAncestors()));
                }
            } else {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getAncestors()));
                }
            }
            parentsAndAncestors.retainAll(conceptIds);
            for (String ancestor : parentsAndAncestors) {
                descendantsByAncestor.put(ancestor, hit.getId());
            }
        }
        final Collection<String> componentIds = newHashSet(descendantsByAncestor.values());
        if (limit > 0 && !componentIds.isEmpty()) {
            // query descendants again
            final SnomedConcepts descendants = SnomedRequests.prepareSearchConcept().all().filterByIds(componentIds).setLocales(locales()).setExpand(descendantExpandOptions.get("expand", Options.class)).build().execute(context());
            final Map<String, SnomedConcept> descendantsById = newHashMap();
            descendantsById.putAll(Maps.uniqueIndex(descendants, SnomedConcept::getId));
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final List<SnomedConcept> currentDescendants = FluentIterable.from(descendantIds).limit(limit).transform(Functions.forMap(descendantsById)).toList();
                final SnomedConcepts descendantConcepts = new SnomedConcepts(currentDescendants, null, limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendantConcepts);
                } else {
                    concept.setDescendants(descendantConcepts);
                }
            }
        } else {
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final SnomedConcepts descendants = new SnomedConcepts(limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
        }
    } catch (IOException e) {
        throw SnowowlRuntimeException.wrap(e);
    }
}
Also used : SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) IOException(java.io.IOException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Example 9 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class DefaultRevisionIndex method doRevisionCompare.

private void doRevisionCompare(Searcher searcher, RevisionBranchRef compareRef, RevisionCompare.Builder result) throws IOException {
    if (compareRef.segments().isEmpty()) {
        return;
    }
    ExpressionBuilder compareCommitsQuery = Expressions.builder();
    for (RevisionSegment segment : compareRef.segments()) {
        String segmentBranch = getBranchPath(searcher, segment.branchId());
        compareCommitsQuery.should(Expressions.builder().filter(Commit.Expressions.timestampRange(segment.start(), segment.end())).filter(Commit.Expressions.branches(Collections.singleton(segmentBranch))).build());
    }
    // apply commits happened on the compareRef segments in chronological order
    AfterWhereBuilder<Commit> query = Query.select(Commit.class).where(compareCommitsQuery.build()).limit(// load only 20 commits for each batch (import commits tend to be large, so if we load 20 of them we should not use that much memory)
    20).sortBy(SortBy.field(Commit.Fields.TIMESTAMP, Order.ASC));
    int processedCommits = 0;
    Hits<Commit> hits = null;
    do {
        if (hits != null) {
            query.searchAfter(hits.getSearchAfter());
        }
        hits = searcher.search(query.build());
        hits.forEach(result::apply);
        processedCommits += hits.getLimit();
    } while (processedCommits < hits.getTotal());
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 10 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class Query method withFilter.

public AfterWhereBuilder<T> withFilter(Expression additionalFilter) {
    final BiFunction<ExpressionBuilder, Expression, ExpressionBuilder> boolClause = isWithScores() ? ExpressionBuilder::must : ExpressionBuilder::filter;
    ExpressionBuilder altered = Expressions.bool();
    boolClause.apply(altered, getWhere());
    altered.filter(additionalFilter);
    return withWhere(altered.build());
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Aggregations

ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)47 Expressions (com.b2international.index.query.Expressions)17 Expression (com.b2international.index.query.Expression)6 List (java.util.List)6 Set (java.util.Set)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)5 Options (com.b2international.commons.options.Options)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)5 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)5 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)4 IOException (java.io.IOException)4 CompareUtils (com.b2international.commons.CompareUtils)3 Query (com.b2international.index.query.Query)3 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)3 BranchContext (com.b2international.snowowl.core.domain.BranchContext)3 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)3