pat / thinking-sphinx

Sphinx/Manticore plugin for ActiveRecord/Rails
http://freelancing-gods.com/thinking-sphinx
MIT License
1.63k stars 468 forks source link

Use instances for index filtering from callbacks. #1171

Closed pat closed 4 years ago

pat commented 4 years ago

Prompted by discussions in #1166, this PR changes the IndexSet class to accept :instances alongside the :classes option, which could allow custom subclasses of IndexSet to filter returned indices to a subset as determined by the provided instances.

In particular, if you're sharding your indices, you can return just the specific shard of indices for the instance, rather than all shards:

class IndexSet < ThinkingSphinx::IndexSet
  private

  def indices
    return super if instances.empty?

    super.select do |index|
      # shard_id or equivalent logic needs to be implemented.
      # this would return all core and delta indices for the instance's model,
      # filtered by the shard id for the instance.
      instances.any? { |instance| index.name[/_#{instance.shard_id}_$/] }
    end
  end
end

# This line would go in an initialiser:
ThinkingSphinx::Configuration.instance.index_set_class = IndexSet

While it's not necessary moving forward, I'm still passing through the :classes option for anyone who's got a custom IndexSet class that depends on that.