我最近将我的应用程序升级到Rails 5,现在我想索引时出现错误(请参阅下面的stacktrace)。
只有当我在索引中对这两行进行注释时,TS才能创建索引:
indexes categories(:slug), as: :category_slugs, facet: true
has categories(:id), as: :category_ids, facet: true
我在join_association.rb
(on github)文件中进行了一些调试,发现存在Category
个对象数组而不是Relation
个对象。
摘录:
def join_constraints(foreign_table, foreign_klass, join_type, tables, chain)
joins = []
binds = []
tables = tables.reverse
# The chain starts with the target table, but we want to end with it here (makes
# more sense in this context), so we reverse
chain.reverse_each do |reflection|
table = tables.shift
klass = reflection.klass
join_keys = reflection.join_keys
key = join_keys.key
foreign_key = join_keys.foreign_key
constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
scope_chain_items = reflection.join_scopes(table, predicate_builder)
klass_scope = reflection.klass_join_scope(table, predicate_builder)
scope_chain_items.concat [klass_scope].compact
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
left.merge right
############################ CRASH ############################
end
if rel && !rel.arel.constraints.empty?
binds += rel.bound_attributes
constraint = constraint.and rel.arel.constraints
end
if reflection.type
value = foreign_klass.base_class.name
column = klass.columns_hash[reflection.type.to_s]
binds << Relation::QueryAttribute.new(column.name, value, klass.type_for_attribute(column.name))
constraint = constraint.and klass.arel_attribute(reflection.type, table).eq(Arel::Nodes::BindParam.new)
end
joins << table.create_join(table, table.create_on(constraint), join_type)
# The current table in this iteration becomes the foreign table in the next
foreign_table, foreign_klass = table, klass
end
JoinInformation.new joins, binds
end
# app/indices/programme_index.rb
ThinkingSphinx::Index.define :programme, with: :active_record, delta: true do
has id, as: :programme_id
# Sphinx fields
indexes website
indexes admin_contact
indexes categories(:slug), as: :category_slugs, facet: true
has categories(:id), as: :category_ids, facet: true
end
我正在使用Rails 5.1.3和Ruby 2.2.7
# config/thinking_sphinx.yml
development:
enable_star: true
match_mode: boolean
如果我注释掉这两行,我会得到以下索引:
indexer
{
}
searchd
{
listen = 127.0.0.1:9306:mysql41
log = /myproject/log/development.searchd.log
query_log = /myproject/log/development.searchd.query.log
pid_file = /myproject/log/development.sphinx.pid
workers = threads
binlog_path = /myproject/tmp/binlog/development
}
source programme_core_0
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = fundingunisgch001
sql_query_pre = SET TIME_ZONE = '+0:00'
sql_query_pre = UPDATE `programmes` SET `delta` = 0 WHERE `delta` = 1
sql_query_pre = SET NAMES utf8
sql_query = SELECT SQL_NO_CACHE `programmes`.`id` * 3 + 0 AS `id`, `programmes`.`website` AS `website`, `programmes`.`admin_contact` AS `admin_contact`, `programmes`.`id` AS `sphinx_internal_id`, 'Programme' AS `sphinx_internal_class`, 0 AS `sphinx_deleted`, `programmes`.`id` AS `programme_id` FROM `programmes` WHERE (`programmes`.`id` BETWEEN $start AND $end) GROUP BY `programmes`.`id`, `programmes`.`website`, `programmes`.`admin_contact`, `programmes`.`id`, `programmes`.`id` ORDER BY NULL
sql_query_range = SELECT IFNULL(MIN(`programmes`.`id`), 1), IFNULL(MAX(`programmes`.`id`), 1) FROM `programmes`
sql_attr_uint = sphinx_internal_id
sql_attr_uint = sphinx_deleted
sql_attr_uint = programme_id
sql_attr_string = sphinx_internal_class
}
index programme_core
{
type = plain
path = /myproject/db/sphinx/development/programme_core
docinfo = extern
enable_star = 1
source = programme_core_0
}
source programme_delta_0
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = fundingunisgch001
sql_query_pre = SET TIME_ZONE = '+0:00'
sql_query_pre = SET NAMES utf8
sql_query = SELECT SQL_NO_CACHE `programmes`.`id` * 3 + 0 AS `id`, `programmes`.`website` AS `website`, `programmes`.`admin_contact` AS `admin_contact`, `programmes`.`id` AS `sphinx_internal_id`, 'Programme' AS `sphinx_internal_class`, 0 AS `sphinx_deleted`, `programmes`.`id` AS `programme_id` FROM `programmes` WHERE (`programmes`.`delta` = 1 AND `programmes`.`id` BETWEEN $start AND $end) GROUP BY `programmes`.`id`, `programmes`.`website`, `programmes`.`admin_contact`, `programmes`.`id`, `programmes`.`id` ORDER BY NULL
sql_query_range = SELECT IFNULL(MIN(`programmes`.`id`), 1), IFNULL(MAX(`programmes`.`id`), 1) FROM `programmes` WHERE (`programmes`.`delta` = 1)
sql_attr_uint = sphinx_internal_id
sql_attr_uint = sphinx_deleted
sql_attr_uint = programme_id
sql_attr_string = sphinx_internal_class
}
index programme_delta
{
type = plain
path = /myproject/db/sphinx/development/programme_delta
docinfo = extern
enable_star = 1
source = programme_delta_0
}
index programme
{
type = distributed
local = programme_core
local = programme_delta
}
$ bin/rails ts:index --trace
** Invoke ts:index (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute ts:index
Generating configuration to /myproject/config/development.sphinx.conf
rails aborted!
NoMethodError: undefined method `merge' for #<Array:0x007fc53e6042e8>
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:50:in `block (2 levels) in join_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:49:in `each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:49:in `inject'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:49:in `block in join_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:33:in `reverse_each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency/join_association.rb:33:in `join_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:174:in `make_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:180:in `make_outer_joins'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:227:in `block in walk'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:227:in `each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:227:in `flat_map'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/associations/join_dependency.rb:227:in `walk'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/polyamorous-1.3.1/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb:65:in `block in join_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/polyamorous-1.3.1/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb:63:in `each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/polyamorous-1.3.1/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb:63:in `flat_map'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/polyamorous-1.3.1/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb:63:in `join_constraints'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation/query_methods.rb:1022:in `build_join_query'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation/query_methods.rb:1003:in `build_joins'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation/query_methods.rb:942:in `build_arel'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation/query_methods.rb:918:in `arel'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation.rb:579:in `block in to_sql'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:222:in `unprepared_statement'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activerecord-5.1.3/lib/active_record/relation.rb:578:in `to_sql'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/active_record/sql_builder.rb:11:in `sql_query'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/active_record/sql_source.rb:126:in `build_sql_query'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/active_record/sql_source.rb:141:in `prepare_for_render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/active_record/sql_source.rb:69:in `render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration/index.rb:29:in `block in render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration/index.rb:29:in `collect'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration/index.rb:29:in `render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/core/index.rb:57:in `render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration.rb:43:in `block in render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration.rb:43:in `collect'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/riddle-2.2.0/lib/riddle/configuration.rb:43:in `render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/configuration.rb:102:in `render'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/configuration.rb:108:in `block in render_to_file'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/2.2.0/open-uri.rb:36:in `open'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/2.2.0/open-uri.rb:36:in `open'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/configuration.rb:108:in `render_to_file'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/rake_interface.rb:30:in `configure'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/rake_interface.rb:41:in `index'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/thinking-sphinx-3.3.0/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `call'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/railties-5.1.3/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/railties-5.1.3/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/railties-5.1.3/lib/rails/command.rb:46:in `invoke'
/usr/local/var/rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/railties-5.1.3/lib/rails/commands.rb:16:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => ts:index
thinking-sphinx (3.3.0)
activerecord (>= 3.1.0)
builder (>= 2.1.2)
innertube (>= 1.0.2)
joiner (>= 0.2.0)
middleware (>= 0.1.0)
riddle (>= 2.0.0)
更新:我的数据结构有多种关系,但只有在处理join_type
ThroughReflection + HasManyReflection
时才会发生崩溃。
#<Arel::Table:0x007fc0f9e14bc0>
Programme
Arel::Nodes::OuterJoin
#<Arel::Table:0x007fc100020698>
#<ActiveRecord::Reflection::HasManyReflection:0x007fc0fe3fcca0>
#<Arel::Table:0x007fc0f9e14bc0>
Programme
Arel::Nodes::OuterJoin
#<Arel::Table:0x007fc100001338>
#<ActiveRecord::Reflection::BelongsToReflection:0x007fc0fdd4d698>
#<Arel::Table:0x007fc0f9e14bc0>
Programme
Arel::Nodes::OuterJoin
#<Arel::Table:0x007fc0fe672598>
#<Arel::Table:0x007fc0fe672278>
#<ActiveRecord::Reflection::ThroughReflection:0x007fc0f93245a8>
#<ActiveRecord::Reflection::HasManyReflection:0x007fc0fddb6e18>
############################ CRASH ############################
rails aborted!
class Programme < ActiveRecord::Base
include ThinkingSphinx::Scopes
has_and_belongs_to_many :categories, -> { uniq }
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :programmes, -> { uniq }
end
我确实将我的本地sphinx二进制文件从2.2.10
更新为2.2.11
$ brew info sphinx
sphinx: stable 2.2.11 (bottled), HEAD
答案 0 :(得分:0)
事实证明,, -> { uniq }
部分导致了问题。
当我用, -> { distinct }
替换它时,索引器最终将会运行。