Blog Post - 29. März 2019
Sunspot Rails RSolr::Error::Http (RSolr::Error::Http - 400 Bad Request

Sunspot Rails RSolr::Error::Http (RSolr::Error::Http - 400 Bad Request

Currently I'm working on a Rails 5.2 project with a SOLR based search. Since I've been using elasticsearch a lot, It was my first SOLR based search since ages. On the development side I'm using the SOLR server shipped with sunspot_solr. On production I'm using a standalone SOLR 7.7 installation. After struggeling with the fact, that I had to copy the schema.xml from sunspot to the production SOLR installation and working out how the cores fix (which is very easy, once you get it) my search worked on production but there was a problem while updating the index and during a full reindex.

I got a RSolr::Error::Http (RSolr::Error::Http - 400 Bad Request

with the following error_code
Error: {
  "responseHeader":{
    "status":400,
    "QTime":3},
  "error":{
    "metadata":[
      "error-class","org.apache.solr.common.SolrException",
      "root-error-class","java.lang.NumberFormatException"],
    "msg":"ERROR: [doc=Sample 1] Error adding field 'created_at_d'='2019-03-29T14:08:03Z' msg=For input string: \"2019-03-29T14:08:03Z\"",
    "code":400}}

I configured solr for my model like this

searchable do
  text :title, :additional_info
  string :visible_to
  time :created_at, :updated_at
end

On the development side everything went just fine so I made some research. After reading through the documentation of the SOLR version I'm using (7.7 which is more recent than the one shipped with sunspot_solr) I finally found the error. My app tries to send created_at to solr as created_at_d but it must be a TrieField (created_at_dt, more information on TrieFields can be found here)

Luckily this can be configured easily for sunspot, now my model looks like this

  searchable do
    text :title, :additional_info
    string :visible_to
    time :created_at, :updated_at, trie: true
  end

And everything works!