don't query for photos that we never read

This commit is contained in:
Brenda Wallace committed 2020-01-01 12:32:23 +13:00
1 parent 41c90acbbc
commit d46883f39b
2 files changed
+20 -20

No files matched your search

+8 -8
View File
@@ -4,7 +4,7 @@ require 'will_paginate/array'
class CropsController < ApplicationController
before_action :authenticate_member!, except: %i(index hierarchy search show)
load_and_authorize_resource
load_and_authorize_resource id_param: :slug
skip_authorize_resource only: %i(hierarchy search)
respond_to :html, :json, :rss, :csv, :svg
responders :flash
@@ -54,19 +54,19 @@ class CropsController < ApplicationController
def search
@term = params[:term]
@crops = CropSearchService.search(
@term, page: params[:page],
per_page: 36,
current_member: current_member
)
@crops = CropSearchService.search(@term,
page: params[:page],
per_page: Crop.per_page,
current_member: current_member)
respond_with @crops
end
def show
@crop = Crop.includes(:scientific_names, :alternate_names, :parent, :varieties).find_by!(slug: params[:slug])
@crop = Crop.includes(
:scientific_names, :alternate_names, :parent, :varieties
).find_by!(slug: params[:slug])
respond_to do |format|
format.html do
@photos = Photo.by_crop(@crop)
@posts = @crop.posts.order(created_at: :desc).paginate(page: params[:page])
@companions = @crop.companions.approved
end
+12 -12
View File
@@ -4,40 +4,40 @@ class CropSearchService
# Crop.search(string)
def self.search(query, page: 1, per_page: 12, current_member: nil)
search_params = {
page: page,
per_page: per_page,
fields: %i(name^5 alternate_names scientific_names),
match: :word_start,
boost_by: [:plantings_count],
includes: %i(scientific_names alternate_names),
page: page,
per_page: per_page,
fields: %i(name^5 alternate_names scientific_names),
match: :word_start,
boost_by: [:plantings_count],
includes: %i(scientific_names alternate_names),
misspellings: { edit_distance: 2 }
}
# prioritise crops the member has planted
search_params[:boost_where] = { planters_ids: current_member.id } if current_member
Crop.search(query, search_params)
Crop.search(query, search_params, load: false)
end
def self.random_with_photos(limit)
body = {
"query": {
"function_score": {
"query": { "query_string": { "query": 'has_photos:true' } },
"query": { "query_string": { "query": 'has_photos:true' } },
"random_score": { "seed": DateTime.now.to_i }
}
}
}
Crop.search(
limit: limit,
load: false,
body: body
load: false,
body: body
)
end
def self.recent(limit)
Crop.search(
limit: limit,
load: false,
limit: limit,
load: false,
boost_by: { created_at: { factor: 100 } } # default factor is 1
)
end