Giter VIP home page Giter VIP logo

spree_sunspot_search's Introduction

SpreeSunspotSearch

Adds Solr search to Spree using Sunspot.

This is compatible with Spree 1.2. I haven't tested it below that.

Install

I make the assumption that you have a functioning Spree store and are just extending the search capabilities with Sunspot/Solr

Add spree_sunspot_search to your Gemfile and run bundler.

gem 'spree_sunspot', github: 'jbrien/spree_sunspot_search'

add the following to the Gemfile if you are not using another solr install locally for testing and development. The rake tasks for starting and stop this for development are included automatically for your use.

group :test, :development do
  gem 'sunspot_solr'
end

Install the sunspot.yml file from Sunspot and the spree_sunspot and spree_sunspot_filters initializers.

rails g spree:sunspot:install

Running

Start up Solr (bundled with Sunspot's install)

rake sunspot:solr:run

Build the index for the first time

rake sunspot:reindex

Setup and Customize the Index and Filters/Facets

The spree_sunspot.rb initializer is a simple Sunspot definition just as defined by Sunspot. This adds the searchable method to the Spree::Product model.

The meat of this extension is in the spree_sunspot_filters.rb initializers file. Here are all your facets / filtering are setup.

  • search_param - is the field in the index that you want to be treated as a facet.
  • search_condition - is the conjunction / disjunction to use. Acceptable values are :all / :any (At this time your :any is evaluated against the result of :all) as seen below.
condition.all_of do
  filter_all_condition
  condition.any_of do
    filter_any_conditions
  end
end

Value is an array of fixed values for a filter. This is best used with Pricing as seen below.

Ranges are supported in values and translated to Solr's search. Zeros (0) are translated to stars(*) to allow for min/max results.

filters.add do |f|
  f.search_param = 'price'
  f.search_condition = :all
  f.values {[
    0..25,
    26..50,
    51..75,
    76..0
  ]}
end

Performing the Searches within Spree

Setup a Spree Searcher with:

@searcher = Spree::Config.searcher_class.new(params)

Then retrieve the products from Solr:

@products = @searcher.retrieve_products

This returns the Solr results.

please note below is going to be changing and simplified greatly

Sunspot returns a lot more information with the results which have been made available with the solr_search method

@searcher.solr_search

Pagination

Spree Sunspot observes Kaminari's params and Spree's Preferences for per_page

Showing and Navigation through Facets

Facets views based on default index and using HAML for clarity

- @searcher.solr_search.facet(:taxon_ids).rows.each do |row|
  %div
    = link_to( nested_taxons_path( @taxon.permalink, s: { category_ids: row.value } ) ) do
      = row.instance.name
      = row.count

We pull the facets for the results using Sunspot's facet method. Spree Sunspot Search expects a Hash with an 's' key to store the facets to be searched against. Such as:

s: { taxon_ids: 1 }

values can also be an array

s: { taxon_ids: [ 1, 2, 4 ] }

you obviously can have multiple facets too.

s: { taxon_ids: [ 1, 2, 4 ], price: 0..25 }

Sorting

Spree Sunspot Search looks for an :order_by param for sorting. It must have a value relating to an indexed field and direction comma separated.

order_by: 'taxon_ids, asc'

asc and desc are the acceptable directions; ascending and descending respectively.

TODOs

  • Finish Similar Products that is in the works
  • Major code cleanup (due to development has been client driven)
  • Tests
  • Better Conjunction / Disjunction Configs for Spree
  • Inclusion of Failover support for replicated Solr servers
  • Support for Solr 4.x (and subsequent Sunspot contribs)

Testing

bundle
bundle exec rake test_app
bundle exec rspec spec

Inspired and Expanded from My Original Extension and The work begun by five18pm Copyright (c) 2012-13 John Brien Dilts, released under the New BSD License.

spree_sunspot_search's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spree_sunspot_search's Issues

order_by bugs

Hey folks,

I'm not sure what is going on here, but I followed the documentation to order my store's products by price and received the following error:

[21] pry(main)> @searcher = Spree::Config.searcher_class.new({:order_by => 'price, asc'})
  Spree::Preference Load (0.3ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/currency' LIMIT 1
  Spree::Preference Load (0.2ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/products_per_page' LIMIT 1
  Spree::Preference Load (0.2ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/total_similar_products' LIMIT 1
=> #<Spree::Sunspot::Search:0x007fd9a1af4100
 @current_currency="USD",
 @properties=
  {:taxon=>nil,
   :keywords=>nil,
   :search=>nil,
   :per_page=>39,
   :page=>1,
   :filters=>[],
   :order_by=>"price, asc",
   :location_coords=>nil,
   :total_similar_products=>10}>
[22] pry(main)> @searcher.retrieve_products                           (0.6ms)  SELECT COUNT(*) FROM `spree_products` WHERE (`spree_products`.deleted_at IS NULL)
ArgumentError: Unknown sort direction  asc. Acceptable input is: :asc, :ascending, :desc, :descending
from /Users/sent1nel/Apps/store/vendor/bundle/ruby/2.0.0/gems/sunspot-2.0.0/lib/sunspot/query/sort.rb:50:in `direction_for_solr'

I also tried playing around with "price, :asc", "price,:asc" (which obviously doesn't work because of the .split happening somewhere), and "price, desc" / its alternatives to no avail.

Thinking it might just be an issue with the field, I tried sorting my products by name...

[23] pry(main)> @searcher = Spree::Config.searcher_class.new({:order_by => 'name, desc'})
  Spree::Preference Load (0.3ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/currency' LIMIT 1
  Spree::Preference Load (0.2ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/products_per_page' LIMIT 1
  Spree::Preference Load (0.2ms)  SELECT `spree_preferences`.* FROM `spree_preferences` WHERE `spree_preferences`.`key` = 'spree/app_configuration/total_similar_products' LIMIT 1
=> #<Spree::Sunspot::Search:0x007fd99bbdd950
 @current_currency="USD",
 @properties=
  {:taxon=>nil,
   :keywords=>nil,
   :search=>nil,
   :per_page=>39,
   :page=>1,
   :filters=>[],
   :order_by=>"name, desc",
   :location_coords=>nil,
   :total_similar_products=>10}>
[24] pry(main)> @searcher.retrieve_products                        Sunspot::UnrecognizedFieldError: No field configured for Spree::Product with name 'name'
from /Users/sent1nel/Apps/store/vendor/bundle/ruby/2.0.0/gems/sunspot-2.0.0/lib/sunspot/setup.rb:113:in `field'

Am I doing it wrong?

Products not showing under a taxon

Hi,

Generally, when you go to taxon show page, you can see the products related with that taxon. But, since the time I have added spree_sunspot_search, products don't seems to appear on the taxon show page.

From console I see this line which is querying to load products:

SELECT DISTINCT(spree_products.id), spree_products.* FROM spree_products INNER JOIN spree_variants ON spree_variants.product_id = spree_products.id AND spree_variants.is_master = 1 INNER JOIN spree_prices ON spree_prices.variant_id = spree_variants.id INNER JOIN spree_products_taxons ON spree_products_taxons.product_id = spree_products.id INNER JOIN spree_taxons ON spree_taxons.id = spree_products_taxons.taxon_id WHERE spree_products.deleted_at IS NULL AND spree_prices.currency = 'INR' AND spree_taxons.id IN (11) AND (spree_products.available_on <= '2013-05-06 12:49:09') AND (spree_prices.amount IS NOT NULL) AND (spree_products.id = -1)

May be issue is at this condition: spree_products.id = -1 which will ofcourse won't load any product.

Has anybody faced this before? Is there any way to make this work? From all I can see, this issue is coming from this line:
https://github.com/jbrien/spree_sunspot_search/blob/master/lib/spree/sunspot/search.rb#L97

When I commented out the line everything was back to normal. Any pointers to solve this issue?

RSolr::Error::InvalidRubyResponse when reindexing

Hi,

I found your gem and was very excited about it.
But I have a problem to get it running:

I started the solr server with rake sunspot:solr:run and then try to reindex my products with rake sunspot:reindex. After that, I get this error:

*Note: the reindex task will remove your current indexes and start from scratch.
If you have a large dataset, reindexing can take a very long time, possibly weeks.
This is not encouraged if you have anywhere near or over 1 million rows.
Are you sure you want to drop your indexes and completely reindex? (y/n)
y
Skipping progress bar: for progress reporting, add gem 'progress_bar' to your Gemfile
RSolr::Error::InvalidRubyResponse - 200 OK
Error: <result status="1">java.lang.NullPointerException
    at org.apache.solr.handler.XmlUpdateRequestHandler.doLegacyUpdate(XmlUpdateRequestHandler.java:129)
    at org.apache.solr.servlet.SolrUpdateServlet.doPost(SolrUpdateServlet.java:87)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:297)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

Request Data: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>type:Spree\\:\\:Product</query></delete>"

Backtrace: /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:297:in `rescue in evaluate_ruby_response'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:294:in `evaluate_ruby_response'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:269:in `adapt_response'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:175:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:161:in `send_and_receive'
(eval):2:in `post'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:67:in `update'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rsolr-1.0.9/lib/rsolr/client.rb:131:in `delete_by_query'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:57:in `remove_all'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sunspot-2.0.0/lib/sunspot/session.rb:181:in `block in remove_all'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sunspot-2.0.0/lib/sunspot/session.rb:181:in `each'

I just started using Solr, so I'm a little bit lost here. Can someone help me or point me in the right direction where I can start?

Undefined local variable or method `each_hit_with_result'

I installed the plugin rails plugin install command

I started the Solr server also

But when i am opening the spree page it showing the following error

Internal Server Error
undefined local variable or method `each_hit_with_result' for #ActiveRecord::Relation:0xcc1eb3c

Please help me out .I cannot follow the gem installation because of the error mentioned in previous issue

Plugin not working for me

Today i was trying to make your solr search plugin work on fresh spree installation.But when i am getting an errror like this

You have requested:
spree_sunspot_search >= 0

The bundle currently has spree_sunspot_search locked at 0.10b.
Try running bundle update spree_sunspot_search
Run bundle install to install missing gems.

I will explain the sequence of action i did

git clone -b 0-70-stable git://github.com/spree/spree.git s6 [Cloned yo a folder s6]
cd s6
rails new sp6 [included the spree source code path to gem file and bundle installed]
rails g spree:site -A
rake db:bootstrap [Spree installation complete]

Included following in gemfile of sp6
gem 'spree_sunspot_search', git: 'git://github.com/jbrien/spree_sunspot_search.git'
group :test, :development do
gem 'sunspot_solr'
end

Plugin installation complete

Then i run rails generate sunspot_rails:install

After that i am getting the error

You have requested:
spree_sunspot_search >= 0

The bundle currently has spree_sunspot_search locked at 0.10b.
Try running bundle update spree_sunspot_search

Please help me .Tried running bundle update spree_sunspot_search is also .But of no use

Skipping progress bar - no such file to load -- progress_bar

Hello!

With rake sunspot:solr:reindex error:

rake sunspot:solr:reindex --trace
** Invoke sunspot:solr:reindex (first_time)
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sunspot:reindex
Skipping progress bar - no such file to load -- progress_bar
** Execute sunspot:solr:reindex

If add gem 'progress_bar' in Gemfile

rake sunspot:solr:reindex
rake aborted!
no such file to load -- options

Please help me

Ordering not working

The products are ordered by id not by score. The problem is that when you pull the hits out of the db
by id (search.rb line 93) you lose the sorting(at least with sqlite). Is there a reason you are not using Sunspot's results method?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.