Pull crops and harvests from elasticsearch

This commit is contained in:
Brenda Wallace
2019-12-09 22:19:20 +13:00
parent bfe1b072db
commit 282791d91b
9 changed files with 63 additions and 30 deletions

View File

@@ -9,7 +9,7 @@ class CropsController < ApplicationController
def index
@sort = params[:sort]
@crops = crops
@crops = Crop.search('*', boost_by: [:plantings_count, :harvests_count], limit: 100, page: params[:page], load: false)
@num_requested_crops = requested_crops.size if current_member
@filename = filename
respond_with @crops
@@ -213,13 +213,6 @@ class CropsController < ApplicationController
}
end
def crops
q = Crop.approved.includes(:scientific_names, plantings: :photos)
q = q.popular unless @sort == 'alpha'
q.order(Arel.sql("LOWER(crops.name)"))
.includes(:photos).paginate(page: params[:page])
end
def requested_crops
current_member.requested_crops.pending_approval
end

View File

@@ -11,10 +11,12 @@ class HarvestsController < ApplicationController
@crop = Crop.find_by(slug: params[:crop_slug])
@planting = Planting.find_by(slug: params[:planting_id])
@harvests = @harvests.where(owner: @owner) if @owner.present?
@harvests = @harvests.where(crop: @crop) if @crop.present?
@harvests = @harvests.where(planting: @planting) if @planting.present?
@harvests = @harvests.order(harvested_at: :desc).joins(:owner, :crop).paginate(page: params[:page])
where = {}
where['owner_id']= @owner.id if @owner.present?
where['crop_id']= @crop.id if @crop.present?
where['planting_id']= @planting.id if @planting.present?
@harvests = Harvest.search('*', where: where, limit: 100, page: params[:page], load: false, boost_by: [:created_at])
@filename = csv_filename

View File

@@ -2,11 +2,8 @@ module CropSearch
extend ActiveSupport::Concern
included do
####################################
# Elastic search configuration
searchkick word_start: %i(name alternate_names scientific_names), case_sensitive: false
# Special scope to control if it's in the search index
scope :search_import, -> { includes(:scientific_names, :photos) }
def should_index?
@@ -21,6 +18,7 @@ module CropSearch
scientific_names: scientific_names.pluck(:name),
# boost the crops that are planted the most
plantings_count: plantings_count,
harvests_count: harvests.size,
# boost this crop for these members
planters_ids: plantings.pluck(:owner_id),
has_photos: photos.size.positive?,

View File

@@ -0,0 +1,22 @@
module HarvestSearch
extend ActiveSupport::Concern
included do
searchkick
scope :search_import, -> { includes(:owner, :crop, :plant_part) }
def search_data
{
slug: slug,
crop_slug: crop.slug,
crop_name: crop.name,
crop_id: crop_id,
owner_id: owner_id,
planting_id: planting_id,
thumbnail_url: default_photo&.thumbnail_url,
created_at: created_at.to_i
}
end
end
end

View File

@@ -3,6 +3,7 @@ class Harvest < ApplicationRecord
extend FriendlyId
include PhotoCapable
include Ownable
include HarvestSearch
friendly_id :harvest_slug, use: %i(slugged finders)

View File

@@ -0,0 +1,14 @@
.card.crop-thumbnail
= link_to crop_path(slug: crop['slug']) do
= image_tag(crop['photo'] ? crop['photo'] : placeholder_image,
alt: crop['name'],
class: 'img img-card')
.text
%h3.crop-name
= link_to crop['name'], crop_path(slug: crop['slug'])
%h5.crop-sci-name
&nbsp;
= crop['scientific_name']

View File

@@ -23,7 +23,7 @@
= will_paginate @crops
.index-cards
- @crops.each do |crop|
= render 'crops/thumbnail', crop: crop
= render 'crops/search_result', crop: crop
= will_paginate @crops

View File

@@ -33,5 +33,20 @@
= page_entries_info @harvests
= will_paginate @harvests
.index-cards
= render @harvests, full: true
- @harvests.each do |harvest|
- cache harvest do
.card
= link_to harvest_path(id: harvest['id']) do
- if harvest['thumbnail_url'].present?
= image_tag harvest['thumbnail_url'], alt: harvest, class: 'img-card'
- else
= image_tag placeholder_image, alt: harvest, class: 'img-card'
.card-body
%h5
%strong
= link_to harvest_path(id: harvest['id']) do
= harvest['crop_name']
%span.badge.badge-pill= harvest['plant_part']
.card-footer
.float-right=render 'members/tiny', member: Member.find(harvest['owner_id'])
= will_paginate @harvests

View File

@@ -1,16 +1,4 @@
- cache cache_key_for(Crop, 'homepage'), expires_in: 1.day do
.index-cards
- CropSearchService.random_with_photos(16).each do |crop|
.card.crop-thumbnail
= link_to crop_path(slug: crop['slug']) do
= image_tag(crop['photo'],
alt: crop['name'],
class: 'img img-card')
.text
%h3.crop-name
= link_to crop['name'], crop_path(slug: crop['slug'])
%h5.crop-sci-name
&nbsp;
= crop['scientific_name']
= render 'crops/search_result', crop: crop