Skip to content
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
15 changes: 10 additions & 5 deletions api/searchv1/dsl_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func sortWithField(innerQuery map[string]any, sortField, direction string) strin
return dsl
}

// countScoreFunction scales a score by count+1. A plain field_value_factor
// multiplies by the raw count, which zeroes out anything with a count of 0
// and buries it below every other match, exact or not.
func countScoreFunction(field string) string {
source := fmt.Sprintf("(doc['%s'].size() == 0 ? 0 : doc['%s'].value) + 1", field, field)
return fmt.Sprintf(`"script_score": {"script": {"source": %q}}`, source)
}

func BuildFunctionScoreDSL(scoreField string, weight float64, innerQuery map[string]any) string {
innerJson, err := json.Marshal(innerQuery)
if err != nil {
Expand All @@ -51,18 +59,15 @@ func BuildFunctionScoreDSL(scoreField string, weight float64, innerQuery map[str
"query": %s,
"functions": [
{
"field_value_factor": {
"field": %q,
"missing": 0
},
%s,
"weight": %g
}
],
"boost_mode": "multiply",
"score_mode": "multiply"
}
}
}`, innerJson, scoreField, weight)
}`, innerJson, countScoreFunction(scoreField), weight)

return dsl
}
Expand Down
7 changes: 2 additions & 5 deletions api/searchv1/user_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ func buildUserFunctionScoreDSL(followerWeight float64, queryString string, inner
"query": %s,
"functions": [
{
"field_value_factor": {
"field": "follower_count",
"missing": 0
},
%s,
"weight": %g
},
{
Expand Down Expand Up @@ -147,7 +144,7 @@ func buildUserFunctionScoreDSL(followerWeight float64, queryString string, inner
"score_mode": "multiply"
}
}
}`, innerJson, followerWeight, queryNoSpaces, queryNoSpaces)
}`, innerJson, countScoreFunction("follower_count"), followerWeight, queryNoSpaces, queryNoSpaces)

return dsl
}
22 changes: 22 additions & 0 deletions api/v1_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func TestSearch(t *testing.T) {
"handle": "find_infix_works",
"name": "asdf",
},
{
"user_id": 1005,
"handle": "zerofans",
"name": "zerofans",
},
{
"user_id": 1006,
"handle": "zerofansclub",
"name": "zerofansclub",
},
},
"tracks": {
{
Expand Down Expand Up @@ -151,6 +161,7 @@ func TestSearch(t *testing.T) {
},
"follows": {
{"follower_user_id": 1001, "followee_user_id": 1002},
{"follower_user_id": 1001, "followee_user_id": 1006},
},
"reposts": {
{
Expand Down Expand Up @@ -211,6 +222,17 @@ func TestSearch(t *testing.T) {
})
}

// users: an exact handle match with no followers still ranks first
{
status, body := testGet(t, app, "/v1/users/search?query=zerofans")
require.Equal(t, 200, status)
jsonAssert(t, body, map[string]any{
"data.#": 2,
"data.0.handle": "zerofans",
"data.1.handle": "zerofansclub",
})
}

{
status, body := testGet(t, app, "/v1/users/search?query=monoist")
require.Equal(t, 200, status)
Expand Down
Loading