【问题标题】:Sunspot/Solr: indexing a time field results in NumberFormatExceptionSunspot/Solr:索引时间字段会导致 NumberFormatException
【发布时间】:2012-06-10 15:18:21
【问题描述】:

我正在尝试将时间戳字段添加到我的模型中,以便通过 Sunspot/Solr 进行索引。 Solr 对此感到窒息并产生NumberFormatException

class Book < ActiveRecord::Base
  attr_accessible :lastUpdated, :category, :title  # etc...

  searchable do
    text :title
    text :category
    time :lastUpdated   # mysql 'datetime' field
    # etc...
  end
end

Jun 06, 2012 10:59:10 AM org.apache.solr.common.SolrException log
SEVERE: java.lang.NumberFormatException: For input string: "2012-01-02T03:29:00Z"

我也尝试过使用date :lastUpdated,结果相同。

考虑到我的模型可能有一些虚假的lastUpdated 值,我尝试从Time.now 索引结果,并得到了相同的结果。

我在外部使用 Solr 3.4.0,但使用sunspot-installer 提供的“内部”Solr 重现了同样的问题,并相应地调整了sunspot.yml。我的情况看起来很像here 提到的问题,但是重新安装 Sunspot/Solr 配置似乎并不能解决它。

编辑:也尝试过针对 Solr 3.6.0;同样的结果。

【问题讨论】:

    标签: ruby solr lucene sunspot sunspot-solr


    【解决方案1】:

    我怀疑这是由于 Sunspot 的 type.rb 中的错误造成的。 TimeType 将其 indexed_name 定义为“_d”,而不是“_dt”。我在我的模型代码中使用以下方法解决了这个问题:

    module Sunspot
      module Type
        class TimeType < AbstractType
          def indexed_name(name) #:nodoc:
            "#{name}_dt"
          end
        end
        register TimeType
      end
    end
    

    【讨论】:

    • 3 年后,这个答案仍然相关并且错误仍然存​​在。为此,我在 Github 上打开了一个问题。
    • 修复也对我有用,差不多 5 年后!将此覆盖 this 放在我的模型顶部,在类定义之前。使用 Solr 6.0.1 / 太阳黑子 2.2.7
    • 又过了一年,我们仍然面临同样的问题,您的修复仍然有效,但为什么此代码在 config/initializer/sunspot/type.rb 文件中不起作用。
    • 截至 2019 年 6 月,仍然非常重要。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多