Skip to content

Commit

Permalink
scope based upsert or delete
Browse files Browse the repository at this point in the history
  • Loading branch information
akostadinov committed Feb 28, 2024
1 parent 5caa63f commit 1b0ebdb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
20 changes: 18 additions & 2 deletions lib/thinking_sphinx/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def delete

indices.each { |index|
ThinkingSphinx::Deletion.perform(
index, id || instance.public_send(index.primary_key)
index, id
)
}
end
Expand All @@ -25,9 +25,21 @@ def upsert
end
end

def stage
real_time_indices.each do |index|
found = index.scope.find_by(model.primary_key => id)

if found
ThinkingSphinx::RealTime::Transcriber.new(index).copy found
else
ThinkingSphinx::Deletion.perform(index, id)
end
end
end

private

attr_reader :instance, :model, :id
attr_reader :instance, :model

def indices
ThinkingSphinx::Configuration.instance.index_set_class.new(
Expand All @@ -39,6 +51,10 @@ def loaded_instance
@loaded_instance ||= instance || model.find(id)
end

def id
@id ||= instance.public_send(instance.class.primary_key)
end

def real_time_indices
indices.select { |index| index.is_a? ThinkingSphinx::RealTime::Index }
end
Expand Down
42 changes: 41 additions & 1 deletion spec/acceptance/real_time_updates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
expect(Admin::Person.search('Mort').to_a).to eq([person])
end

it "can use a direct interface for processing records" do
it "can use direct interface for upserting records" do
Admin::Person.connection.execute <<~SQL
INSERT INTO admin_people (name, created_at, updated_at)
VALUES ('Pat', now(), now());
Expand All @@ -52,4 +52,44 @@

expect(Admin::Person.search('Patrick').to_a).to eq([instance])
end

it "can use direct interface for processing records outside scope" do
Article.connection.execute <<~SQL
INSERT INTO articles (title, published, created_at, updated_at)
VALUES ('Nice Title', TRUE, now(), now());
SQL

article = Article.last

ThinkingSphinx::Processor.new(model: article.class, id: article.id).stage

expect(ThinkingSphinx.search('Nice', :indices => ["published_articles_core"])).to include(article)

Article.connection.execute <<~SQL
UPDATE articles SET published = FALSE WHERE title = 'Nice Title';
SQL
ThinkingSphinx::Processor.new(model: article.class, id: article.id).stage

expect(ThinkingSphinx.search('Nice', :indices => ["published_articles_core"])).to be_empty
end

it "can use direct interface for processing deleted records" do
Article.connection.execute <<~SQL
INSERT INTO articles (title, published, created_at, updated_at)
VALUES ('Nice Title', TRUE, now(), now());
SQL

article = Article.last
ThinkingSphinx::Processor.new(:instance => article).stage

expect(ThinkingSphinx.search('Nice', :indices => ["published_articles_core"])).to include(article)

Article.connection.execute <<~SQL
DELETE FROM articles where title = 'Nice Title';
SQL

ThinkingSphinx::Processor.new(:instance => article).stage

expect(ThinkingSphinx.search('Nice', :indices => ["published_articles_core"])).to be_empty
end
end
6 changes: 6 additions & 0 deletions spec/internal/app/indices/article_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@

set_property :morphology => 'stem_en'
end

ThinkingSphinx::Index.define :article, :name => :published_articles, :with => :real_time do
indexes title, content

scope { Article.where :published => true }
end

0 comments on commit 1b0ebdb

Please sign in to comment.