From 1e8a666ec5e860b81eaddc906637a56570395a99 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Sun, 7 Jul 2024 18:48:52 +1000 Subject: [PATCH] Add search_none/none scopes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When called directly on a model, the method is `search_none`: Article.search_none But otherwise, it’s available as just `none`, either on ThinkingSphinx, or through an existing Thinking Sphinx scope: ThinkingSphinx.none Article.search(“something”).none --- lib/thinking_sphinx.rb | 4 ++++ lib/thinking_sphinx/active_record/base.rb | 4 ++++ lib/thinking_sphinx/masks/scopes_mask.rb | 6 ++++++ lib/thinking_sphinx/search.rb | 4 ++-- lib/thinking_sphinx/search/context.rb | 1 + spec/acceptance/searching_across_models_spec.rb | 7 +++++++ spec/acceptance/searching_within_a_model_spec.rb | 7 +++++++ spec/acceptance/sphinx_scopes_spec.rb | 7 +++++++ 8 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/thinking_sphinx.rb b/lib/thinking_sphinx.rb index 60a378313..89de63a2a 100644 --- a/lib/thinking_sphinx.rb +++ b/lib/thinking_sphinx.rb @@ -34,6 +34,10 @@ def self.search_for_ids(query = '', options = {}) ThinkingSphinx::Search::Merger.new(search).merge! nil, :ids_only => true end + def self.none + ThinkingSphinx::Search.new nil, :none => true + end + def self.before_index_hooks @before_index_hooks end diff --git a/lib/thinking_sphinx/active_record/base.rb b/lib/thinking_sphinx/active_record/base.rb index ebc656ade..6c9935ecb 100644 --- a/lib/thinking_sphinx/active_record/base.rb +++ b/lib/thinking_sphinx/active_record/base.rb @@ -58,6 +58,10 @@ def sphinx_search_for_ids(query = nil, options = {}) ).merge! nil, :ids_only => true end + def sphinx_search_none + merge ThinkingSphinx.search, nil, none: true + end + private def default_sphinx_scope? diff --git a/lib/thinking_sphinx/masks/scopes_mask.rb b/lib/thinking_sphinx/masks/scopes_mask.rb index 92aa20b4e..2dd773d8b 100644 --- a/lib/thinking_sphinx/masks/scopes_mask.rb +++ b/lib/thinking_sphinx/masks/scopes_mask.rb @@ -26,6 +26,12 @@ def search_for_ids(query = nil, options = {}) search query, options.merge(:ids_only => true) end + def none + ThinkingSphinx::Search::Merger.new(@search).merge! nil, :none => true + end + + alias_method :search_none, :none + private def apply_scope(scope, *args) diff --git a/lib/thinking_sphinx/search.rb b/lib/thinking_sphinx/search.rb index 617e721f2..97b1799c9 100644 --- a/lib/thinking_sphinx/search.rb +++ b/lib/thinking_sphinx/search.rb @@ -12,7 +12,7 @@ class ThinkingSphinx::Search < Array [ :classes, :conditions, :excerpts, :geo, :group_by, :ids_only, :ignore_scopes, :indices, :limit, :masks, :max_matches, :middleware, - :offset, :order, :order_group_by, :page, :per_page, :populate, + :none, :offset, :order, :order_group_by, :page, :per_page, :populate, :retry_stale, :select, :skip_sti, :sql, :star, :with, :with_all, :without, :without_ids ] + @@ -92,7 +92,7 @@ def per_page(value = nil) def populate return self if @populated - middleware.call [context] + middleware.call [context] unless options[:none] @populated = true self diff --git a/lib/thinking_sphinx/search/context.rb b/lib/thinking_sphinx/search/context.rb index 8d3e692dd..b1f051b3a 100644 --- a/lib/thinking_sphinx/search/context.rb +++ b/lib/thinking_sphinx/search/context.rb @@ -7,6 +7,7 @@ def initialize(search, configuration = nil) @search = search @configuration = configuration || ThinkingSphinx::Configuration.instance @memory = { + :raw => [], :results => [], :panes => ThinkingSphinx::Configuration::Defaults::PANES.clone } diff --git a/spec/acceptance/searching_across_models_spec.rb b/spec/acceptance/searching_across_models_spec.rb index ab0d9ce5a..e471fcd71 100644 --- a/spec/acceptance/searching_across_models_spec.rb +++ b/spec/acceptance/searching_across_models_spec.rb @@ -37,4 +37,11 @@ expect(ThinkingSphinx.search(:classes => [User, Article]).to_a). to match_array([article, user]) end + + it "has a 'none' default scope" do + article = Article.create! :title => 'Pancakes' + index + + expect(ThinkingSphinx.none).to be_empty + end end diff --git a/spec/acceptance/searching_within_a_model_spec.rb b/spec/acceptance/searching_within_a_model_spec.rb index b05ce8ea2..45cb79b74 100644 --- a/spec/acceptance/searching_within_a_model_spec.rb +++ b/spec/acceptance/searching_within_a_model_spec.rb @@ -99,6 +99,13 @@ expect(Article.sphinx_search.first).to eq(article) end + + it "has a 'none' default scope" do + article = Article.create! :title => 'Pancakes' + index + + expect(Article.search_none).to be_empty + end end describe 'Searching within a model with a realtime index', :live => true do diff --git a/spec/acceptance/sphinx_scopes_spec.rb b/spec/acceptance/sphinx_scopes_spec.rb index aea3c90c1..c47d11ee0 100644 --- a/spec/acceptance/sphinx_scopes_spec.rb +++ b/spec/acceptance/sphinx_scopes_spec.rb @@ -77,4 +77,11 @@ ThinkingSphinx::PopulatedResultsError ) end + + it "handles a chainable 'none' scope and returns nothing" do + Book.create! :title => 'Small Gods' + index + + expect(Book.by_query('gods').none).to be_empty + end end