1. Working With Apache Solr - A Developer’s Insights
06/19/15
Radha R
Krishnan,
Technology Lead,
Alignminds
Technologies
2. Introduction - About Search
Search module is a vital component in today's
web applications.
Its importance as well as user friendliness is
very critical in the growth of business.
Why we really need a search engine? Direct
search in database is not enough?
Small web application - direct database search.
But for applications with huge data, if we do a
direct database search it will be a really heavy
process to perform search.
06/19/15
3. Introduction – Search Engine
Search Engines read the data from database
and keep a local index with that data.
Periodically or when some data update occur,
we update the index
Search is done in the index and matching results
are fetch from the index.
Since the index and the code lie on the same
location, search becomes tremendously fast and
easy
Two most important, mostly discussed, and
widely used Search Engines are,
Apache Solr.06/19/15
4. How Solr Works?
• Apache Solr is
an open source
software.
• Written entirely in
Java and uses
Lucene as the
"engine“.
• Search your data
by submitting
HTTP requests
and receive
responses in
either XML or06/19/15
5. Solr Setup
• Download the latest
version of the Service
from the official site.
Solr is written in Java
so you also need Java
Runtime Environment
to run it.
• $ cd solr-
4.1.0/example/
• $ java -jar start.jar
• Open a web browser
and go to
http://localhost:8983/solr/
(assuming Solr is
installed in your local06/19/15
6. Solr Setup
• Collections in Solr are something similar to database table. You can query it.
Click on the collection and chose “query” from submenu.
• “Request-Handler (qt)” with default value “/select”. Request handlers are sort
of pre-defined queries.
• Next parameter is query and its default value “*:*” selects everything. If you
execute the query, it will select all data from the index. For now since the
index is empty, it will give zero results.
• Insert some data to the index from our database.
• First include the Solr service.php (the config file that reads and writes data
to the Solr Index).
• Now, fetch the current index and save it to a new array say $results_old. So
$results_old->response->docs shall contain all the data in the Solr index (if
there is some data, for now no value will be there).
• Fetch data we want to index from the database and map it to the Solr index
fields. Once all data are mapped, write them to the index in a key->value pair
format.
• select query from the web interface, you will get a result similar to following:
{ "responseHeader": { "status": 0, "QTime": 1, "params": { "indent": "true", "q":
"*:*", "_": "1406616999120", "wt": "json", "rows": "2" } }, "response":
06/19/15
7. Solr in PHP
• Download PHP library called solr-php-client
http://code.google.com/p/solr-php-client/downloads/list .
• Once the library is added along with your project files, go
to the file SolrPhpClient/Apache/Solr/Service.php . This
is the main configuration file.
• To connect your project with Solr, include the
service.php file in your code. Then you will be able to
access the solr object as:
$solr = new Apache_Solr_Service(SOLRHOST,
SOLRPORT, SOLRNAME);
• Now, we need to write our data retrieved from database
to the solr index. First initiallize the Solr document as:
$document = new Apache_Solr_Document();
06/19/15
8. Solr in PHP
• Save each of your data as a key value pair (if multiple tables , using
multidimentional array structure so that, the table names falls on the parent
key value pair.). After adding all your data to the Solr document ,
if(!empty($documents))
{
$solr->addDocuments($documents);
$solr->commit4();
$solr->optimize4();
}
• Now simply to retrieve results from solr,
$results = $solr->search("*",0,0);
• So if you need to make a search in your code to Solr, do
it like :
$results = $solr->search(your_search_query, start_val,
no_of_rows, query_conditions);06/19/15
9. Advantages for Solr
• Multilingual support
• Faceting Feature- categorize your results into
sub-groups. Solr supports Faceting to a minimal
level.
• Auto Suggest Feature- Apache Solr has the
native support for autosuggest feature. It can be
facilitated in many ways using –
NGramFilterFactory, EdgeNGramFilterFactory or
TermsComponent.
• Find Similar Feature - compare products
before making a transaction. It is implemented06/19/15
10. Advantages for Solr
• Did you Mean feature - automatically corrects
the spelling and present you with even the
search result. This feature of presenting the user
with spelling corrected suggestions is called “Did
you mean...” feature. Apache Solr implements
the “Did you mean...” feature with the Spellcheck
search component.
• Cache - as LRUCache and FastLRUCache.
Solr, being open source, it can be extended by
adding your own algorithms.
• Solr is a free service. All you need to do is install06/19/15