Using elastic for plantings look ups

This commit is contained in:
Brenda Wallace
2019-12-11 11:38:34 +13:00
parent b0d55ad064
commit b44877a63e
5 changed files with 44 additions and 29 deletions

View File

@@ -10,20 +10,28 @@ class PlantingsController < ApplicationController
responders :flash
def index
@owner = Member.find_by(slug: params[:member_slug]) if params[:member_slug]
@crop = Crop.find_by(slug: params[:crop_slug]) if params[:crop_slug]
@show_all = params[:all] == '1'
@plantings = @plantings.where(owner: @owner) if @owner.present?
@plantings = @plantings.where(crop: @crop) if @crop.present?
@where = {}
@where[:active] = true unless @show_all
@plantings = @plantings.active unless params[:all] == '1'
if params[:member_slug]
@owner = Member.find_by(slug: params[:member_slug])
@where[:owner_id] = @owner.id
end
@plantings = @plantings.joins(:owner, :crop, :garden)
.order(created_at: :desc)
.includes(:owner, :garden, crop: :parent)
.paginate(page: params[:page])
if params[:crop_slug]
@crop = Crop.find_by(slug: params[:crop_slug])
@where[:crop_id] = @crop.id
end
@plantings = Planting.search(
page: params[:page],
limit: 100,
boost_by: [:created_at],
load: false
)
@filename = "Growstuff-#{specifics}Plantings-#{Time.zone.now.to_s(:number)}.csv"
@@ -31,10 +39,16 @@ class PlantingsController < ApplicationController
end
def show
@planting = Planting.includes(:owner, :crop, :garden)
.find(params[:slug])
@photos = @planting.photos.includes(:owner).order(date_taken: :desc)
@harvests = Harvest.search(where: { planting_id: @planting.id } )
@matching_seeds = matching_seeds
# TODO use elastic search long/lat
@neighbours = @planting.nearby_same_crop
.where.not(id: @planting.id)
.includes(:owner, :crop, :garden)
.limit(6)
respond_with @planting
end

View File

@@ -2,8 +2,7 @@ module CropSearch
extend ActiveSupport::Concern
included do
searchkick word_start: %i(name alternate_names scientific_names), case_sensitive: false
searchkick word_start: %i(name description), case_sensitive: false
scope :search_import, -> { includes(:scientific_names, :photos) }
def should_index?
@@ -13,6 +12,7 @@ module CropSearch
def search_data
{
name: name,
description: description,
slug: slug,
alternate_names: alternate_names.pluck(:name),
scientific_names: scientific_names.pluck(:name),
@@ -25,7 +25,6 @@ module CropSearch
has_photos: photo_associations_count.positive?,
thumbnail_url: default_photo&.thumbnail_url,
scientific_name: default_scientific_name&.name,
description: description,
created_at: created_at.to_i
}
end

View File

@@ -8,18 +8,20 @@ module PlantingSearch
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,
planted_from: planted_from,
photos_count: photos.size,
harvests_count: photos.size,
has_photos: photos.size.positive?,
thumbnail_url: default_photo&.thumbnail_url,
created_at: created_at.to_i
slug: slug,
crop_slug: crop.slug,
crop_name: crop.name,
crop_id: crop_id,
owner_id: owner_id,
owner_name: owner.login_name,
planted_from: planted_from,
photos_count: photos.size,
harvests_count: harvests.size,
has_photos: photos.size.positive?,
active: active?,
thumbnail_url: default_photo&.thumbnail_url,
percentage_grown: percentage_grown.to_i,
created_at: created_at.to_i
}
end

View File

@@ -20,9 +20,9 @@
include finished plantings
%hr
- if @owner.present?
= render @owner
= render @owner, cached: true
- if @crop.present?
= render @crop
= render @crop, cached: true
%section.open-data
%h2 Open Data
@@ -41,6 +41,6 @@
= will_paginate @plantings
.index-cards
- @plantings.each do |planting|
= render planting, full: true
.card= planting
= will_paginate @plantings

View File

@@ -30,7 +30,7 @@ Rails.application.routes.draw do
get 'timeline' => 'charts/gardens#timeline', constraints: { format: 'json' }
end
resources :plantings, concerns: :has_photos do
resources :plantings, concerns: :has_photos, param: :slug do
resources :harvests
resources :seeds
collection do
@@ -43,7 +43,7 @@ Rails.application.routes.draw do
get 'crop/:crop' => 'seeds#index', as: 'seeds_by_crop', on: :collection
end
resources :harvests, concerns: :has_photos do
resources :harvests, concerns: :has_photos, param: :slug do
get 'crop/:crop' => 'harvests#index', as: 'harvests_by_crop', on: :collection
end