Commit 52718197 by Nguyen Quoc Kien

Search solr

parent a8fbe918
......@@ -29,7 +29,7 @@ gem 'vacuum', '~> 1.3.0'
gem "figaro"
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'solr-ruby'
# Use ActiveModel has_secure_password
......
......@@ -137,6 +137,7 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
solr-ruby (0.0.8)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
......@@ -186,6 +187,7 @@ DEPENDENCIES
rails (= 4.2.2)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
solr-ruby
spring
turbolinks
uglifier (>= 1.3.0)
......
require 'solr'
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
......
class SearchController < ApplicationController
before_action :check_page, only: [:search]
=begin
def search
if params[:keyword].nil?
@products = []
......@@ -8,4 +9,35 @@ class SearchController < ApplicationController
@products = Product.search(params[:keyword]).paginate(page: params[:page]).per_page(18)
end
end
=end
def search
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on )
keyword = params[:keyword]
query = "((name:#{keyword}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query, 'wt' => "xml", 'rows' => 2000, 'indent' => true})
if keyword.index( /[^[:alnum:]]/ ) == nil
@result_total = @solr.send(select_obj)
@result_products = get_result_solr(@result_total)
@products = get_products(@result_products)
else
@products = []
end
end
private
def get_result_solr(result_total)
products = result_total.data['response']['docs']
end
def get_products(result_products)
products = Array.new
result_products.each do |iProduct|
product = Product.find(iProduct['id'])
products << product
end
return products
end
end
<% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2>
<%= will_paginate @products %>
<div class="col-md-12" style="text-align: center;">
<% if @products!= [] %>
<% @products.each do |product| %>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<%= image_tag(product.image, alt: product.name, style: 'height: 300px') %>
<div class="caption">
<div class="div_product_name" style="width: 235px; height: 53px;">
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<%= form_tag cart_products_path do %>
<p>
<%= hidden_field_tag :product_id, product.id %>
Quantity:<b> <%= number_field_tag :quantity, "1", class: 'form-control' %></b><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p>
<% end %>
<%= link_to "More Info", product, :class => "btn btn-default" %>
</div>
</div>
</div>
<% end %>
<% else %>
<h1> Not found</h1>
<% end %>
</div>
<div><%= will_paginate @products %></div>
<% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2>
<%= will_paginate @products %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %> - <%= @products.count %> resulfs</h2>
<div class="col-md-12" style="text-align: center;">
<% if @products!= [] %>
<% @products.each do |product| %>
......@@ -28,4 +28,4 @@
<h1> Not found</h1>
<% end %>
</div>
<div><%= will_paginate @products %></div>
<h1>Pages not found!</h1>
\ No newline at end of file
<div class="dialog">
<div>
<h1>We're sorry, but something went wrong.</h1>
</div>
<p>If you are the application owner check the logs for more information.</p>
</div>
\ No newline at end of file
......@@ -36,6 +36,8 @@ Rails.application.configure do
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
#solr
config.solr_host = "http://localhost:8080/solr/core0"
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment