10
Working With Apache Solr - A Developer’s Insights 06/18/22 Radha R Krishnan, Technology Lead, Alignminds

Working with solr.pptx

Embed Size (px)

Citation preview

Page 1: Working with solr.pptx

Working With Apache Solr - A Developer’s Insights

04/15/23

Radha R Krishnan,Technology Lead, Alignminds Technologies

Page 2: Working with solr.pptx

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.

04/15/23

Page 3: Working with solr.pptx

Introduction – Search EngineSearch 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.Amazon CloudSearch

04/15/23

Page 4: Working with solr.pptx

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 or JSON

• Current, released, stable version of Apache Solr is 3.3.

04/15/23

Page 5: Working with solr.pptx

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 local server).

04/15/23

Page 6: Working with solr.pptx

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": { "numFound": 258, "start": 0, "docs": [ { "id": "1", "field_a": "abcd", "field_b": "xyzz", "field_c": "43234" }, { "id": "2", "field_a": "efgh", "field_b": "xyzz", "field_c": "76545" } ] } }

04/15/23

Page 7: Working with solr.pptx

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();04/15/23

Page 8: Working with solr.pptx

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);

04/15/23

Page 9: Working with solr.pptx

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 implemented using handlers/components like MoreLikeThisHandler or MoreLikeThisComponent.

04/15/23

Page 10: Working with solr.pptx

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 install the Solr to your server, add the Solr-php-client library, and write some small codes to connect your project to Solr.

04/15/23