Commit f8adf8bb by Tan Phat Nguyen

Add function search connect to solr

parent fc504b30
...@@ -18,3 +18,4 @@ ...@@ -18,3 +18,4 @@
# Ignore application configuration # Ignore application configuration
/config/application.yml /config/application.yml
/solr/data
...@@ -19,7 +19,8 @@ gem 'mysql2' ...@@ -19,7 +19,8 @@ gem 'mysql2'
gem 'vacuum' gem 'vacuum'
gem 'kaminari' gem 'kaminari'
gem 'devise' gem 'devise'
gem "figaro" gem 'figaro'
gem 'rsolr'
group :development, :test do group :development, :test do
gem 'sqlite3', '1.3.9' gem 'sqlite3', '1.3.9'
......
...@@ -94,7 +94,7 @@ GEM ...@@ -94,7 +94,7 @@ GEM
fog-core (~> 1.22) fog-core (~> 1.22)
fog-json fog-json
inflecto inflecto
fog-core (1.24.0) fog-core (1.25.0)
builder builder
excon (~> 0.38) excon (~> 0.38)
formatador (~> 0.2) formatador (~> 0.2)
...@@ -221,6 +221,8 @@ GEM ...@@ -221,6 +221,8 @@ GEM
json (~> 1.4) json (~> 1.4)
responders (2.0.2) responders (2.0.2)
railties (>= 4.2.0.alpha, < 5) railties (>= 4.2.0.alpha, < 5)
rsolr (1.0.10)
builder (>= 2.1.2)
ruby-progressbar (1.7.0) ruby-progressbar (1.7.0)
sass (3.4.8) sass (3.4.8)
sass-rails (5.0.0.beta1) sass-rails (5.0.0.beta1)
...@@ -305,6 +307,7 @@ DEPENDENCIES ...@@ -305,6 +307,7 @@ DEPENDENCIES
rails (= 4.2.0.beta4) rails (= 4.2.0.beta4)
rails-html-sanitizer (= 1.0.1) rails-html-sanitizer (= 1.0.1)
rails_12factor (= 0.0.2) rails_12factor (= 0.0.2)
rsolr
sass-rails (= 5.0.0.beta1) sass-rails (= 5.0.0.beta1)
sdoc (= 0.4.0) sdoc (= 0.4.0)
shoulda shoulda
......
require 'rsolr'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
......
class ProductsController < ApplicationController class ProductsController < ApplicationController
PAGINATE_PER = 10
Please register or sign in to reply
def show def show
@product = Product.find(params[:id]) @product = Product.find(params[:id])
end end
def index
Please register or sign in to reply
solr = RSolr.connect url: Rails.configuration.solr_host.to_s
  • @phatnt move this logic out of controller. Good place is service object with class Solr. This makes controller cleaner

Please register or sign in to reply
response = solr.paginate params[:page].to_i - 1, PAGINATE_PER, "select", params: { q: params[:search], wt: 'ruby' }
@products = response['response']['docs']
@products_paging = Kaminari.paginate_array(@products, total_count: response['response']['numFound']).page(params[:page]).per(PAGINATE_PER)
flash.now[:warning] = 'No results.' unless @products.any?
end
end end
...@@ -24,9 +24,10 @@ ...@@ -24,9 +24,10 @@
<% end %> <% end %>
<li style='width: 36px'><%= link_to '', cart_path(current_cart), class: 'nav-icon' %></li> <li style='width: 36px'><%= link_to '', cart_path(current_cart), class: 'nav-icon' %></li>
</ul> </ul>
<form class='navbar-form navbar-right'> <%= form_tag products_path, method: 'get', class: 'navbar-form navbar-right' do %>
Please register or sign in to reply
<input type='text' class='form-control' placeholder='Search...'> <%= text_field_tag :search, params[:search], placeholder: 'Search...', class: 'form-control' %>
</form> <% end %>
</div> </div>
</div> </div>
</nav> </nav>
\ No newline at end of file
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag Product.find(product['id']).photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<div class='prod-title'><h4><%= link_to product['name'], product_path(product['id']) %></h4></div>
<span class='price-col'>$<%= product['price'] %></span>
<%= button_to 'Add to Cart', line_items_path(product_id: product['id']), { class: 'btn btn-default' } %>
</div>
\ No newline at end of file
<%= provide(:title, 'Search Result') %>
<h1 class='page-header'>Search Result</h1>
<div class='row placeholders'>
<%= render partial: 'search_product', collection: @products, as: :product %>
</div>
<%= paginate @products_paging, :outer_window => 2 %>
\ No newline at end of file
...@@ -46,4 +46,6 @@ Rails.application.configure do ...@@ -46,4 +46,6 @@ Rails.application.configure do
# Raises error for missing translations # Raises error for missing translations
# config.action_view.raise_on_missing_translations = true # config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.solr_host = "http://localhost:8983/solr/collection1"
end end
...@@ -17,6 +17,8 @@ Rails.application.routes.draw do ...@@ -17,6 +17,8 @@ Rails.application.routes.draw do
get '/category/:id', to: 'categories#show', as: 'category' get '/category/:id', to: 'categories#show', as: 'category'
get 'products' => 'products#index'
resources :products, only: [:show] resources :products, only: [:show]
get 'home' => 'home#index' get 'home' => 'home#index'
......
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The content of this page will be statically included into the top
of the admin page. Uncomment this as an example to see there the content
will show up.
<hr>
<i>This line will appear before the first table</i>
<tr>
<td colspan="2">
This row will be appended to the end of the first table
</td>
</tr>
<hr>
-->
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- If this file is found in the config directory, it will only be
loaded once at startup. If it is found in Solr's data
directory, it will be re-loaded every commit.
-->
<elevate>
<query text="foo bar">
<doc id="1" />
<doc id="2" />
<doc id="3" />
</query>
<query text="ipod">
<doc id="MA147LL/A" /> <!-- put the actual ipod at the top -->
<doc id="IW-02" exclude="true" /> <!-- exclude this cable -->
</query>
</elevate>
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Syntax:
# "source" => "target"
# "source".length() > 0 (source cannot be empty.)
# "target".length() >= 0 (target can be empty.)
# example:
# "À" => "A"
# "\u00C0" => "A"
# "\u00C0" => "\u0041"
# "ß" => "ss"
# "\t" => " "
# "\n" => ""
# À => A
"\u00C0" => "A"
# Á => A
"\u00C1" => "A"
# Â => A
"\u00C2" => "A"
# Ã => A
"\u00C3" => "A"
# Ä => A
"\u00C4" => "A"
# Å => A
"\u00C5" => "A"
# Æ => AE
"\u00C6" => "AE"
# Ç => C
"\u00C7" => "C"
# È => E
"\u00C8" => "E"
# É => E
"\u00C9" => "E"
# Ê => E
"\u00CA" => "E"
# Ë => E
"\u00CB" => "E"
# Ì => I
"\u00CC" => "I"
# Í => I
"\u00CD" => "I"
# Î => I
"\u00CE" => "I"
# Ï => I
"\u00CF" => "I"
# IJ => IJ
"\u0132" => "IJ"
# Ð => D
"\u00D0" => "D"
# Ñ => N
"\u00D1" => "N"
# Ò => O
"\u00D2" => "O"
# Ó => O
"\u00D3" => "O"
# Ô => O
"\u00D4" => "O"
# Õ => O
"\u00D5" => "O"
# Ö => O
"\u00D6" => "O"
# Ø => O
"\u00D8" => "O"
# Œ => OE
"\u0152" => "OE"
# Þ
"\u00DE" => "TH"
# Ù => U
"\u00D9" => "U"
# Ú => U
"\u00DA" => "U"
# Û => U
"\u00DB" => "U"
# Ü => U
"\u00DC" => "U"
# Ý => Y
"\u00DD" => "Y"
# Ÿ => Y
"\u0178" => "Y"
# à => a
"\u00E0" => "a"
# á => a
"\u00E1" => "a"
# â => a
"\u00E2" => "a"
# ã => a
"\u00E3" => "a"
# ä => a
"\u00E4" => "a"
# å => a
"\u00E5" => "a"
# æ => ae
"\u00E6" => "ae"
# ç => c
"\u00E7" => "c"
# è => e
"\u00E8" => "e"
# é => e
"\u00E9" => "e"
# ê => e
"\u00EA" => "e"
# ë => e
"\u00EB" => "e"
# ì => i
"\u00EC" => "i"
# í => i
"\u00ED" => "i"
# î => i
"\u00EE" => "i"
# ï => i
"\u00EF" => "i"
# ij => ij
"\u0133" => "ij"
# ð => d
"\u00F0" => "d"
# ñ => n
"\u00F1" => "n"
# ò => o
"\u00F2" => "o"
# ó => o
"\u00F3" => "o"
# ô => o
"\u00F4" => "o"
# õ => o
"\u00F5" => "o"
# ö => o
"\u00F6" => "o"
# ø => o
"\u00F8" => "o"
# œ => oe
"\u0153" => "oe"
# ß => ss
"\u00DF" => "ss"
# þ => th
"\u00FE" => "th"
# ù => u
"\u00F9" => "u"
# ú => u
"\u00FA" => "u"
# û => u
"\u00FB" => "u"
# ü => u
"\u00FC" => "u"
# ý => y
"\u00FD" => "y"
# ÿ => y
"\u00FF" => "y"
# ff => ff
"\uFB00" => "ff"
# fi => fi
"\uFB01" => "fi"
# fl => fl
"\uFB02" => "fl"
# ffi => ffi
"\uFB03" => "ffi"
# ffl => ffl
"\uFB04" => "ffl"
# ſt => ft
"\uFB05" => "ft"
# st => st
"\uFB06" => "st"
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
# Use a protected word file to protect against the stemmer reducing two
# unrelated words to the same base word.
# Some non-words that normally won't be encountered,
# just to test that they won't be stemmed.
dontstems
zwhacky
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
user=
solr_hostname=localhost
solr_port=8983
rsyncd_port=18983
data_dir=
webapp_name=solr
master_host=
master_data_dir=
master_status_dir=
pizza
history
\ No newline at end of file
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
# a couple of test stopwords to test that the words are really being
# configured from this file:
stopworda
stopwordb
#Standard english stop words taken from Lucene's StopAnalyzer
a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
s
such
t
that
the
their
then
there
these
they
this
to
was
will
with
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
#some test synonym mappings unlikely to appear in real input text
aaa => aaaa
bbb => bbbb1 bbbb2
ccc => cccc1,cccc2
a\=>a => b\=>b
a\,a => b\,b
fooaaa,baraaa,bazaaa
# Some synonym groups specific to this example
GB,gib,gigabyte,gigabytes
MB,mib,megabyte,megabytes
Television, Televisions, TV, TVs
#notice we use "gib" instead of "GiB" so any WordDelimiterFilter coming
#after us won't split it into two words.
# Synonym mappings can be used for spelling correction too
pixima => pixma
<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
<cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
<core name="default" instanceDir="." dataDir="default/data"/>
<core name="development" instanceDir="." dataDir="development/data"/>
<core name="test" instanceDir="." dataDir="test/data"/>
</cores>
</solr>
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