简体   繁体   English

如何在无循环依赖的情况下使用Think Sphinx在Rails 5中索引多态(STI)模型?

[英]How do I index polymorphic (STI) models in Rails 5 with Thinking Sphinx without a circular dependency?

I'm integrating a simple indexed search for a rails 5.1.4 application, using Sphinx 2.2.11-id64-release (95ae9a6) and thinking_sphinx v 4.0.0 我正在使用Sphinx 2.2.11-id64-release (95ae9a6)Sphinx 2.2.11-id64-release (95ae9a6) thinking_sphinx v 4.0.0集成对rails 5.1.4应用程序的简单索引搜索

Expected behavior: 预期行为:

When I submit a new search, I expect to see either an empty array [] or a set of search results. 提交新搜索时,我希望看到一个空数组[]或一组搜索结果。

Actual behavior: 实际行为:

When I submit a new search with empty parameters from the view layer and try to access the ThinkingSphinx::Search object via a binding.pry in the controller action, rails throws a ActionView::Template::Error (Circular dependency detected while autoloading constant StudentLesson) 当我从视图层提交带有空参数的新搜索并尝试通过控制器动作中的binding.pry访问ThinkingSphinx :: Search对象时,rails会抛出ActionView::Template::Error (Circular dependency detected while autoloading constant StudentLesson)

[1] pry(main)> ThinkingSphinx.search ''
=> [#<ThinkingSphinx::Search:0x2b0925399e10>
[2] pry(main)> _.inspect
RuntimeError: Circular dependency detected while autoloading constant StudentLesson
from /home/kf/.rvm/gems/ruby-2.4.3@crm/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:509:in `load_missing_constant'
[3] pry(main)>

Code snippets: 代码段:

class Lesson < ApplicationRecord
  LESSON_TYPES = {
    'StudentLesson': StudentLesson,
    'ProfessionalLesson': ProfessionalLesson
  }.freeze
end

class StudentLesson < Lesson
  after_save ThinkingSphinx::RealTime.callback_for(:student_lesson)
end

class ProfessionalLesson < Lesson
  after_save ThinkingSphinx::RealTime.callback_for(:professional_lesson)
end
# app/indices/student_lesson_index.rb
ThinkingSphinx::Index.define :student_lesson, with: :real_time do
  indexes name, sortable: true
end

# app/indices/professional_lesson_index.rb
ThinkingSphinx::Index.define :professional_lesson, with: :real_time do
  indexes name, sortable: true
end
class SearchesController < ApplicationController
  def index
    @results = []
  end

  def create
    @results = ThinkingSphinx.search(params[:search])
    render :index
  end
end
<div class="collapse navbar-collapse" id="header-navbar">
   <%= render 'layouts/nav_links' %>
   <%= form_for searches_path do %>
     <%= search_field_tag :search, params[:search] %>
     <%= submit_tag 'Search', name: nil, method: :get %>
   <% end %>
 </div>

Here's the dev.sphinx.conf 这是dev.sphinx.conf

indexer
{
}

searchd
{
  listen = 127.0.0.1:9306:mysql41
  log = /home/myapp/log/development.searchd.log
  query_log = /home/myapp/log/development.searchd.query.log
  pid_file = /home/myapp/log/development.sphinx.pid
  workers = threads
  binlog_path = /home/myapp/tmp/binlog/development
}

index game_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/game_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = summary
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index lesson_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/lesson_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = purpose
  rt_field = meta
  rt_field = supplies
  rt_field = activity
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index protocol_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/protocol_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = description
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index resource_page_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/resource_page_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = header
  rt_field = content
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = header_sort
}

index game
{
  type = distributed
  local = game_core
}

index lesson
{
  type = distributed
  local = lesson_core
}

index protocol
{
  type = distributed
  local = protocol_core
}

index resource_page
{
  type = distributed
  local = resource_page_core
}

I think the issue here is not directly related to Thinking Sphinx - it just errors because it can't load the search results due to the circular dependency in your models - particularly, the LESSON_TYPES constant: 我认为这里的问题与Think Sphinx没有直接关系-只是错误,因为由于模型中的循环依赖关系,它无法加载搜索结果-特别是LESSON_TYPES常量:

  • Thinking Sphinx makes a search call, and in its result set it has at least one StudentLesson instance, so it needs to load that model. Thinking Sphinx进行了搜索调用,并且在其结果集中它至少具有一个StudentLesson实例,因此需要加载该模型。
  • Loading StudentLesson finds its dependency (as a subclass) on Lesson . 加载StudentLesson发现其对Lesson依赖关系(作为子类)。
  • Loading Lesson finds its dependency (as references to constants) on both StudentLesson and ProfessionalLesson . 正在加载Lesson可以找到其对StudentLessonProfessionalLesson依赖关系(作为对常量的引用)。
  • So, StudentLesson is attempted to be loaded again, and hence the endless loop of dependencies. 因此,试图再次加载StudentLesson ,从而导致无限循环的依赖关系。

(FWIW I just confirmed this behaviour in a test Rails app using the model code you've provided, without TS being involved: all I needed to run in a console was StudentLesson.first .) (FWIW我只是使用您提供的模型代码在测试Rails应用程序中确认了此行为,而没有涉及TS:我需要在控制台中运行的只是StudentLesson.first 。)

You have 2 classes which both inherit a constant definition, this looks problematic. 您有2个类都继承一个常量定义,这看起来很麻烦。

Try moving this constant definition to an initializer: 尝试将此常量定义移至初始化程序:

LESSON_TYPES = {
    'StudentLesson': StudentLesson,
    'ProfessionalLesson': ProfessionalLesson
  }.freeze

解决方案实际上是在注释线程中找到的spring仍然未解决的问题,可通过初始化require_dependency 'lesson' ->来解决,该问题我实际上已经在初始化程序中有了,但将其移至已解决的Rails.application.config.to_prepare块中重新加载问题以及与狮身人面像相关的症状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM