思考Sphinx - 如何索引相关模型中可能为零的值?

时间:2011-10-03 22:13:55

标签: thinking-sphinx

我正在尝试在我的类别模型上定义以下索引:

define_index do
  has document.author.name :as => :author_name, :facet => true
end

我的模型定义是:

class Category < ActiveRecord::Base
  has_many: documents
end

class Author ActiveRecord::Base
  has_many :documents
end

class Document ActiveRecord::Base
  belongs_to :category
  belongs_to :author
end

某个类别可能有也可能没有与之关联的文档 - 取决于类别,许多类别可以在没有任何文档的情况下存在。

问题是当我尝试运行索引器时:

Cannot automatically map column type NilClass to an equivalent Sphinx
type (integer, float, boolean, datetime, string as ordinal). You could try to
explicitly convert the column's value in your define_index block:
  has "CAST(column AS INT)", :type => :integer, :as => :column

有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:0)

define_index do
  # firstly, you must have at least one indexed column
  indexes document.author.name :as => :author_name, :facet => true

  # to add 'has' for string you crc32
  # has "CRC32(string_col)", :as => :filtered_string_col
end

如果您需要搜索'has'col:

:conditions => { "string to filter on".to_crc32 }