I am giving a simple example of lucen solr search with php.
I am assuming that you already
- setup a lucen solr server which is on http://localhost:8389/solr/
- created a core admin
- created indexing with documet/sql database
- tested solr admin by runing query with GUI interface of solr sever
So let’s start with PHP+HTML part. I will do it with below steps
- Create a html file for search by including jquery + css
- Create a ajax file to getting query response with solr server
List of files
- html
- php
First explaining basic function which i am going to use
Jquery ajax search function
<script type="text/javascript"> $(function() { $("#dd_user_input").autocomplete({ source: "global_search.php?cityId=28", minLength: 1, select: function(event, ui) { /* do whatever you want to do*/ }, html: true, open: function(event, ui) { $(".ui-autocomplete").css("z-index", 1000); } }); }); </script>
Put this script in your html file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>lucen</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script> <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $(function() { $("#dd_user_input").autocomplete({ source: "global_search.php?cityId=28", minLength: 1, select: function(event, ui) { /* do whatever you want to do*/ } html: true, open: function(event, ui) { $(".ui-autocomplete").css("z-index", 1000); } }); }); </script> <style> .cnt_left { border: 1px solid #EEEEEE; padding-right: 20px; width: 660px; height:250px; } .cnt_right { width: 316px; border: 1px solid #EEEEEE; height: 250px; } .floatL { float:left; } .floatR { float:right; } .container{width:1000px;margin:auto;} .home_link{padding:10px;} .home_link a { color: #0D92E1; font: bold 23px arial; text-decoration:underline; } .main_subs{ border:1px solid #0D92E1; background:#F3F3F3; height:180px;} .main_subs p{padding:5px 0 0 5px; color:#0D92E1; font: bold 18px arial,tahoma;} .subs_email{font: normal 12px arial,tahoma; padding: 26px 0 0 35px;} .in_subs{padding:5px 0 0 5px; color:#0D92E1; font: bold 18px arial,tahoma;} .in_subs input{padding:5px 0 0 5px; color:#ccc; font: normal 15px arial,tahoma;} </style> </head> <body> <div class="container"> <div class="cnt_left floatL"> <div style="border: 1px solid #EEEEEE;"></div> <form onsubmit="return false;"> <input id="dd_user_input" type="text" class="search_form" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';" value="Type your Input Here"/> </form> </div> </div> </div> </body> </html>
Now comes on PHP part
Our script calling global_search.php file in which we have all solr code as below
<?php error_reporting(E_ALL); define("SOLR_SERVER_HOSTNAME", "localhost"); define("SOLR_SERVER_USERNAME", "admin"); define("SOLR_SERVER_PASSWORD", ""); define("SOLR_SERVER_PORT", "8389"); define('SOLR_PATH', '/solr/myproject); define('RESPONSE_TYPE',"json"); $options = array ( 'hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_PATH, 'wt' => RESPONSE_TYPE, ); $client = new SolrClient($options); $query = new SolrQuery(); $display_json = array(); $json_arr = array(); $serachTerm = trim($_REQUEST['term']); $query->setQuery($serachTerm); $query->addField('entryid'); $query->addField('first_name'); $query->addField('middle_name'); $query->addField('last_name'); $query->addField('zipcode'); $query_response = $client->query($query); $response = $query_response->getResponse(); foreach ($response->response->docs as $doc) { if($doc->first_name[0]!=''){ $json_arr["id"] = $doc->entryid[0]; $json_arr["value"] = $doc->first_name[0]." ".$doc->middle_name[0]." ".$doc->last_name[0]; $json_arr["label"] = $doc->first_name[0]." ".$doc->middle_name[0]." ".$doc->last_name[0]; array_push($display_json, $json_arr); } } $jsonWrite = json_encode($display_json); //encode that search data print $jsonWrite; ?>
Let’s check one by one
define("SOLR_SERVER_HOSTNAME", "localhost"); define("SOLR_SERVER_USERNAME", "admin"); define("SOLR_SERVER_PASSWORD", ""); define("SOLR_SERVER_PORT", "8389"); define('SOLR_PATH', '/solr/myproject); define('RESPONSE_TYPE',"json");
in above code we are defining solr server configuration like hostname, user name, password, port on solr server running, your project path if you have multiple project on one solr server and response type json,xml,phparray.
creating an object to access solr functionality
$client = new SolrClient($options); $query = new SolrQuery();
Adding our query field which we are getting by typing on search box
$serachTerm = trim($_REQUEST['term']); $query->setQuery($serachTerm);
Adding field which we want to get in our result set:(not in search, default search in all indexing files)
$query->addField('entryid'); $query->addField('first_name'); $query->addField('middle_name'); $query->addField('last_name'); $query->addField('zipcode');
If you want to add a filter, like you want to search “rajeev” in all field with particular zip code combination then you can use filter as below:
$query->addFilterQuery(''zipcode':12345);
it will search rajeev which have zip code 12345.
Sending query and getting response.
$query_response = $client->query($query); $response = $query_response->getResponse();
That’s it. For more help you can visit below url:-
http://docs.php.net/manual/en/book.solr.php
https://code.google.com/p/solr-php-client/w/list
- Jquery webcam plugin - June 19, 2016
- How To Add and Delete Users on a CentOSServer - June 5, 2016
- How To Set Up vsftpd on CentOS 6 - June 5, 2016