use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.
the class BoolQueryTest method generateDeepBooleanClause.
private Expression generateDeepBooleanClause(int depth, BiFunction<ExpressionBuilder, Expression, ExpressionBuilder> boolClause) {
ExpressionBuilder root = Expressions.bool();
ExpressionBuilder current = root;
boolClause.apply(current, Expressions.exactMatch("id", UUID.randomUUID().toString()));
for (int i = 0; i < depth; i++) {
ExpressionBuilder nested = Expressions.bool();
boolClause.apply(nested, Expressions.exactMatch("id", UUID.randomUUID().toString()));
current.should(nested.build());
current = nested;
}
return root.build();
}
use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.
the class CommitInfoSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(RepositoryContext context) {
ExpressionBuilder queryBuilder = Expressions.builder();
addIdFilter(queryBuilder, Commit.Expressions::ids);
addSecurityFilter(queryBuilder, context);
addBranchClause(queryBuilder, context);
addBranchPrefixClause(queryBuilder);
addUserIdClause(queryBuilder);
addCommentClause(queryBuilder);
addTimeStampClause(queryBuilder);
addTimeStampRangeClause(queryBuilder);
addAffectedComponentClause(queryBuilder);
return queryBuilder.build();
}
use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.
the class CommitInfoSearchRequest method addSecurityFilter.
private void addSecurityFilter(final ExpressionBuilder builder, RepositoryContext context) {
final User user = context.service(User.class);
if (user.isAdministrator() || user.hasPermission(Permission.requireAll(Permission.OPERATION_BROWSE, Permission.ALL))) {
return;
}
final List<Permission> readPermissions = user.getPermissions().stream().filter(p -> Permission.ALL.equals(p.getOperation()) || Permission.OPERATION_BROWSE.equals(p.getOperation())).collect(Collectors.toList());
final Set<String> exactResourceIds = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> !resource.endsWith("*")).collect(Collectors.toSet());
final Set<String> resourceIdPrefixes = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> isWildCardResource(resource)).map(resource -> resource.substring(0, resource.length() - 1)).collect(Collectors.toSet());
SetView<String> resourceIds = Sets.union(exactResourceIds, resourceIdPrefixes);
ExpressionBuilder branchFilter = Expressions.builder();
ResourceRequests.prepareSearch().filterByIds(resourceIds).setLimit(resourceIds.size()).setFields(ResourceDocument.Fields.ID, ResourceDocument.Fields.BRANCH_PATH, ResourceDocument.Fields.RESOURCE_TYPE).buildAsync().getRequest().execute(context).stream().filter(TerminologyResource.class::isInstance).map(TerminologyResource.class::cast).forEach(r -> {
if (resourceIdPrefixes.contains(r.getId())) {
final String branchPattern = String.format("%s(/[a-zA-Z0-9.~_\\-]{1,%d})?", r.getBranchPath(), DEFAULT_MAXIMUM_BRANCH_NAME_LENGTH);
branchFilter.should(regexp(BRANCH, branchPattern));
}
});
builder.filter(branchFilter.build());
}
use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.
the class SnomedConceptSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(BranchContext context) {
ExpressionBuilder queryBuilder = Expressions.builder();
addActiveClause(queryBuilder);
addReleasedClause(queryBuilder);
addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
addEclFilter(context, queryBuilder, OptionKey.DEFINITION_STATUS, SnomedConceptDocument.Expressions::definitionStatusIds);
addNamespaceFilter(queryBuilder);
addNamespaceConceptIdFilter(context, queryBuilder);
addEffectiveTimeClause(queryBuilder);
addActiveMemberOfClause(context, queryBuilder);
addMemberOfClause(context, queryBuilder);
addFilter(queryBuilder, OptionKey.PARENT, String.class, SnomedConceptDocument.Expressions::parents);
addFilter(queryBuilder, OptionKey.STATED_PARENT, String.class, SnomedConceptDocument.Expressions::statedParents);
if (containsKey(OptionKey.ANCESTOR)) {
final Collection<String> ancestorIds = getCollection(OptionKey.ANCESTOR, String.class);
queryBuilder.filter(Expressions.builder().should(parents(ancestorIds)).should(ancestors(ancestorIds)).build());
}
if (containsKey(OptionKey.STATED_ANCESTOR)) {
final Collection<String> ancestorIds = getCollection(OptionKey.STATED_ANCESTOR, String.class);
queryBuilder.filter(Expressions.builder().should(statedParents(ancestorIds)).should(statedAncestors(ancestorIds)).build());
}
if (containsKey(OptionKey.ECL)) {
final String ecl = getString(OptionKey.ECL);
Expression eclExpression = EclExpression.of(ecl, Trees.INFERRED_FORM).resolveToExpression(context).getSync(3, TimeUnit.MINUTES);
if (eclExpression.isMatchNone()) {
throw new NoResultException();
} else if (!eclExpression.isMatchAll()) {
queryBuilder.filter(eclExpression);
}
}
if (containsKey(OptionKey.STATED_ECL)) {
final String ecl = getString(OptionKey.STATED_ECL);
Expression statedEclExpression = EclExpression.of(ecl, Trees.STATED_FORM).resolveToExpression(context).getSync(3, TimeUnit.MINUTES);
if (statedEclExpression.isMatchNone()) {
throw new NoResultException();
} else if (!statedEclExpression.isMatchAll()) {
queryBuilder.filter(statedEclExpression);
}
}
Expression searchProfileQuery = null;
final Expression queryExpression;
if (containsKey(OptionKey.SEMANTIC_TAG)) {
queryBuilder.filter(SnomedConceptDocument.Expressions.semanticTags(getCollection(OptionKey.SEMANTIC_TAG, String.class)));
}
if (containsKey(OptionKey.TERM)) {
final ExpressionBuilder bq = Expressions.builder();
// nest current query
bq.filter(queryBuilder.build());
queryBuilder = bq;
final TermFilter termFilter = containsKey(OptionKey.TERM) ? get(OptionKey.TERM, TermFilter.class) : null;
final Map<String, Float> conceptScoreMap = executeDescriptionSearch(context, termFilter);
if (termFilter != null) {
try {
final ComponentCategory category = SnomedIdentifiers.getComponentCategory(termFilter.getTerm());
if (category == ComponentCategory.CONCEPT) {
conceptScoreMap.put(termFilter.getTerm(), Float.MAX_VALUE);
}
} catch (IllegalArgumentException e) {
// ignored
}
}
if (conceptScoreMap.isEmpty()) {
throw new NoResultException();
}
queryBuilder.filter(RevisionDocument.Expressions.ids(conceptScoreMap.keySet()));
final Expression q = addSearchProfile(searchProfileQuery, queryBuilder.build());
queryExpression = Expressions.scriptScore(q, "doiFactor", Map.of("termScores", conceptScoreMap, "useDoi", containsKey(OptionKey.USE_DOI), "minDoi", MIN_DOI_VALUE, "maxDoi", MAX_DOI_VALUE));
} else if (containsKey(OptionKey.USE_DOI)) {
final Expression q = addSearchProfile(searchProfileQuery, queryBuilder.build());
queryExpression = Expressions.scriptScore(q, "doi");
} else {
queryExpression = addSearchProfile(searchProfileQuery, queryBuilder.build());
}
return queryExpression;
}
use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.
the class SnomedRefSetMemberSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(BranchContext context) {
final Collection<String> referencedComponentIds = getCollection(OptionKey.REFERENCED_COMPONENT, String.class);
final Collection<SnomedRefSetType> refSetTypes = getCollection(OptionKey.REFSET_TYPE, SnomedRefSetType.class);
final Options propsFilter = getOptions(OptionKey.PROPS);
ExpressionBuilder queryBuilder = Expressions.builder();
addActiveClause(queryBuilder);
addReleasedClause(queryBuilder);
addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
addEffectiveTimeClause(queryBuilder);
addEclFilter(context, queryBuilder, OptionKey.REFSET, SnomedRefSetMemberIndexEntry.Expressions::refsetIds);
addComponentClause(queryBuilder);
if (containsKey(OptionKey.REFERENCED_COMPONENT_TYPE)) {
queryBuilder.filter(referencedComponentTypes(getCollection(OptionKey.REFERENCED_COMPONENT_TYPE, String.class)));
}
if (!referencedComponentIds.isEmpty()) {
queryBuilder.filter(referencedComponentIds(referencedComponentIds));
}
if (!refSetTypes.isEmpty()) {
queryBuilder.filter(refSetTypes(refSetTypes));
}
if (!propsFilter.isEmpty()) {
final Set<String> propKeys = newHashSet(propsFilter.keySet());
if (propKeys.remove(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID)) {
queryBuilder.filter(acceptabilityIds(propsFilter.getCollection(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP)) {
final String operatorKey = SearchResourceRequest.operator(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP);
SearchResourceRequest.Operator op;
if (propKeys.remove(operatorKey)) {
op = propsFilter.get(operatorKey, Operator.class);
} else {
op = SearchResourceRequest.Operator.EQUALS;
}
switch(op) {
case EQUALS:
queryBuilder.filter(relationshipGroup(propsFilter.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)));
break;
case NOT_EQUALS:
queryBuilder.mustNot(relationshipGroup(propsFilter.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)));
break;
default:
throw new NotImplementedException("Unsupported relationship group operator %s", op);
}
}
if (propKeys.remove(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::characteristicTypeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_CORRELATION_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_CORRELATION_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::correlationIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_DESCRIPTION_FORMAT)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_DESCRIPTION_FORMAT, String.class), SnomedRefSetMemberIndexEntry.Expressions::descriptionFormats);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_CATEGORY_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_CATEGORY_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::mapCategoryIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::targetComponentIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_TARGET)) {
queryBuilder.filter(mapTargets(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_TARGET, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_TARGET_DESCRIPTION)) {
queryBuilder.filter(mapTargetDescriptions(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_TARGET_DESCRIPTION, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_GROUP)) {
queryBuilder.filter(mapGroups(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_GROUP, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_PRIORITY)) {
queryBuilder.filter(mapPriority(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_PRIORITY, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_BLOCK)) {
queryBuilder.filter(mapBlock(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_BLOCK, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_VALUE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_VALUE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::valueIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::typeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::domainIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::contentTypeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::ruleStrengthIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::ruleRefSetIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_GROUPED)) {
queryBuilder.filter(grouped(propsFilter.getBoolean(SnomedRf2Headers.FIELD_MRCM_GROUPED)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT)) {
queryBuilder.filter(rangeConstraint(propsFilter.getString(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_OWL_EXPRESSION)) {
queryBuilder.filter(Expressions.exactMatch(SnomedRf2Headers.FIELD_OWL_EXPRESSION, propsFilter.getString(SnomedRf2Headers.FIELD_OWL_EXPRESSION)));
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_CONCEPTID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_CONCEPTID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionConcept);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_DESTINATIONID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_DESTINATIONID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionDestination);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_TYPEID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_TYPEID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionType);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_GCI)) {
queryBuilder.filter(gciAxiom(propsFilter.getBoolean(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_GCI)));
}
final Collection<DataType> dataTypes = propsFilter.getCollection(SnomedRefSetMemberIndexEntry.Fields.DATA_TYPE, DataType.class);
if (propKeys.remove(SnomedRefSetMemberIndexEntry.Fields.DATA_TYPE)) {
queryBuilder.filter(dataTypes(dataTypes));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_VALUE)) {
if (dataTypes.size() != 1) {
throw new BadRequestException("DataType filter must be specified if filtering by value");
}
final DataType dataType = Iterables.getOnlyElement(dataTypes);
final String operatorKey = SearchResourceRequest.operator(SnomedRf2Headers.FIELD_VALUE);
SearchResourceRequest.Operator op;
if (propKeys.remove(operatorKey)) {
op = propsFilter.get(operatorKey, Operator.class);
} else {
op = SearchResourceRequest.Operator.EQUALS;
}
final Collection<Object> attributeValues = propsFilter.getCollection(SnomedRf2Headers.FIELD_VALUE, Object.class);
switch(op) {
case EQUALS:
queryBuilder.filter(values(dataType, attributeValues));
break;
case NOT_EQUALS:
queryBuilder.mustNot(values(dataType, attributeValues));
break;
case LESS_THAN:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, null, Iterables.getOnlyElement(attributeValues), false, false));
break;
case LESS_THAN_EQUALS:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, null, Iterables.getOnlyElement(attributeValues), false, true));
break;
case GREATER_THAN:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, Iterables.getOnlyElement(attributeValues), null, false, false));
break;
case GREATER_THAN_EQUALS:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, Iterables.getOnlyElement(attributeValues), null, true, false));
break;
default:
throw new NotImplementedException("Unsupported concrete domain value operator %s", op);
}
}
if (!propKeys.isEmpty()) {
throw new IllegalQueryParameterException("Unsupported property filter(s), %s", propKeys);
}
}
return queryBuilder.build();
}
Aggregations