I'm migrating my app from solr to Elasticsearch for document searching. The recommended pattern for deploying Elasticsearch is to include the Elasticsearch functionality (ie Elasticsearch::Model
and Elasticsearch::Model::Indexing
) in a Rails concern and then include this module in your model. I've implemented this as such. (Some methods temporarily commented out as I'm working through this.)
#app/models/concerns/elasticsearch_user.rb
require 'active_support/concern'
module ElasticsearchUser
extend ActiveSupport::Concern
included do
require 'elasticsearch/model'
include Elasticsearch::Model
include Elasticsearch::Model::Indexing
#mapping do
# #to-do
#end
#def self.search(query)
# _elasticsearch_.search(
# {
# query: {
# multi_match: {
# query: query,
# fields: ['username^2', 'email', 'full_name']
# }
# }
# }
# )
#end
end
end
#User.rb
...
# Elasticsearch configuration
include ElasticsearchUser
# index_name "users" <-- This value is inferred automatically by the model name
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :username
indexes :email
indexes :full_name
end
end
...
def as_indexed_json(options={})
self.as_json(only: [:username, :email, :full_name],
methods: [:full_name]
)
end
I can call Model.import
and Model.search
with no problems. Calling things like Model._elasticsearch_.create_index!
(with and without the _elasticsearch_
namespace) throw a NoMethodError. I've tried including Elasticsearch::Model
directly in my model definition with no success. I've also tried requiring `active_support/concern' at the top of my model - no luck there either. It seems obvious that the module/concern is not being included in my model but I can't figure out why.
Aucun commentaire:
Enregistrer un commentaire