Skip to content

Add support for custom search_document_id with record loading#1734

Open
khasinski wants to merge 1 commit intoankane:masterfrom
khasinski:fix-custom-search-document-id-loading
Open

Add support for custom search_document_id with record loading#1734
khasinski wants to merge 1 commit intoankane:masterfrom
khasinski:fix-custom-search-document-id-loading

Conversation

@khasinski
Copy link
Contributor

When a model defines a custom search_document_id, searching with load: true would fail to find records because Searchkick used the custom ID to query the database primary key.

This adds support for models to define find_by_search_document_ids class method to handle the mapping from custom search document IDs back to database records.

Example:

  class Product < ApplicationRecord
    searchkick

    def search_document_id
      "tenant_#{tenant_id}_#{id}"
    end

    def self.find_by_search_document_ids(search_ids)
      ids = search_ids.map { |sid| sid.split("_").last }
      where(id: ids)
    end
  end

Fixes #1732

When a model defines a custom search_document_id, searching with load: true
would fail to find records because Searchkick used the custom ID to query
the database primary key.

This adds support for models to define find_by_search_document_ids class
method to handle the mapping from custom search document IDs back to
database records.

Example:
  class Product < ApplicationRecord
    searchkick

    def search_document_id
      "tenant_#{tenant_id}_#{id}"
    end

    def self.find_by_search_document_ids(search_ids)
      ids = search_ids.map { |sid| sid.split("_").last }
      where(id: ids)
    end
  end

Fixes ankane#1732
@khasinski khasinski force-pushed the fix-custom-search-document-id-loading branch from 93e280b to 79f97e2 Compare January 14, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When adding a search_document_id, it is impossible to find a record in the database

1 participant