From 289cf9b4db283492cac936b21cce1d58b6ac21ca Mon Sep 17 00:00:00 2001 From: Lakshmi Murugappan Date: Tue, 2 Sep 2025 16:09:00 +0100 Subject: [PATCH] Remove fuzzy-search from cascading PDS search algorithm We have seen at least two cases where fuzzy matching in PDS returned a patient that was obviously wrong (first name and last name swapped around and spellings completely different, and postcode different). We think it is safest to remove the last search step (fuzzy step) from the cascading search --- app/jobs/process_patient_changesets_job.rb | 19 ++++--------------- spec/factories/patient_changesets.rb | 2 +- ...port_child_pds_lookup_extravaganza_spec.rb | 12 ++---------- .../process_patient_changesets_job_spec.rb | 9 +++++---- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/app/jobs/process_patient_changesets_job.rb b/app/jobs/process_patient_changesets_job.rb index f1ef85ee9b..706e0cbf35 100644 --- a/app/jobs/process_patient_changesets_job.rb +++ b/app/jobs/process_patient_changesets_job.rb @@ -79,7 +79,7 @@ def steps too_many_matches: :no_fuzzy_without_history }, no_fuzzy_without_history: { - no_matches: :fuzzy, + no_matches: :give_up, one_match: :save_nhs_number_if_unique, too_many_matches: :give_up, format_query: ->(query) { query.merge(history: false) } @@ -98,22 +98,11 @@ def steps format_query: ->(query) { query[:given_name][3..] = "*" } }, no_fuzzy_with_wildcard_family_name: { - no_matches: :fuzzy, - one_match: :fuzzy, - too_many_matches: :fuzzy, - skip_step: :fuzzy, - format_query: ->(query) { query[:family_name][3..] = "*" } - }, - fuzzy: { - no_matches: :give_up, + no_matches: :save_nhs_number_if_unique, one_match: :save_nhs_number_if_unique, too_many_matches: :save_nhs_number_if_unique, - format_query: ->(query) do - query[:fuzzy] = true - # For fuzzy searches, history is the default. We get an error if we - # try to set it to true explicitly - query[:history] = nil - end + skip_step: :save_nhs_number_if_unique, + format_query: ->(query) { query[:family_name][3..] = "*" } } } end diff --git a/spec/factories/patient_changesets.rb b/spec/factories/patient_changesets.rb index 01e777c80a..9da0e1f2a3 100644 --- a/spec/factories/patient_changesets.rb +++ b/spec/factories/patient_changesets.rb @@ -46,7 +46,7 @@ { child: { "given_name" => "John", - "family_name" => "Doe", + "family_name" => "Dover", "date_of_birth" => "2010-01-01", "address_postcode" => "SW1A 1AA", "nhs_number" => nil diff --git a/spec/features/import_child_pds_lookup_extravaganza_spec.rb b/spec/features/import_child_pds_lookup_extravaganza_spec.rb index 0b09bc0894..27f6a4b9c3 100644 --- a/spec/features/import_child_pds_lookup_extravaganza_spec.rb +++ b/spec/features/import_child_pds_lookup_extravaganza_spec.rb @@ -350,7 +350,7 @@ def and_pds_lookup_during_import_is_enabled address_postcode: "B1 1AA", steps: { wildcard_postcode: "1111111111", - fuzzy: "9435726097" + wildcard_family_name: "9435726097" } ) end @@ -387,13 +387,6 @@ def stub_pds_cascading_search( "given" => given_name, "birthdate" => birthdate, "address-postalcode" => address_postcode - }, - fuzzy: { - "family" => family_name, - "given" => given_name, - "birthdate" => birthdate, - "address-postalcode" => address_postcode, - "_fuzzy-match" => "true" } }.each do |step, query| if steps[step] @@ -827,14 +820,13 @@ def then_maia_has_the_uploaded_nhs_number def and_maia_has_multiple_pds_search_results maia = Patient.find_by(given_name: "Maia", family_name: "Smith") - expect(maia.pds_search_results.count).to eq(5) + expect(maia.pds_search_results.count).to eq(4) expect(maia.pds_search_results.pluck(:step)).to eq( %w[ no_fuzzy_with_history no_fuzzy_with_wildcard_postcode no_fuzzy_with_wildcard_given_name no_fuzzy_with_wildcard_family_name - fuzzy ] ) end diff --git a/spec/jobs/process_patient_changesets_job_spec.rb b/spec/jobs/process_patient_changesets_job_spec.rb index dbcdf9bae3..9d54133b08 100644 --- a/spec/jobs/process_patient_changesets_job_spec.rb +++ b/spec/jobs/process_patient_changesets_job_spec.rb @@ -68,8 +68,8 @@ end end - context "when a later fuzzy search finds a match" do - let(:step) { :fuzzy } + context "when a later wildcard-family-name search finds a match" do + let(:step) { :no_fuzzy_with_wildcard_family_name } before do patient_changeset["pending_changes"]["search_results"] = [ @@ -94,8 +94,8 @@ end end - context "when fuzzy search returns conflicting NHS numbers" do - let(:step) { :fuzzy } + context "when wildcard-family-name search returns conflicting NHS numbers" do + let(:step) { :no_fuzzy_with_wildcard_family_name } before do patient_changeset["pending_changes"]["search_results"] = [ @@ -115,6 +115,7 @@ perform expect(patient_changeset.child_attributes["nhs_number"]).to be_blank + expect(patient_changeset.pds_nhs_number).to be_nil end end