我有状态(整数)和date_of_dervice_provided(日期)字段的记录模型。
我想根据以下两个字段执行搜索。
1)枚举状态:[:created,:valued,:signed,:approved] 2)date_of_dervice_provided
我可以使用ActiveRecord rails进行搜索,如下所示。
@records = Record.all
@records = @records.where(state: params[:state]) if params[:state]
@records = @records.where('extract(year from date_of_service_provided) = ?', params[:year]) if params[:year]
我想使用弹性搜索
进行搜索为了实现弹性搜索,我已经添加了以下内容
的Gemfile
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
Record.rb 模型文件
require 'elasticsearch/model'
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
settings do
mappings dynamic: false do
indexes :state, type: 'integer'
indexes :date_of_service_provided, type: 'date'
end
end
我对弹性搜索不太熟悉。我可以使用弹性搜索进行搜索,如下所示
@elastic_records = Record.all
@elastic_records = @elastic_records.search(params[:state])