Skip to content

Commit

Permalink
fix: search should work as (term1 and term2) (#3865)
Browse files Browse the repository at this point in the history
  • Loading branch information
mswertz authored Jun 20, 2024
1 parent 6749beb commit 676c8c7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,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 @@ -425,7 +425,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 @@ -520,8 +520,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 @@ -571,8 +572,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 @@ -1394,7 +1396,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

0 comments on commit 676c8c7

Please sign in to comment.