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.

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.