Skip to content

Commit

Permalink
rework API to use Solr when available
Browse files Browse the repository at this point in the history
  • Loading branch information
jywarren committed Apr 5, 2017
1 parent 62b25df commit 80eb40d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def test
term = params[:q] || "spectrometer"
@search = DrupalNode.search do
@search = Node.search do
fulltext term
end
render json: @search.results[0]
Expand Down
2 changes: 1 addition & 1 deletion app/models/drupal_node_revision.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class DrupalNodeRevision < ActiveRecord::Base

include SolrToggle
searchable if: :shouldIndexSolr do
searchable if: :solr_available? do
text :title
text :body do
body.to_s.gsub!(/[[:cntrl:]]/,'').to_s.slice(0..10000)
Expand Down
2 changes: 1 addition & 1 deletion app/models/drupal_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DrupalUsers < ActiveRecord::Base
end

def internalShouldIndexSolr
shouldIndexSolr && status == 1
solr_available? && status == 1
end

def user
Expand Down
2 changes: 1 addition & 1 deletion app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Node < ActiveRecord::Base
self.primary_key = 'nid'

include SolrToggle
searchable if: :shouldIndexSolr do
searchable if: :solr_available? do
text :title
text :body do
body.to_s.gsub!(/[[:cntrl:]]/,'').to_s.slice!(0..32500)
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class User < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :location_privacy

include SolrToggle
searchable if: :shouldIndexSolr do
searchable if: :solr_available? do
text :username, :email
end

Expand Down
20 changes: 14 additions & 6 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class SearchService

include SolrToggle

def initialize
end

Expand Down Expand Up @@ -50,14 +52,20 @@ def find_comments(input, limit=5)
end

## search for node title only
## FIXme with solr
def find_notes(input, limit=5)
Node.limit(limit)
.order('nid DESC')
.where('type = "note" AND node.status = 1 AND title LIKE ?', '%' + input + '%')
def find_notes(input, limit = 5)
if solr_available?
Node.search do
fulltext input
#paginate :page => 1, :per_page => 10
end
else
Node.limit(limit)
.order('nid DESC')
.where('type = "note" AND node.status = 1 AND title LIKE ?', '%' + input + '%')
end
end

def find_maps(input, limit=5)
def find_maps(input, limit = 5)
Node.limit(limit)
.order('nid DESC')
.where('type = "map" AND node.status = 1 AND title LIKE ?', '%' + input + '%')
Expand Down
2 changes: 1 addition & 1 deletion lib/solr_toggle/solr_toggle.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SolrToggle

def shouldIndexSolr
def solr_available?
begin
if !Sunspot::Rails.configuration.disabled?
Node.search do
Expand Down

0 comments on commit 80eb40d

Please sign in to comment.