From 20a19daa67856c492a6cc70d38214398558e5768 Mon Sep 17 00:00:00 2001 From: Krzysztof Rybka Date: Mon, 2 Nov 2020 15:38:15 +0100 Subject: [PATCH] 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 --- config/elasticsearch/mappings/users.json | 3 ++- .../20200914042434_reindex_users_for_profiles.rb | 6 +++--- ...201030134117_reindex_users_for_username_search.rb | 9 +++++++++ .../reindex_users_for_username_search_spec.rb | 12 ++++++++++++ spec/services/search/user_spec.rb | 11 +++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 lib/data_update_scripts/20201030134117_reindex_users_for_username_search.rb create mode 100644 spec/lib/data_update_scripts/reindex_users_for_username_search_spec.rb diff --git a/config/elasticsearch/mappings/users.json b/config/elasticsearch/mappings/users.json index 499d93b4a..c7f6e594a 100644 --- a/config/elasticsearch/mappings/users.json +++ b/config/elasticsearch/mappings/users.json @@ -58,7 +58,8 @@ "type": "text" }, "username": { - "type": "keyword" + "type": "keyword", + "copy_to": "search_fields" }, "profile_fields": { "type": "nested", diff --git a/lib/data_update_scripts/20200914042434_reindex_users_for_profiles.rb b/lib/data_update_scripts/20200914042434_reindex_users_for_profiles.rb index aa0fb4e11..da64d31c9 100644 --- a/lib/data_update_scripts/20200914042434_reindex_users_for_profiles.rb +++ b/lib/data_update_scripts/20200914042434_reindex_users_for_profiles.rb @@ -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 diff --git a/lib/data_update_scripts/20201030134117_reindex_users_for_username_search.rb b/lib/data_update_scripts/20201030134117_reindex_users_for_username_search.rb new file mode 100644 index 000000000..e131bef58 --- /dev/null +++ b/lib/data_update_scripts/20201030134117_reindex_users_for_username_search.rb @@ -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 diff --git a/spec/lib/data_update_scripts/reindex_users_for_username_search_spec.rb b/spec/lib/data_update_scripts/reindex_users_for_username_search_spec.rb new file mode 100644 index 000000000..bd9826833 --- /dev/null +++ b/spec/lib/data_update_scripts/reindex_users_for_username_search_spec.rb @@ -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 diff --git a/spec/services/search/user_spec.rb b/spec/services/search/user_spec.rb index e31ac92da..9075dad63 100644 --- a/spec/services/search/user_spec.rb +++ b/spec/services/search/user_spec.rb @@ -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