Return Additional Data Needed for Autocomplete (#12823)
* Return Additional Data Needed for Autocomplete * return name instead of username twice
This commit is contained in:
parent
f975b8b737
commit
37f609e5c5
4 changed files with 19 additions and 7 deletions
|
|
@ -10,7 +10,12 @@ module Search
|
|||
def search_usernames(username)
|
||||
results = search(body: username_query(username))
|
||||
results.dig("hits", "hits").map do |doc|
|
||||
doc.dig("_source", "username")
|
||||
source = doc["_source"]
|
||||
{
|
||||
"username" => source["username"],
|
||||
"name" => source["name"],
|
||||
"profile_image_90" => source["profile_image_90"]
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@
|
|||
'/search/reactions',
|
||||
'/search/tags',
|
||||
'/search/users',
|
||||
'/search/usernames',
|
||||
'/shell_', // Don't fetch for shell.
|
||||
'/shop', // redirects
|
||||
'/sidekiq', // Skip for Sidekiq dashboard
|
||||
|
|
|
|||
|
|
@ -68,15 +68,21 @@ RSpec.describe "Search", type: :request, proper_status: true do
|
|||
|
||||
describe "GET /search/usernames" do
|
||||
let(:authorized_user) { create(:user) }
|
||||
let(:names) { ["username"] }
|
||||
let(:result) do
|
||||
{
|
||||
"username" => "molly",
|
||||
"name" => "Molly",
|
||||
"profile_image_90" => "something"
|
||||
}
|
||||
end
|
||||
|
||||
it "returns json" do
|
||||
sign_in authorized_user
|
||||
allow(Search::User).to receive(:search_usernames).and_return(
|
||||
names,
|
||||
result,
|
||||
)
|
||||
get "/search/usernames"
|
||||
expect(response.parsed_body).to eq("result" => names)
|
||||
expect(response.parsed_body).to eq("result" => result)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ RSpec.describe Search::User, type: :service do
|
|||
it "searches with username" do
|
||||
usernames = described_class.search_usernames(user1.username)
|
||||
expect(usernames.count).to eq(1)
|
||||
expect(usernames).to match([user1.username])
|
||||
expect(usernames.map { |u| u["username"] }).to match([user1.username])
|
||||
end
|
||||
|
||||
it "analyzes wildcards" do
|
||||
|
|
@ -77,10 +77,10 @@ RSpec.describe Search::User, type: :service do
|
|||
index_documents([user3])
|
||||
|
||||
usernames = described_class.search_usernames("star*")
|
||||
expect(usernames).to match(
|
||||
expect(usernames.map { |u| u["username"] }).to match(
|
||||
[user1.username, user2.username],
|
||||
)
|
||||
expect(usernames).not_to include(user3.username)
|
||||
expect(usernames.map { |u| u["username"] }).not_to include(user3.username)
|
||||
end
|
||||
|
||||
it "does not allow leading wildcards" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue