Skip to content

Commit 4951476

Browse files
committed
Prioritize matches by whether the return tokens match
1 parent 2a16ea6 commit 4951476

File tree

1 file changed

+13
-4
lines changed
  • src/Share/Postgres/Search/DefinitionSearch

1 file changed

+13
-4
lines changed

src/Share/Postgres/Search/DefinitionSearch/Queries.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
307307
Just (ReleaseFilter relId) -> [sql| AND doc.release_id = #{relId} |]
308308
Just (UserFilter userId) -> [sql| AND p.owner_id = #{userId} |]
309309
Nothing -> mempty
310-
let tsQueryText = searchTokensToTsQuery searchTokens
310+
let (regularTokens, returnTokens) =
311+
searchTokens & foldMap \token -> case token of
312+
TypeMentionToken _ ReturnPosition -> (mempty, Set.singleton token)
313+
TypeVarToken _ ReturnPosition -> (mempty, Set.singleton token)
314+
_ -> (Set.singleton token, mempty)
315+
let tsQueryText = searchTokensToTsQuery regularTokens
316+
let returnTokensText = searchTokensToTsQuery returnTokens
311317
rows <-
312318
queryListRows @(ProjectId, ReleaseId, Name, Hasql.Jsonb)
313319
[sql|
@@ -323,9 +329,12 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
323329
ORDER BY doc.project_id, doc.name, r.major_version, r.minor_version, r.patch_version
324330
) SELECT m.project_id, m.release_id, m.name, m.metadata
325331
FROM matches_deduped_by_project m
326-
-- prefer results which have at LEAST the requested arity, then prefer shorter
327-
-- arities.
328-
ORDER BY (m.arity >= #{preferredArity}) DESC, m.arity ASC, m.num_search_tokens ASC
332+
-- Score matches by:
333+
-- * Whether arity is equal or greater than the preferred arity
334+
-- * Whether the return type of the query matches the type signature
335+
-- * Prefer shorter arities
336+
-- * Prefer less complex type signatures (by number of tokens)
337+
ORDER BY (m.arity >= #{preferredArity}) DESC, tsquery(#{returnTokensText}) @@ doc.search_tokens DESC, m.arity ASC, m.num_search_tokens ASC
329338
LIMIT #{limit}
330339
|]
331340
rows & traverseOf (traversed . _4) \(Hasql.Jsonb v) -> do

0 commit comments

Comments
 (0)