: This article mainly introduces the solr operation class and demo of php. if you are interested in the PHP Tutorial, please refer to it.
Php solr operation class and demo
I. solr class
'2017. 0.0.1 ', 'port' => '123');/*** set solr library to * @ param $ core string library name */public static function setCore ($ core) {if ($ core) self: $ options ['path'] = 'solr /'. $ core;}/*** add solr Index * @ param $ fieldArr index Field and value * @ return bool true */public static function addDocument ($ fieldArr = array ()) {$ client = new SolrClient (self: $ options); $ doc = new SolrInputDocument (); foreach ($ fieldArr as $ k => $ v) {$ doc-> addField ($ k, $ v);} $ client-> addDocument ($ doc); $ client-> commit (); return true ;} /* ** delete Index * @ param $ id the primary key id can be an array. it is applied to multiple selections * @ return bool true */public static function delDocument ($ ids) {$ client = new SolrClient (self: $ options); if (is_array ($ ids) $ client-> deleteByIds ($ ids ); else $ client-> deleteById ($ ids); $ client-> commit (); return true ;} /*** query data ** @ param $ qwhere keyword * @ param $ fqwhere additional condition, retrieved by range, applicable to numeric * @ param $ getField query fields * @ param $ sort sorting array ('duration' => 'asc ') asc: Ascending, desc: descending order * @ param $ pageindex query page number * @ param $ pagesize number of entries per page */public static function selectQuery ($ qwhere = array (), $ fqwhere = array (), $ getField = array (), $ sort = array (), $ pageindex = 1, $ pagesize = 20) {$ client = new SolrClient (self: $ options ); $ query = new SolrQuery (); $ sel = ''; foreach ($ qwhere as $ k => $ v) {// $ sel. = '+ '. $ k. ':'. $ v; $ sel = "{$ k }:\" {$ v} \ "" ;}$ query-> setQuery ($ sel ); // keyword search // additional condition, search by range, applicable to numeric if ($ fqwhere) {$ query-> setFacet (true ); foreach ($ fqwhere as $ k =>$ v) $ query-> addFacetQuery ($ v); // $ query-> addFacetQuery ('price: [* TO 500] ');} // query the field if ($ getField) {foreach ($ getField as $ key => $ val) $ query-> addField ($ val);} // sort if ($ sort) {foreach ($ sort as $ k => $ v) {if ($ v = 'asc ') $ query-> addSortField ($ k, SolrQuery: ORDER_ASC); elseif ($ v = 'desc ') $ query-> addSortField ($ k, SolrQuery: ORDER_DESC) ;}// page $ query-> setStart (self: getPageIndex ($ pageindex, $ pagesize )); $ query-> setRows ($ pagesize); $ query_response = $ client-> query ($ query); $ response = $ query_response-> getResponse (); return $ response ;} /*** paging data processing */private static function getPageIndex ($ pageindex, $ pagesize) {if ($ pageindex <= 1) $ pageindex = 0; else $ pageindex = ($ pageindex-1) * $ pagesize; return $ pageindex ;}}
II. operation demo
"Wang jing jie",); print_r (phpSolr: selectQuery ($ qwhere); // add $ fieldArr = array ("id" => 15, "username" => "si sheng chao", "usertype" => 1, "last_update_time" => "2016-01-05T03: 35: 13Z",); phpSolr :: addDocument ($ fieldArr); // delete // phpsolr: delDocument (15 );
The above introduces the solr operation class and demo of php, including some content. For more information, see PHP Chinese website (www.php1.cn )!
Related articles:
Install php-solr extension
How does one install and configure solr + php?
Integrate PHP applications and Solr search engines