Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

User can choose to use get http method #276

Closed
wants to merge 4 commits into from

5 participants

@xinuc

There are a lot of use cases where using get is acceptable.
Using get makes it easy for us to use http caching like varnish etc

Review on Reviewable

@travisbot

This pull request fails (merged d332677 into 8f33061).

@travisbot

This pull request fails (merged c10cf2e into 8f33061).

@alindeman
Owner

The build is failing. Any idea why?

@xinuc

travis ci keeps telling

RuntimeError:
Factory can only create an instance once!

Pretty sure this is not caused by my commits.
The specs pass on my local machine.

@alindeman
Owner

I think having the configuration default to post is better. And instead of a conditional, we just __send__ the verb specified. Does this make sense?

@xinuc

Yes, the default is still using post

I think we can't use __send__ cause rsolr have a slightly different syntax for them.
post uses data option, while get uses params.

To change the http method, user have to add

Sunspot.config.solr.http_method = :get

somewhere.

I'm not sure this is the best api though.

Any idea?

@alindeman
Owner

Can we have http_method default to get? See https://github.com/sunspot/sunspot/blob/master/sunspot/lib/sunspot/configuration.rb for examples of defaults.

@xinuc

Of course.
I think using get as default method is a sensible configuration.

@xinuc

finally, good to merge :+1:

@rywall
Owner

This pull request has conflicts. Can you rebase it?

@joneslee85
Owner

@xinuc I am going to close it since it is so out of date. Feel free to reopen if you are active again.

@joneslee85 joneslee85 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 29, 2012
  1. @xinuc
  2. @xinuc
Commits on Nov 26, 2012
  1. @xinuc

    use get as default http_method

    xinuc authored
  2. @xinuc

    fix connection mock

    xinuc authored
This page is out of date. Refresh to see the latest.
View
3  sunspot/lib/sunspot/configuration.rb
@@ -23,6 +23,7 @@ def build #:nodoc:
LightConfig.build do
solr do
url 'http://127.0.0.1:8983/solr'
+ http_method 'get'
read_timeout nil
open_timeout nil
end
@@ -37,7 +38,7 @@ def build #:nodoc:
end
end
end
-
+
# Location for the default solr configuration files,
# required for bootstrapping a new solr installation
#
View
76 sunspot/lib/sunspot/search/abstract_search.rb
@@ -3,15 +3,15 @@
module Sunspot
module Search #:nodoc:
-
- #
+
+ #
# This class encapsulates the results of a Solr search. It provides access
# to search results, total result count, facets, and pagination information.
# Instances of Search are returned by the Sunspot.search and
# Sunspot.new_search methods.
#
class AbstractSearch
- #
+ #
# Retrieve all facet objects defined for this search, in order they were
# defined. To retrieve an individual facet by name, use #facet()
#
@@ -20,9 +20,9 @@ class AbstractSearch
attr_accessor :request_handler
include HitEnumerable
-
+
def initialize(connection, setup, query, configuration) #:nodoc:
- @connection, @setup, @query = connection, setup, query
+ @connection, @setup, @query, @configuration = connection, setup, query, configuration
@query.paginate(1, configuration.pagination.default_per_page)
@facets = []
@@ -31,7 +31,7 @@ def initialize(connection, setup, query, configuration) #:nodoc:
@groups_by_name = {}
@groups = []
end
-
+
#
# Execute the search on the Solr instance and store the results. If you
# use Sunspot#search() to construct your searches, there is no need to call
@@ -42,15 +42,19 @@ def initialize(connection, setup, query, configuration) #:nodoc:
def execute
reset
params = @query.to_params
- @solr_result = @connection.post "#{request_handler}", :data => params
+ if @configuration.solr.http_method.to_s == 'post'
+ @solr_result = @connection.post "#{request_handler}", :data => params
+ else
+ @solr_result = @connection.get "#{request_handler}", :params => params
+ end
self
end
def execute! #:nodoc: deprecated
execute
end
-
- #
+
+ #
# Get the collection of results as instantiated objects. If WillPaginate is
# available, the results will be a WillPaginate::Collection instance; if
# not, it will be a vanilla Array.
@@ -65,8 +69,8 @@ def execute! #:nodoc: deprecated
def results
@results ||= paginate_collection(verified_hits.map { |hit| hit.instance })
end
-
- #
+
+ #
# Access raw Solr result information. Returns a collection of Hit objects
# that contain the class name, primary key, keyword relevance score (if
# applicable), and any stored fields.
@@ -92,7 +96,7 @@ def hits(options = {})
end
alias_method :raw_results, :hits
- #
+ #
# The total number of documents matching the query parameters
#
# ==== Returns
@@ -102,8 +106,8 @@ def hits(options = {})
def total
@total ||= solr_response['numFound'] || 0
end
-
- #
+
+ #
# The time elapsed to generate the Solr response
#
# ==== Returns
@@ -113,8 +117,8 @@ def total
def query_time
@query_time ||= solr_response_header['QTime']
end
-
- #
+
+ #
# Get the facet object for the given name. `name` can either be the name
# given to a query facet, or the field name of a field facet. Returns a
# Sunspot::Facet object.
@@ -169,14 +173,14 @@ def group(name)
@groups_by_name[name.to_sym]
end
end
-
- #
+
+ #
# Deprecated in favor of optional second argument to #facet
#
def dynamic_facet(base_name, dynamic_name) #:nodoc:
facet(base_name, dynamic_name)
end
-
+
def facet_response #:nodoc:
@solr_result['facet_counts']
end
@@ -184,8 +188,8 @@ def facet_response #:nodoc:
def group_response #:nodoc:
@solr_result['grouped']
end
-
- #
+
+ #
# Build this search using a DSL block. This method can be called more than
# once on an unexecuted search (e.g., Sunspot.new_search) in order to build
# a search incrementally.
@@ -202,8 +206,8 @@ def build(&block)
Util.instance_eval_or_call(dsl, &block)
self
end
-
-
+
+
def inspect #:nodoc:
"<Sunspot::Search:#{query.to_params.inspect}>"
end
@@ -211,16 +215,16 @@ def inspect #:nodoc:
def add_field_group(field, options = {}) #:nodoc:
add_group(field.name, FieldGroup.new(field, self, options))
end
-
+
def add_field_facet(field, options = {}) #:nodoc:
name = (options[:name] || field.name)
add_facet(name, FieldFacet.new(field, self, options))
end
-
+
def add_query_facet(name, options) #:nodoc:
add_facet(name, QueryFacet.new(name, self, options))
end
-
+
def add_date_facet(field, options) #:nodoc:
name = (options[:name] || field.name)
add_facet(name, DateFacet.new(field, self, options))
@@ -236,21 +240,21 @@ def highlights_for(doc) #:nodoc:
@solr_result['highlighting'][doc['id']]
end
end
-
+
private
-
+
def dsl
- raise NotImplementedError
+ raise NotImplementedError
end
-
+
def execute_request(params)
raise NotImplementedError
end
-
+
def solr_response
@solr_response ||= @solr_result['response'] || {}
end
-
+
def solr_response_header
@solr_response_header ||= @solr_result['responseHeader'] || {}
end
@@ -258,15 +262,15 @@ def solr_response_header
def solr_docs
solr_response['docs']
end
-
+
def verified_hits
@verified_hits ||= paginate_collection(super)
end
-
+
def paginate_collection(collection)
PaginatedCollection.new(collection, @query.page, @query.per_page, total)
end
-
+
def add_facet(name, facet)
@facets << facet
@facets_by_name[name.to_sym] = facet
@@ -276,7 +280,7 @@ def add_group(name, group)
@groups << group
@groups_by_name[name.to_sym] = group
end
-
+
# Clear out all the cached ivars so the search can be called again.
def reset
@results = @hits = @verified_hits = @total = @solr_response = @doc_ids = nil
View
8 sunspot/spec/mocks/connection.rb
@@ -62,6 +62,14 @@ def post(path, params)
@response || {}
end
+ def get(path, params)
+ unless path == "#{@expected_handler}"
+ raise ArgumentError, "Expected request to #{@expected_handler} request handler"
+ end
+ @searches << @last_search = params[:params]
+ @response || {}
+ end
+
def method_missing(method, *args, &block)
get("#{method}", *args)
end
Something went wrong with that request. Please try again.