Seeds into elastic search

This commit is contained in:
Brenda Wallace
2019-12-20 16:20:52 +13:00
parent d59f8f4419
commit 8789fee365
3 changed files with 70 additions and 13 deletions

View File

@@ -8,14 +8,34 @@ class SeedsController < ApplicationController
respond_to :rss, only: :index
def index
@owner = Member.find_by(slug: params[:member_slug]) if params[:member_slug].present?
@crop = Crop.find_by(slug: params[:crop_slug]) if params[:crop_slug].present?
@planting = Planting.find_by(slug: params[:planting_id]) if params[:planting_id].present?
where = {}
if params[:member_slug].present?
@owner = Member.find_by(slug: params[:member_slug])
where['owner_id'] = @owner.id
end
if params[:crop_slug].present?
@crop = Crop.find_by(slug: params[:crop_slug])
where['crop_id'] = @crop.id
end
if params[:planting_id].present?
@planting = Planting.find_by(slug: params[:planting_id])
where['parent_planting'] = @planting.id
end
@show_all = (params[:all] == '1')
where['finished'] = false unless @show_all
@filename = csv_filename
@seeds = seeds.order(created_at: :desc).includes(:owner, :crop).paginate(page: params[:page])
@seeds = Seed.search(
where: where,
page: params[:page],
limit: 30,
boost_by: [:created_at],
load: false
)
respond_with(@seeds)
end
@@ -66,15 +86,6 @@ class SeedsController < ApplicationController
@seed = Seed.find(params[:slug])
end
def seeds
records = Seed.all
records = records.where(owner: @owner) if @owner.present?
records = records.where(crop: @crop) if @crop.present?
records = records.where(parent_planting: @planting) if @planting.present?
records = records.active unless @show_all
records
end
def seed_params
params.require(:seed).permit(
:crop_id, :description, :quantity, :plant_before,

View File

@@ -0,0 +1,45 @@
module SeedSearch
extend ActiveSupport::Concern
included do
searchkick merge_mappings: true,
mappings: {
properties: {
created_at: { type: :integer },
photos_count: { type: :integer },
tradable_to: { type: :text }
}
}
scope :search_import, -> { includes(:owner, :crop, :parent_planting) }
def search_data
{
slug: slug,
crop_slug: crop.slug,
crop_name: crop.name,
crop_id: crop_id,
owner_id: owner_id,
owner_name: owner.login_name,
tradable_to: tradable_to,
tradeable: tradable?,
parent_planting: parent_planting,
photos_count: photos.size,
has_photos: photos.size.positive?,
thumbnail_url: default_photo&.thumbnail_url,
finished: finished?,
created_at: created_at.to_i
}
end
def self.homepage_records(limit)
search('*',
limit: limit,
where: {
photos_count: { gt: 0 }
},
boost_by: [:created_at],
load: false)
end
end
end

View File

@@ -3,6 +3,7 @@ class Seed < ApplicationRecord
include PhotoCapable
include Finishable
include Ownable
include SeedSearch
friendly_id :seed_slug, use: %i(slugged finders)
TRADABLE_TO_VALUES = %w(nowhere locally nationally internationally).freeze