From 91db45b197377750a88c0ac236554451dc6b567d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 25 Apr 2025 12:35:21 +0200 Subject: [PATCH] Change account search to be more forgiving of spaces (#34455) --- app/chewy/accounts_index.rb | 16 ++++++++++++ app/services/account_search_service.rb | 36 ++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/app/chewy/accounts_index.rb b/app/chewy/accounts_index.rb index 59f2f991f2..796584e9c6 100644 --- a/app/chewy/accounts_index.rb +++ b/app/chewy/accounts_index.rb @@ -19,9 +19,16 @@ class AccountsIndex < Chewy::Index type: 'stemmer', language: 'possessive_english', }, + + word_joiner: { + type: 'shingle', + output_unigrams: true, + token_separator: '', + }, }, analyzer: { + # "The FOOING's bar" becomes "foo bar" natural: { tokenizer: 'standard', filter: %w( @@ -35,11 +42,20 @@ class AccountsIndex < Chewy::Index ), }, + # "FOO bar" becomes "foo bar" verbatim: { tokenizer: 'standard', filter: %w(lowercase asciifolding cjk_width), }, + # "Foo bar" becomes "foo bar foobar" + word_join_analyzer: { + type: 'custom', + tokenizer: 'standard', + filter: %w(lowercase asciifolding cjk_width word_joiner), + }, + + # "Foo bar" becomes "f fo foo b ba bar" edge_ngram: { tokenizer: 'edge_ngram', filter: %w(lowercase asciifolding cjk_width), diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index dab5f748bf..5261040884 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -128,11 +128,37 @@ class AccountSearchService < BaseService def core_query { - multi_match: { - query: @query, - type: 'best_fields', - fields: %w(username^2 display_name^2 text text.*), - operator: 'and', + dis_max: { + queries: [ + { + match: { + username: { + query: @query, + analyzer: 'word_join_analyzer', + }, + }, + }, + + { + match: { + display_name: { + query: @query, + analyzer: 'word_join_analyzer', + }, + }, + }, + + { + multi_match: { + query: @query, + type: 'best_fields', + fields: %w(text text.*), + operator: 'and', + }, + }, + ], + + tie_breaker: 0.5, }, } end