diff --git a/app/lib/search_query_transformer.rb b/app/lib/search_query_transformer.rb index 27b0a4653c5972..434e24573dd1e5 100644 --- a/app/lib/search_query_transformer.rb +++ b/app/lib/search_query_transformer.rb @@ -18,6 +18,7 @@ def initialize(clauses, options = {}) @clauses = clauses @options = options + @searchability = options[:searchability]&.to_sym || :public flags_from_clauses! end @@ -64,33 +65,128 @@ def indexes end def default_filter + definition_should = [ + default_should1, + default_should2, + non_publicly_searchable, + searchability_limited, + ] + definition_should << searchability_public if %i(public).include?(@searchability) + definition_should << searchability_private if %i(public private).include?(@searchability) + + { + bool: { + should: definition_should, + minimum_should_match: 1, + }, + } + end + + def default_should1 + { + term: { + _index: PublicStatusesIndex.index_name, + }, + } + end + + def default_should2 { bool: { - should: [ + must: [ { term: { - _index: PublicStatusesIndex.index_name, + _index: StatusesIndex.index_name, }, }, { - bool: { - must: [ - { - term: { - _index: StatusesIndex.index_name, - }, - }, - { - term: { - searchable_by: @options[:current_account].id, - }, - }, - ], + term: { + searchable_by: @options[:current_account].id, }, }, ], + }, + } + end - minimum_should_match: 1, + def non_publicly_searchable + { + bool: { + must: [ + { + term: { _index: StatusesIndex.index_name }, + }, + { + exists: { + field: 'searchability', + }, + }, + { + term: { searchable_by: @account.id }, + }, + ], + must_not: [ + { + term: { searchability: 'limited' }, + }, + ], + }, + } + end + + def searchability_public + { + bool: { + must: [ + { + exists: { + field: 'searchability', + }, + }, + { + term: { searchability: 'public' }, + }, + ], + }, + } + end + + def searchability_private + { + bool: { + must: [ + { + exists: { + field: 'searchability', + }, + }, + { + term: { searchability: 'private' }, + }, + { + terms: { account_id: following_account_ids }, + }, + ], + }, + } + end + + def searchability_limited + { + bool: { + must: [ + { + exists: { + field: 'searchability', + }, + }, + { + term: { searchability: 'limited' }, + }, + { + term: { account_id: @account.id }, + }, + ], }, } end diff --git a/yarn.lock b/yarn.lock index 58cd2d1826be3a..0d10238e35c2f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1087,7 +1087,7 @@ dependencies: regenerator-runtime "^0.12.0" -"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.0", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.8", "@babel/runtime@^7.18.3", "@babel/runtime@^7.2.0", "@babel/runtime@^7.20.13", "@babel/runtime@^7.20.7", "@babel/runtime@^7.22.3", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.5", "@babel/runtime@^7.6.3", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2": +"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.0", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.8", "@babel/runtime@^7.15.4", "@babel/runtime@^7.18.3", "@babel/runtime@^7.2.0", "@babel/runtime@^7.20.13", "@babel/runtime@^7.20.7", "@babel/runtime@^7.22.3", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.5", "@babel/runtime@^7.6.3", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2": version "7.22.15" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.15.tgz#38f46494ccf6cf020bd4eed7124b425e83e523b8" integrity sha512-T0O+aa+4w0u06iNmapipJXMV4HoUir03hpx3/YqXXhu9xim3w+dVphjFWl1OH8NbZHw5Lbm9k45drDkgq2VNNA==