Elasticsearch and Rails: analyzer [searchkick_search] not found

Posted: March 13, 2015. Tags: Elasticsearch, Rails, Ruby

Elasticsearch will create indices on the fly for you. This can be a problem. e.g. indices created during test will not behave the same way as indices created in development.

If you are using Elasticsearch with Rails using the searchkick gem, then rake searchkick:reindex or Model.reindex will create an index with extra analyzers.

If an index is created on the fly, this does not happen:

console> DeliveredEmail.searchkick_index.delete
=> {"acknowledged"=>true}

# Cause an index to be created automatically by indexing one row
console> DeliveredEmail.first.reindex
=> {"_index"=>"delivered_emails_development", "_type"=>"delivered_email", "_id"=>"2120427", "_version"=>1, "created"=>true}

# Note "analyzer ... not found" message in error
console> DeliveredEmail.search("foo", fields:[:body])
Searchkick::InvalidQueryError: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {…, nested: QueryParsingException[[delivered_emails_development] [match] analyzer [searchkick_search] not found]; }]","status":400}
=> {"acknowledged"=>true}

# Drop and recreate index the right way:
console> DeliveredEmail.reindex
=> true

# And now we get results
console> DeliveredEmail.search("foo", fields:[:body])
=> #<Searchkick::Results:0x007fd02f8a18b0 @klass=DeliveredEmail(id: integer, message_id: string, body: text, headers: text, created_at: datetime, updated_at: datetime), @response={"took"=>6, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>1, "max_score"=>0.30685282, "hits"=>[{"_index"=>"delivered_emails_development_20150313142335356", "_type"=>"delivered_email", "_id"=>"2120429", "_score"=>0.30685282}]}}, @options={:page=>1, :per_page=>100000, :padding=>0, :load=>true, :includes=>nil, :json=>false}>