Improved search results for localized nickname match. Tweaked user search to rank nickname matches higher than name matches.

This commit is contained in:
Ivan Tashkinov 2020-07-08 13:14:18 +03:00
parent a8447c3803
commit 704a383055
3 changed files with 44 additions and 15 deletions

View file

@ -88,15 +88,21 @@ defp to_tsquery(query_string) do
|> Enum.join(" | ")
end
# Considers nickname match, localized nickname match, name match; preferences nickname match
defp trigram_rank(query, query_string) do
from(
u in query,
select_merge: %{
search_rank:
fragment(
"similarity(?, trim(? || ' ' || coalesce(?, '')))",
"similarity(?, ?) + \
similarity(?, regexp_replace(?, '@.+', '')) + \
similarity(?, trim(coalesce(?, '')))",
^query_string,
u.nickname,
^query_string,
u.nickname,
^query_string,
u.name
)
}

View file

@ -464,17 +464,17 @@ test "it returns users matching" do
moot = insert(:user, nickname: "moot")
kawen = insert(:user, nickname: "kawen", name: "fediverse expert moon")
{:ok, user} = User.follow(user, kawen)
{:ok, user} = User.follow(user, moon)
assert [moon.id, kawen.id] == User.Search.search("moon") |> Enum.map(& &1.id)
res = User.search("moo") |> Enum.map(& &1.id)
assert moon.id in res
assert moot.id in res
assert kawen.id in res
assert [moon.id, kawen.id] == User.Search.search("moon fediverse") |> Enum.map(& &1.id)
assert [kawen.id, moon.id] ==
User.Search.search("moon fediverse", for_user: user) |> Enum.map(& &1.id)
res = User.search("moo") |> Enum.map(& &1.id)
assert Enum.sort([moon.id, moot.id, kawen.id]) == Enum.sort(res)
assert [kawen.id, moon.id] == User.Search.search("expert fediverse") |> Enum.map(& &1.id)
assert [moon.id, kawen.id] ==
User.Search.search("expert fediverse", for_user: user) |> Enum.map(& &1.id)
end
end

View file

@ -46,30 +46,53 @@ test "accepts offset parameter" do
assert length(User.search("john", limit: 3, offset: 3)) == 2
end
test "finds a user by full or partial nickname" do
defp clear_virtual_fields(user) do
Map.merge(user, %{search_rank: nil, search_type: nil})
end
test "finds a user by full nickname or its leading fragment" do
user = insert(:user, %{nickname: "john"})
Enum.each(["john", "jo", "j"], fn query ->
assert user ==
User.search(query)
|> List.first()
|> Map.put(:search_rank, nil)
|> Map.put(:search_type, nil)
|> clear_virtual_fields()
end)
end
test "finds a user by full or partial name" do
test "finds a user by full name or leading fragment(s) of its words" do
user = insert(:user, %{name: "John Doe"})
Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
assert user ==
User.search(query)
|> List.first()
|> Map.put(:search_rank, nil)
|> Map.put(:search_type, nil)
|> clear_virtual_fields()
end)
end
test "is not [yet] capable of matching by non-leading fragments (e.g. by domain)" do
user1 = insert(:user, %{nickname: "iamthedude"})
insert(:user, %{nickname: "arandom@dude.com"})
assert [] == User.search("dude")
# Matching by leading fragment works, though
user1_id = user1.id
assert ^user1_id = User.search("iam") |> List.first() |> Map.get(:id)
end
test "ranks full nickname match higher than full name match" do
nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
results = User.search("hj")
assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
end
test "finds users, considering density of matched tokens" do
u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})