Skip to content

Commit

Permalink
fix(ecl): use wildcard care instead of handcrafted regex query...
Browse files Browse the repository at this point in the history
...for wild lexical search type
  • Loading branch information
cmark committed May 13, 2024
1 parent b904b47 commit 030304a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2022-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -512,8 +512,7 @@ protected Expression toExpression(final TypedSearchTermClause clause) {
if (WILD_ANY.matcher(term).matches()) {
return Expressions.matchAll();
} else {
final String regex = term.replace("*", ".*");
return termRegexExpression(regex, true);
return termWildExpression(term, true);
}
case REGEX:
if (REGEX_ANY.matcher(term).matches()) {
Expand Down Expand Up @@ -556,6 +555,10 @@ protected Expression termCaseInsensitiveExpression(String term) {
return throwUnsupported("Unable to provide case insensitive term expression for term filter: " + term);
}

protected Expression termWildExpression(String wild, boolean caseInsensitive) {
return throwUnsupported("Unable to provide wild term expression for term filter: " + wild);

Check warning on line 559 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ecl/EclEvaluationRequest.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ecl/EclEvaluationRequest.java#L559

Added line #L559 was not covered by tests
}

protected Expression termRegexExpression(String regex, boolean caseInsensitive) {
return throwUnsupported("Unable to provide regex term expression for term filter: " + regex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,31 @@ public void termRegexAnyCharacterShouldNotCreateQueryClause() throws Exception {
assertEquals(expected, actualThree);
}

@Test
public void termWildEscapedAnyCharacterShouldNotGenerateInvalidRegexQuery() throws Exception {
indexRevision(MAIN, SnomedDescriptionIndexEntry.builder()
.id(generateDescriptionId())
.active(true)
.moduleId(Concepts.MODULE_SCT_CORE)
.term("Clinical finding with * character")
.conceptId(Concepts.ALL_SNOMEDCT_CONTENT)
.typeId(Concepts.TEXT_DEFINITION)
.build());

indexRevision(MAIN, SnomedDescriptionIndexEntry.builder()
.id(generateDescriptionId())
.active(true)
.moduleId(Concepts.MODULE_SCT_CORE)
.term("Clinical finding without the any character")
.conceptId(Concepts.ALL_PRECOORDINATED_CONTENT)
.typeId(Concepts.TEXT_DEFINITION)
.build());

final Expression actual = eval("* {{ term = wild:'*\\\\**\' }}");
Expression expected = SnomedDocument.Expressions.ids(Set.of(Concepts.ALL_SNOMEDCT_CONTENT));
assertEquals(expected, actual);
}

@Test
public void disjunctionActiveAndModuleId() throws Exception {
final Expression actual = eval("* {{ c active = true OR moduleId = " + Concepts.MODULE_SCT_CORE + " }}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import jakarta.validation.constraints.NotNull;

import org.eclipse.emf.ecore.EObject;

import com.b2international.commons.collections.Collections3;
Expand Down Expand Up @@ -63,6 +61,8 @@
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;

import jakarta.validation.constraints.NotNull;

/**
* Evaluates the given ECL expression {@link String} or parsed {@link ExpressionConstraint} to an executable {@link Expression query expression}.
* <p>
Expand Down Expand Up @@ -632,6 +632,11 @@ protected Expression termRegexExpression(String regex, boolean caseInsensitive)
return Expressions.regexp(SnomedDescriptionIndexEntry.Fields.TERM, regex, caseInsensitive);
}

@Override
protected Expression termWildExpression(String wild, boolean caseInsensitive) {
return Expressions.wildcard(SnomedDescriptionIndexEntry.Fields.TERM, wild, caseInsensitive);
}

@Override
protected Expression termCaseInsensitiveExpression(String term) {
return com.b2international.snowowl.core.request.search.TermFilter.exact().term(term).caseSensitive(false).build().toExpression(SnomedDescriptionIndexEntry.Fields.TERM);
Expand Down

0 comments on commit 030304a

Please sign in to comment.