-
Notifications
You must be signed in to change notification settings - Fork 757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Searching with multiple STI models broken since 4.0 #1259
Comments
Hey @cars10, all of the inheritance tests are passing. Please use the gist in the Contributing Guide to reproduce. |
Sure. require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord"
gem "sqlite3"
gem "searchkick", git: "https://github.com/ankane/searchkick.git"
end
require "active_record"
puts "Searchkick version: #{Searchkick::VERSION}"
puts "Elasticsearch version: #{Searchkick.server_version}"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :animals do |t|
t.string :name
t.string :type
end
class Animal < ActiveRecord::Base
searchkick inheritance: true
end
class Dog < Animal; end
class Cat < Animal; end
class Fish < Animal; end
Animal.reindex
Dog.create!(name: "Dog1")
Cat.create!(name: "Cat1")
Animal.search_index.refresh
search = Searchkick.search("*", fields: [:name], models: [Dog, Cat], execute: false)
response = search.execute
# this only returns 1 mapping, but we are searching for 2 models. See description in my issue
pp response.options[:index_mapping]
# this only returns 1 cat, not 1 cat and 1 dog.
pp response.results Edit: require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord"
gem "sqlite3"
gem "searchkick", git: "https://github.com/ankane/searchkick.git", tag: "v3.1.3"
end
require "active_record"
puts "Searchkick version: #{Searchkick::VERSION}"
puts "Elasticsearch version: #{Searchkick.server_version}"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :animals do |t|
t.string :name
t.string :type
end
class Animal < ActiveRecord::Base
searchkick inheritance: true
end
class Dog < Animal; end
class Cat < Animal; end
class Fish < Animal; end
Animal.reindex
Dog.create!(name: "Dog1")
Cat.create!(name: "Cat1")
Animal.search_index.refresh
search = Searchkick.search("*", fields: [:name], index_name: [Dog, Cat], execute: false)
response = search.execute
# this returns 1 cat and 1 dog - on searchkick 3.1.3
pp response.results |
Thanks @cars10, will check it out when I have a chance |
Hey @cars10, after an initial look, the examples above aren't the documented way to search inherited models. In your 2nd example (with Searchkick 3.1.3), add a fish record and you'll see it returns 3 results. You want to use the Animal.search("*", type: [Cat, Dog]) There may be some magic we can do to make the |
Alright, the Thanks for the detailed report! |
Thanks for the fix. What would be the recommended way to search for multiple STI models and a different model together? for example Searchkick.search("*", models: [Dog, Cat, Person]) I think i cannot use the |
I think this situation is pretty rare (searching two child models + a different one), but depending on your case, you could use multi-search instead (if results don't need to be scored together) or add Searchkick to each of the child models instead of the parent. Ideally, we could replace the
However, Hopefully support for aliases will be added to the |
Hi,
consider having 3 STI models
And searching like this:
This will not return any objects of type
Dog
. The problem seems to be that you changed how theindex
/@index_mapping
(new name) variable is calculated, see 7ce0440#diff-9e535cec87dc0b7137c4f81bdabdafd7R60On searchkick 3.x it was an array so you could have duplicates of the same index in there. In our example all three sti classes use the same index (
animals
). With the change in searchkick 4 you changed that to be a hash, but because each key has to be unique we cannot have the same behaviour as before. We will end up with onlyCat
getting searched because it overrides the previously set key forDog
in@index_mapping
.Edit:
The hash will look something like this in the first step:
And in the next iteration:
What we actually want is that both models are searched.
The text was updated successfully, but these errors were encountered: