Copy username to search_fields (#11045)

* Copy username to search_fields

* Add test for searching by a username

* Add data update script for reindexing users for username search

* Comment out ReindexUsersForProfiles update script

Co-authored-by: rhymes <rhymes@hey.com>
This commit is contained in:
Krzysztof Rybka 2020-11-02 15:38:15 +01:00 committed by GitHub
parent 6007cadad9
commit 20a19daa67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 4 deletions

View file

@ -58,7 +58,8 @@
"type": "text"
},
"username": {
"type": "keyword"
"type": "keyword",
"copy_to": "search_fields"
},
"profile_fields": {
"type": "nested",

View file

@ -1,9 +1,9 @@
module DataUpdateScripts
class ReindexUsersForProfiles
def run
User.select(:id).in_batches(of: 200) do |batch|
Search::BulkIndexWorker.set(queue: :default).perform_async("User", batch.ids)
end
# User.select(:id).in_batches(of: 200) do |batch|
# Search::BulkIndexWorker.set(queue: :default).perform_async("User", batch.ids)
# end
end
end
end

View file

@ -0,0 +1,9 @@
module DataUpdateScripts
class ReindexUsersForUsernameSearch
def run
User.select(:id).in_batches(of: 200) do |batch|
Search::BulkIndexWorker.set(queue: :default).perform_async("User", batch.ids)
end
end
end
end

View file

@ -0,0 +1,12 @@
require "rails_helper"
require Rails.root.join("lib/data_update_scripts/20201030134117_reindex_users_for_username_search.rb")
describe DataUpdateScripts::ReindexUsersForUsernameSearch do
let(:user) { create :user }
it "reindexes users" do
sidekiq_assert_enqueued_with(job: Search::BulkIndexWorker, args: ["User", [user.id]], queue: "default") do
described_class.new.run
end
end
end

View file

@ -30,6 +30,17 @@ RSpec.describe Search::User, type: :service do
doc_ids = user_docs.map { |t| t["id"] }
expect(doc_ids).to include(user1.id, user2.id)
end
it "searches by a username" do
allow(user1).to receive(:username).and_return("kyloren")
index_documents([user1])
query_params = { size: 5, search_fields: "kyloren" }
user_docs = described_class.search_documents(params: query_params)
expect(user_docs.count).to eq(1)
doc_ids = user_docs.map { |t| t["id"] }
expect(doc_ids).to include(user1.id)
end
end
context "with a filter" do