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
  • Loading branch information
gaetanmaisse authored and ytvnr committed Jul 4, 2023
1 parent fbfebce commit 28437e9
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 @@ -250,7 +250,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 @@ -370,6 +371,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 28437e9

Please sign in to comment.