Skip to content

Commit

Permalink
initial search functionality with sunspot. backend, no ui
Browse files Browse the repository at this point in the history
  • Loading branch information
jeloi committed Jan 16, 2014
1 parent d73780c commit aa2895d
Show file tree
Hide file tree
Showing 60 changed files with 1,535 additions and 16 deletions.
12 changes: 7 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ gem 'rmagick'

gem 'jquery-turbolinks'

# gem 'zurb-foundation'
# gem 'foundation-rails', github: 'ahacking/foundation-rails'
gem 'foundation-rails'
gem 'omniauth-facebook', '1.4.0'
Expand All @@ -28,15 +27,18 @@ gem 'friendly_id', '5.0.0.beta4'
# gem 'chosen-rails', github: 'Jeloi/chosen-rails'
gem 'chosen-rails'
gem 'compass-rails', github: 'Compass/compass-rails'
# gem 'select2-rails'


gem 'sunspot_rails'
gem 'sunspot_solr'

gem 'mysql2'

# For making development life better
gem 'hirb'
gem 'quiet_assets'

group :development, :test do
# For making development life better
gem 'hirb'
gem 'quiet_assets'
gem 'sqlite3', '1.3.7'
gem 'rspec-rails'
# The following optional lines are part of the advanced setup.
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ GEM
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
polyglot (0.3.3)
pr_geohash (1.0.0)
pry (0.9.12.4)
coderay (~> 1.0)
method_source (~> 0.8)
Expand Down Expand Up @@ -230,6 +231,8 @@ GEM
rdoc (3.12.2)
json (~> 1.4)
rmagick (2.13.2)
rsolr (1.0.9)
builder (>= 2.1.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
Expand Down Expand Up @@ -281,6 +284,14 @@ GEM
net-scp (>= 1.1.2)
net-ssh
term-ansicolor
sunspot (2.1.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)
sunspot_rails (2.1.0)
nokogiri
rails (>= 3)
sunspot (= 2.1.0)
sunspot_solr (2.1.0)
term-ansicolor (1.2.2)
tins (~> 0.8)
terminal-notifier-guard (1.5.3)
Expand Down Expand Up @@ -352,6 +363,8 @@ DEPENDENCIES
selenium-webdriver
shoulda
sqlite3 (= 1.3.7)
sunspot_rails
sunspot_solr
terminal-notifier-guard
turbolinks (= 1.1.1)
uglifier (= 2.1.1)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/search.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/search.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
35 changes: 35 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class SearchController < ApplicationController
def search
everything = (params[:context] == "Everything")
@total_results = 0
if everything || params[:context] == "Tags"
@tag_results = Tag.search do
keywords params[:keywords] do
minimum_match 1
query_phrase_slop 1
end
end.results
logger.debug { "Tags: " + @tag_results.size.to_s }
@total_results += @tag_results.size
end
if everything || params[:context] == "Recipes"
@recipe_results = Recipe.search do
keywords params[:keywords] do
minimum_match 1
end
end.results
logger.debug { "Recipes: " + @recipe_results.size.to_s }
@total_results += @recipe_results.size
end
if everything || params[:context] == "Ingredients"
@item_results = Item.search do
keywords params[:keywords] do
minimum_match 1
end
end.results
logger.debug { @item_results }
@total_results += @item_results.size
end
logger.debug { @total_results }
end
end
2 changes: 2 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SearchHelper
end
5 changes: 5 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Item < ActiveRecord::Base

validates_uniqueness_of :name, message: "must be unique!"

# Sunspot Solr Search
searchable do
text :name
end

# Sets the this item's item_category to "Other" if one is not specified
def set_item_category
if self.item_category.nil?
Expand Down
10 changes: 10 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Recipe < ActiveRecord::Base
# Scopes
scope :in_last_month, -> { where('created_at > ?', Time.now.months_ago(1)) }

# Sunspot Solr Search
searchable do
text :title, boost: 5
text :blurb, boost: 3
text :directions
text :tags do
tags.map { |tag| tag.name }
end
end

# Constants
BITE_WEIGHT = 1.0
FAV_WEIGHT = 1.8
Expand Down
8 changes: 8 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ class Tag < ActiveRecord::Base
belongs_to :tag_category

validates_uniqueness_of :name, :on => :save, :message => "must be unique"

# Sunspot Solr Search
searchable do
text :name, boost: 2
text :tag_category do
tag_category.name
end
end
end
17 changes: 8 additions & 9 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@
<!-- <h1 class="middbites"> middbites </h1> -->
<%= image_tag "logo.png", class: "logo" %>
<% end %>
<!-- <div class="slogan">discover & share the best bites @ midd</div> -->
<!--
<form method="get" id="search-form" action="" >
<div id="search">
<input class="field" type="text" placeholder="Search anything.." name="search" id="search-input" />
<input class="submit" id="search-submit" type="submit" value=" " />
<div style="clear: both;"></div>
</div>
</form> -->



<div id="sticky_navigation_wrapper">
<div id="sticky_navigation">
<div class="row">
Expand All @@ -57,6 +50,12 @@
</div>
</div>
</header>

<%= form_tag(search_path, {method: :get, :class => "form"}) do %>
<%= text_field_tag :keywords, params[:keywords] %>
<%= select_tag :context, options_for_select(["Everything", "Recipes", "Tags", "Ingredients"], "Everything")%>
<%= submit_tag "Search", name: nil %>
<% end -%>

<div class="hide-for-large" id="mobile-nav-wrapper">
<nav class="hide-for-large-up fixed">
Expand Down
2 changes: 2 additions & 0 deletions app/views/search/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Search#search</h1>
<p>Find me in app/views/search/search.html.erb</p>
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@



get "comments/create"
get "comments/destroy"

root 'pages#home'

get "pages/home"
Expand All @@ -20,6 +19,9 @@
delete 'recipes/:id/unvote' => 'recipes#unvote', as: 'unvote_recipe'
post 'recipes/:id/vote' => 'recipes#vote', as: 'vote_recipe'

# Search Routes
get "search", to: 'search#search', as: 'search'

# Comment Routes
resources :comments, only: [:create, :destroy]

Expand Down
23 changes: 23 additions & 0 deletions config/sunspot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
production:
solr:
hostname: localhost
port: 8983
log_level: WARNING
path: /solr/production
# read_timeout: 2
# open_timeout: 0.5

development:
solr:
hostname: localhost
port: 8982
log_level: INFO
path: /solr/development

test:
solr:
hostname: localhost
port: 8981
log_level: WARNING
path: /solr/test

Empty file.
31 changes: 31 additions & 0 deletions solr/conf/admin-extra.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
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>
-->
36 changes: 36 additions & 0 deletions solr/conf/elevate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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>
Loading

0 comments on commit aa2895d

Please sign in to comment.