ankane/searchkick

Unable to search with multiple databases

Closed this issue · 2 comments

First
Yes, I'm on the latest version

Describe the bug
Search Index is showing the right count of items ingested on Model.reindex in logs but cannot find the items when searching..

To reproduce
Use this code to reproduce when possible:

test_searchkick.rb on a brand new Rails 8 project

require "bundler/inline"
require "amazing_print"
require "bundler/inline"
require "./config/environment.rb"

gemfile do
  source "https://rubygems.org"

  gem "activerecord", "8.0.0.rc1", require: "active_record"
  # gem "activejob", require: "active_job"
  gem "pg"
  gem "searchkick", "5.4.0"
  # uncomment one
  # gem "elasticsearch"
  gem "opensearch-ruby"
end

ENV["OPENSEARCH_HOST"] = "192.168.5.38"

Searchkick.client = OpenSearch::Client.new(
  host: "https://#{ENV['OPENSEARCH_HOST']}:9200",
  user: "admin",
  password: "StrongPass123$",
  transport_options: { ssl: { verify: false } }  # For testing only. Use certificate for validation.
)



puts "Searchkick version: #{Searchkick::VERSION}"
puts "Server version: #{Searchkick.server_version}"

ActiveRecord::Base.establish_connection adapter: "postgresql", database: "postgres"

class ApplicationSearchRecord < ActiveRecord::Base
  self.abstract_class = true
  connects_to database: { writing: :postgres, reading: :postgres }
end

class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class

  connects_to database: { writing: :users, reading: :users }
end

module Itemable
  extend ActiveSupport::Concern

  included do
    validates :barcode, presence: true, length: { maximum: 20 }
    validates :category, presence: true, length: { maximum: 120 }
    validates :title, length: { maximum: 366 }
    searchkick suggest: [ :title, :alt_title ], word_middle: [ :title, :alt_title, :description, :brand, :product_type ], highlight: [ :title, :description ]
  end

  def search_data
    {
      class_name: self.class.name,
      price_count: self.price_count
    }
  end
end

class ComAmazonItem < ApplicationSearchRecord
  include Itemable

  has_many :com_amazon_prices, primary_key: "barcode", foreign_key: "barcode", class_name: "ComAmazonPrice"

  def prices
    self.com_amazon_prices
  end
end

class ComAmazonPrice < ApplicationSearchRecord
  belongs_to :com_amazon_item, class_name: "ComAmazonItem", foreign_key: "barcode"

  def item
    self.com_amazon_item
  end
end

ComAmazonItem.search_index.delete
ComAmazonItem.reindex
ComAmazonItem.create!(barcode: rand(1000000000), category: "Electronics", title: "Razer Basilisk V3 Customizable Ergonomic Gaming Mouse: Fastest Gaming Mouse Switch - Chroma RGB Lighting - 26K DPI Optical Sensor -Classic Black : Video Games")
ComAmazonItem.reindex
p ComAmazonItem.search("razer").response
ap ComAmazonItem.search_index.mapping
ap ComAmazonItem.search("razer").results

ap ComAmazonItem.all.length

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: postgres_pass
  host: 192.168.5.38
  # host: alpha.host.com
  port: 5432
  database: postgres
  connect_timeout: 10 #sec
  checkout_timeout: 500
  variables:
    statement_timeout: 1800000 #ms

development:
  users:
    <<: *default
    host: host_ip
    migrations_paths: db/migrate/primary
  postgres:
    <<: *default
    host: 192.168.5.38
    migrations_paths: db/migrate/secondary

Additional context
It should be finding "razer" in the title of the inserted records but it fails to for some reason.. Same happens on latest OpenSearch and ElasticSearch docker images.

Hi @34code, it looks like title isn't included in search_data.

D'oh.. Thanks so much @ankane :) forgot i was overriding the search_data.