Last active
August 29, 2015 14:25
-
-
Save keeperofthenecklace/287ba0994889d39f66a1 to your computer and use it in GitHub Desktop.
This is based of ElasticSearch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchController < ApplicationController | |
before_action :set_search_option, only: [:index] | |
# Query the product model and return all indexed items. | |
def index | |
@product_results = ProductsIndex.aggregations(search_aggregations) | |
apply_filters(@search_options[:search_with]).each { |filter| @product_results = @product_results.merge(filter) } | |
# Apply any facet filters already selected | |
if @search_term.present? | |
# Add search term to search and setup suggestions | |
@product_results = @product_results.merge(ProductsIndex.query(query_string: { query: prepare_query(@search_term), fields: search_fields })) | |
@product_results = @product_results.merge(ProductsIndex.suggest(suggestions: {text: @search_term, term: {field: :name}})) | |
end | |
@product_results = @product_results.merge(ProductsIndex.filter{ sold_out == false }) | |
@products = @product_results.order(@search_options[:order_by]).load(scope: -> { includes(:quote) }).per(8).page(@page) # Load the product results | |
end | |
def apply_filters(search_options = {}, type='product') | |
options = [] | |
case type | |
when 'product' | |
search_options.each_pair { |k,v| options << ProductsIndex.filter({term: {"#{k.to_s}_facet".to_sym => ActionController::Base.helpers.strip_tags(v)}}) unless k == 'type_facet' } | |
end | |
options | |
end | |
def prepare_query(query) | |
query = clean_query(query) | |
return query unless query.split(' ').first.present? | |
wc_query = '' | |
query.split(' ').each { |q| wc_query << (q.is_a?(Numeric) ? "+#{q} " : "+#{q}* ") } | |
wc_query.strip | |
end | |
def clean_query(query) | |
query = query.try(:squish) | |
query = query.gsub('"', '') if (query.count('"') % 2) != 0 | |
query = query.gsub(/[^a-zA-Z0-9']/, ' ').squish | |
query | |
end | |
# This attributes are passed | |
def search_fields | |
%w(quote author^5 year) | |
end | |
private | |
def set_search_option | |
search_conditions, search_with, order_by, search_term = search_options | |
@search_options = { search_conditions: search_conditions, search_with: search_with, order_by: order_by, search_term: search_term } | |
@display_search_by = cookies[:display_search_by].present? ? cookies[:display_search_by] : 'grid' | |
if params.present? && params[:search].present? | |
@search_term = params[:search].encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '') | |
@search_term = @search_term.encode!('UTF-8', 'UTF-16') | |
else | |
@search_term = params[:search] | |
end | |
@per_page = (params[:page] || 8).to_i | |
@page = (params[:page] || 1).to_i | |
@per_page = 8 if @per_page < 1 | |
@page = 1 if @page < 1 | |
@used_facets = search_with | |
@available_facets = product_aggregation_fields('quote') | |
@groups = params.select { |p| ['category', 'single', 'marriage'].include?(p) } | |
@filters = params.select { |p| ['price', 'count'].include?(p) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment