Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search should work as (term1 and term2) #3865

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,14 @@ public static FilterBean[] convertMapToFilterArray(
subFilters.add(and(nestedFilters.toArray(new Filter[nestedFilters.size()])));
}
} else if (entry.getKey().equals(FILTER_SEARCH)) {
subFilters.add(f(Operator.TRIGRAM_SEARCH, entry.getValue()));
if (entry.getValue() instanceof String && !entry.getValue().toString().trim().equals("")) {
subFilters.add(
f(
Operator
.TEXT_SEARCH, // might change to trigram search if we learn how to tune for
// short terms
Arrays.stream(entry.getValue().toString().split(" ")).toArray(String[]::new)));
}
} else if (entry.getKey().equals(FILTER_EQUALS)) {
// complex filter, should be an list of maps per graphql contract
if (entry.getValue() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private List<Condition> jsonFilterQueryConditions(
} else if (Operator.AND.equals(f.getOperator())) {
conditions.add(
and(jsonFilterQueryConditions(table, column, tableAlias, subAlias, f, searchTerms)));
} else if (TRIGRAM_SEARCH.equals(f.getOperator())) {
} else if (TRIGRAM_SEARCH.equals(f.getOperator()) || TEXT_SEARCH.equals(f.getOperator())) {
conditions.add(
jsonSearchConditions(table, subAlias, TypeUtils.toStringArray(f.getValues())));
} else {
Expand Down Expand Up @@ -519,8 +519,9 @@ private List<Condition> jsonFilterQueryConditions(
private Condition jsonSearchConditions(
SqlTableMetadata table, String subAlias, String[] searchTerms) {
// create search
List<Condition> search = new ArrayList<>();
List<Condition> searchCondition = new ArrayList<>();
for (String term : searchTerms) {
List<Condition> search = new ArrayList<>();
search.add(
field(name(alias(subAlias), searchColumnName(table.getTableName())))
.likeIgnoreCase("%" + term + "%"));
Expand Down Expand Up @@ -570,8 +571,9 @@ private Condition jsonSearchConditions(
.likeIgnoreCase("%" + term + "%"));
parent = parent.getInheritedTable();
}
searchCondition.add(or(search));
}
return or(search);
return and(searchCondition);
}

private Collection<Field<?>> jsonSubselectFields(
Expand Down Expand Up @@ -1382,7 +1384,7 @@ private Condition whereConditionSearch(
searchConditions.add(and(subConditions));
}
}
return searchConditions.isEmpty() ? null : or(searchConditions);
return searchConditions.isEmpty() ? null : and(searchConditions);
}

private static SelectJoinStep<org.jooq.Record> limitOffsetOrderBy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ public void nestedSearch() {
// would exclude approved order
assertTrue(!json.contains("approved"));

// test term1 AND term2
json = schema.query("Pet").where(f(Operator.TEXT_SEARCH, "red", "green")).retrieveJSON();
// would be spike and fire ant
assertTrue(json.contains("spike"));
assertTrue(json.contains("fire ant"));
assertTrue(!json.contains("tweety"));

json =
schema
.query("Order")
.where(f("pet", f(Operator.TEXT_SEARCH, "red", "green")))
.retrieveJSON();
// would be only spike
assertTrue(json.contains("spike"));

// nesting example 2
json =
schema
Expand Down