Have you ever faced a problem in your application where your search engine is a bit slow, even though your database is huge? Or maybe you wanted to add a facet search field in just a few minutes? If you encounter these problems and need a solution, then you are reading the right article! While it may be impossible to create an entire eBay-like application in its entirety, within an hour you can certainly create search engine and facets. This is where Blacklight comes in.
So what is Blacklight?
An open source Solr user interface discovery platform (what a mouthful!), Blacklight has highly-configurable Ruby on Rails front-end and can be used to enable the searching and browsing of your collections. Using the Apache Solr search engine to search full text and/or metadata, it’s really a powerful tool which helps us to connect Solr into Rails applications. It has a lot of features and extensions, but in this article, we will be focusing purely on faceted search. You can find more information about other features
here.
What is Solr?
Solr is an open source search platform built in Java. It’s a popular search platform for the web because it can index and search multiple sites. It is designed for scalability and fast searching, whose major features are:
- Full-text search
- Faceted search
- Real-time indexing
- Database integration
- Rich documents handling (DOC, PDF)
What’s next?
Now we know something about Blacklight and Solr, let’s create our eBay clone – on which will sell computers! We will be able to perform a full text search then filter results with multiple facet fields, like:
- Condition (new, used)
- Screen Size (13.3 inches, 14 inches etc.)
- Format (buy now, auction)
- Price
- Category
- Brand
- Memory
Step 1 – Check installed Java version
For the first step, you need to have a version of Java already installed. You need to check that you have at least version 8.0:
$ java -version
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)
If you have a different version, you need to update it to Java 8, which you can download from the
official download page.
Step 2 – Install a Solr client
When installing Solr, we recommend choosing version 5.5.0. This is because anything 5.0+ doesn’t require any other Java servers like Tomcat or JBoss. To install it, we used brew package installer:
$ brew update
$ brew install solr55
After the installation process:
$ solr start
Wait up to 30 seconds to see Solr running on port 8983 [/]
Started Solr server on port 8983 (pid=20056). Happy searching!
If you don’t use OS X, you can find a lot of tutorials which show how to install Solr, for example
here.
Your solr admin panel is available at
http://localhost:8983.
Step 3 – Create a new Rails application
Now you need to create a new, empty Rails application. We used Rails 4.2.5. Let’s skip a test unit and set a database to MySQL.
$ rails new ebay-like --skip-test-unit --database=mysql
$ cd ebay-like
$ rake db:create
Step 4 – Create an item scaffold
The scaffold will keep all data. To create it, just run:
$ rails g scaffold Item title category_id:integer description:text condition:integer price:float format:integer brand_id:integer screen_size:float color:integer memory:integer --skip-assets
Running via Spring preloader in process 20573
invoke active_record
create db/migrate/20160619091806_create_items.rb
create app/models/item.rb
…
BrandID and CategoryID will be just an integers for now, which will point to hard-coded categories and brands in the Item class.But we won’t create ActiveRecord associated models. Right now, we are just focusing on Blacklight and SOLR.
Step 5 – Run migrations
Let’s migrate our database:
$ rake db:migrate
== 20160619091806 CreateItems: migrating ======================================
-- create_table(:items)
-> 0.0154s
== 20160619091806 CreateItems: migrated (0.0155s) =============================
Step 6 – Update the Item class
We’ve just created scaffolded Item model. From here, we can create a new item going through the
http://localhost:3000/items/new, but first, let’s add some changes to our Item model. We need to validate all needed fields to be present, enumerate some of them and create two constants:
- one with available screen sizes
- second with memory sizes.
# app/models/item.rb
Step 7 – Update item’s form
Let’s also modify items’ form view:
# app/views/items/_form.html.erb
Step 8 – Install Blacklight
Ok, now you are able to create your items, but you can’t search for them yet! Let’s add the
Blacklight gem!
# Gemfile
Bundle it, install Blacklight and migrate everything:
$ bundle install
$ rails generate blacklight:install
$ rake db:migrate
Step 9 – Create an empty Solr core
You have installed Blacklight in your application. Now you need an empty core, which you can create using:
$ solr create -c ebay_like
In Solr, the term
core is used to refer to a single index and associated transaction log and configuration files (including the solrconfig.xml and Schema files, among others). Basically, it’s a place where we keep our data and search through it.
You can check to see if the command was successful by visiting the newly created core:
http://localhost:8983/solr/#/~cores/ebay_like
Step 10 – Edit solrconfig.xml
Now you need to modify the solrconfig.xml (/usr/local/Cellar/solr55/5.5.0/server/solr/ebay_like) file. You don’t need to use managed schema, as you want to use classic schema.xml. To achieve it, first let’s add these lines: