ruby_newbie
ruby_newbie

Reputation: 3285

Troubleshooting Solr search in Rails project

A Rails project I am working on is using Solr search but the search feature doesn't return results when certain data is entered.

The search feature should return results if passed an email, guid(global unique identifier), a phone number, or the last 4 of a customer's credit card. Currently it will only work with the email, or the guid.

I don't even know where to begin troubleshooting because everytime I search for Solr search I just return results for the sunspot gem which the project is not using.

The Solr search module which is included in the Application Controller looks like:

module SolrSearch
  def solr_query(query_string)
    query_string << "&fq=publisher_name:#{UMS[:publisher_name]}"
    uri = SOLR_URL + "/select/?version=2.2&start=0&rows=100&indent=on&qt=dismax&wt=json&fl=id,guid,email,user_name,publisher_name,csg_id,state,created_at&q=#{query_string.downcase.strip}"
    response = Curl::Easy.http_get(URI.escape(uri))
    result = JSON.parse(response.body_str)
    return result
  end
end

If I don't enter anything in the search field I get back an array of users with the guid, user_name, email, publisher_name, state, and created_at.

My gemfile looks like:

source 'https://rubygems.org'

gem 'rails', '3.2.13'

gem "cancan"
gem 'curb', '0.8.1'
gem 'haml', '4.0.0'
gem 'jquery-rails'
gem 'json', '1.7.7'
gem 'oauth', '0.4.7'
gem 'ohm'
gem 'omniauth', '>= 1.0.0'
gem "omniauth_crowd", '2.1.1'
gem 'recurly', :git => "https://github.com/sherbet/recurly-client-ruby.git", :branch => 'sherbet-2.1.9.4'
gem 'redis'
gem 'sidekiq', '3.2.1'
gem 'rest-client', '~> 1.7.2'
gem 'slim', '>= 1.1.0'
gem 'sinatra', '>= 1.3.0', :require => nil
gem 'typhoeus', '0.5.3'
gem 'money'
gem 'newrelic_rpm'
gem 'uuidtools', '2.1.3'
gem "vertex", :git => "[email protected]:sherbet/vertex.git", :branch => "releases/1.0.0"
gem 'unicorn'
gem 'whenever', :require => false
gem 'cellophane', :git => "[email protected]:sherbet/cellophane.git", :require => 'cellophane', :branch => "convert_gem"

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

group :development do
  gem 'pry'
  gem 'pry-doc'
  gem 'pry-rails'
  gem 'pry-nav'
end

# For Code Climate
gem "codeclimate-test-reporter", group: :test, require: nil

group :test do
  gem 'cucumber-rails', '1.4.0',  :require => false
  gem 'rspec-rails'
  gem 'pickle', '0.4.11'
  gem 'capybara', '2.2.1'
  gem 'chromedriver-helper'
  gem 'launchy', '2.4.2'
  gem 'selenium-webdriver', '2.41.0'
  gem 'timecop', '0.7.1'
  gem 'vcr', '2.9.0'
  gem 'webmock', '1.17.4'
  gem 'factory_girl_rails','~> 4.0'
  gem 'database_cleaner', '1.2.0'
  gem 'json_spec',       '~> 1.1'
  gem 'simplecov'
  gem 'simplecov-rcov'
end

group :development, :test do
  gem 'ci_reporter_cucumber'
  gem 'ci_reporter_rspec'
  gem 'capistrano-rails'
  gem 'capistrano3-unicorn'
  gem 'capistrano-bundler'
  gem 'capistrano-rvm'
end

Obviously I have tried adding phone_number to the URI string and that didn't work.

How can I start to troubleshoot this?


As per Tinman's suggestion I tried using the URI class to encode the query string. I did:

module SolrSearch
  require 'uri'

  def solr_query(query_string)
    uri = URI.parse(SOLR_ICEBOX_URL)
    uri.query = URI.encode_www_form(
      'fq' => "publisher_name:#{UMS[:publisher_name]}",
      'version' => 2.2,
      'start' => 0,
      'rows' => 100,
      'indent' => 'on',
      'qt' => 'dismax',
      'wt' => 'json',
      'fl' => 'id,guid,email,user_name,publisher_name,csg_id,state,created_at',
      'q' => query_string.downcase.strip
    )
    result = JSON.parse(response.body)
  end
end

Upvotes: 0

Views: 99

Answers (1)

the Tin Man
the Tin Man

Reputation: 160631

As far as building the URL, don't do it using string concatenation or interpolation unless you are ABSOLUTELY sure the resulting string is correctly encoded.

Instead, I'd recommend taking advantage of the built-in URI class, which will take care of that for you:

require 'uri'

SOLR_URL = 'http://somehost.com/select/'
UMS = {publisher_name: 'some company'}
query_string = 'foo bar'

uri = URI.parse(SOLR_URL)
uri.query = URI.encode_www_form(
  'fq' => "publisher_name:#{UMS[:publisher_name]}",
  'version' => 2.2,
  'start' => 0,
  'rows' => 100,
  'indent' => 'on',
  'qt' => 'dismax',
  'wt' => 'json',
  'fl' => 'id,guid,email,user_name,publisher_name,csg_id,state,created_at',
  'q' => query_string.downcase.strip
)

Running that results in uri looking like:

uri.to_s
# => "http://somehost.com/select/?fq=publisher_name%3Asome+company&version=2.2&start=0&rows=100&indent=on&qt=dismax&wt=json&fl=id%2Cguid%2Cemail%2Cuser_name%2Cpublisher_name%2Ccsg_id%2Cstate%2Ccreated_at&q=foo+bar"

Upvotes: 1

Related Questions