Skip to content

Commit

Permalink
fix: add a boost on the API name to ensure search result are sorted i…
Browse files Browse the repository at this point in the history
…n a more relevant way

https://gravitee.atlassian.net/browse/APIM-1991
gravitee-io/issues#9095
(cherry picked from commit 28437e9)
(cherry picked from commit b17bf40)
  • Loading branch information
gaetanmaisse authored and mergify[bot] committed Jul 4, 2023
1 parent 07b3c2c commit 8d72519
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ protected Set<String> appendExplicitFilters(String query, BooleanQuery.Builder m
throws ParseException {
QueryParser parser = new QueryParser("", new KeywordAnalyzer());
parser.setAllowLeadingWildcard(true);
String escapedQuery = query;
// Escape [ and ] because they can be used in API names
String escapedQuery = query.replace("[", "\\[").replace("]", "\\]");
if (escapedQuery.startsWith("/")) { // escape if we are looking for a path
escapedQuery = QueryParserBase.escape(query);
}
Expand Down Expand Up @@ -394,6 +395,13 @@ private BooleanQuery buildApiFields(String query, Query... queries) {
}

String[] tokens = query.split(" ");

// Add boost on exact match on name
builder
.add(new BoostQuery(toWildcard(FIELD_NAME, query), 20.0f), BooleanClause.Occur.SHOULD)
.add(new BoostQuery(toWildcard(FIELD_NAME_LOWERCASE, query.toLowerCase()), 18.0f), BooleanClause.Occur.SHOULD);

// Add boost for partial match
for (String token : tokens) {
builder
.add(new BoostQuery(toWildcard(FIELD_NAME, token), 12.0f), BooleanClause.Occur.SHOULD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void shouldCompleteQueryWithFiltersAndText() {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
assertEquals("", searcher.completeQueryWithFilters(query, builder));
assertEquals(
"#(+(categories:\"sports\" categories:sports)) +(+((name:*Cycling*)^12.0 (name_lowercase:*cycling*)^10.0 (paths:*Cycling*)^8.0 description:*Cycling* description_lowercase:*cycling* hosts:*Cycling* labels:*Cycling* categories:*Cycling* tags:*Cycling* metadata:*Cycling*))",
"#(+(categories:\"sports\" categories:sports)) +(+((name:*Cycling*)^20.0 (name_lowercase:*cycling*)^18.0 (name:*Cycling*)^12.0 (name_lowercase:*cycling*)^10.0 (paths:*Cycling*)^8.0 description:*Cycling* description_lowercase:*cycling* hosts:*Cycling* labels:*Cycling* categories:*Cycling* tags:*Cycling* metadata:*Cycling*))",
builder.build().toString()
);
}
Expand Down

0 comments on commit 8d72519

Please sign in to comment.