Skip to content

Instantly share code, notes, and snippets.

@muayyad-alsadi
Last active August 16, 2019 09:40
Show Gist options
  • Save muayyad-alsadi/5648629 to your computer and use it in GitHub Desktop.
Save muayyad-alsadi/5648629 to your computer and use it in GitHub Desktop.
proposed solr client
<?php
class Http {
protected $_opt = array(
CURLOPT_USERAGENT=>'Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
CURLOPT_HEADER => 0,
CURLOPT_HTTPHEADER=>array('Expect: '),
CURLOPT_FRESH_CONNECT => 0,
CURLOPT_FORBID_REUSE => 0,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 600,
);
protected $_curl=null;
public function __construct($options=array()) {
$this->_curl = curl_init();
$this->_opt = $options + $this->_opt;
}
public function __destruct()
{
if(gettype($this->_curl) == 'resource') curl_close($this->_curl);
}
/**
* Send a POST requst using cURL
* @param string $url to request
* @param array $params values to send
* @param array $options for cURL
* @return info array, result string
*/
public function post($url, $params = array(), $options = array())
{
$options += array(
CURLOPT_POST => 1,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => (is_string($params))?$params:http_build_query($params),
) + $this->_opt;
//curl_reset($this->_curl);
curl_setopt_array($this->_curl, $options);
if( ! $result = curl_exec($this->_curl))
{
return array(array('code'=>'', 'Content-Type'=>'', 'curl-Error'=>curl_error($ch)), '');
}
$info=array(
'code'=>curl_getinfo ($this->_curl, CURLINFO_HTTP_CODE),
'Content-Type'=>curl_getinfo ($this->_curl, CURLINFO_CONTENT_TYPE)
);
//curl_close($this->_curl);
return array($info, $result);
}
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $params values to send
* @param array $options for cURL
* @return string
*/
public function get($url, array $params = array(), array $options = array())
{
$options += array(
CURLOPT_URL => $url. (($params)?((strpos($url, '?') === FALSE ? '?' : ''). http_build_query($params)):''),
) + $this->_opt;
curl_setopt_array($this->_curl, $options);
if( ! $result = curl_exec($this->_curl))
{
return array(array('code'=>'', 'Content-Type'=>'', 'curl-Error'=>curl_error($this->_curl)), '');
}
$info=array(
'code'=>curl_getinfo ($this->_curl, CURLINFO_HTTP_CODE),
'Content-Type'=>curl_getinfo ($this->_curl, CURLINFO_CONTENT_TYPE)
);
return array($info, $result);
}
/**
* call unusual method like PUT, DELETE, PURGE ..etc.
* */
public function __call($method,$args){
$params=shift($args);
$options=shift($args);
$options[CURLOPT_CUSTOMREQUEST]=strtoupper($method);
return $this->post($url, $params, $options);
}
}
<?php
require_once(dirname(__FILE__)."/Http.php");
/**
* LiteSolr a JSON-only solr client
*
* $solr=new LiteSolr('http://localhost:8983/solr/core0/');
* $solr->ping();
* $docs=array();
* $docs[]=array('id'=>'d10', 'title'=>'Ali Ahmad', 'description'=>'Ali autobio');
* $solr->update($docs, array('commit'=>'true')); // see http://wiki.apache.org/solr/UpdateJSON
* $solr->get(array('id'=>'d10')); // see http://wiki.apache.org/solr/RealTimeGet
* $r=$solr->select(array('q'=>'*:*'));
* var_dump($r['response']['docs']);
* // below some shortcuts on update
* $solr->delete(array("id"=>"d10"));
* $solr->delete(array('query'=>'*', 'commitWithin'=>1000)); // commit within 1 second
* $solr->commit();
* $solr->optimize(array("waitSearcher"=>false));
*
**/
class LiteSolr {
public $prefix;
protected $_mapping=array(
'ping'=>array('method'=>'get', 'uri'=>'admin/ping'),
'update'=>array('method'=>'post', 'uri'=>'update'), // in some versions in uri is update/json
);
protected $_http;
public function __construct($prefix='http://localhost:8983/solr/', $mapping=array(), $options=array()) {
$this->prefix=$prefix;
$this->_http=new Http($options);
$this->ping();
}
/**
* commit: a shortcut on update
* params: can be array('waitSearcher'=>true, 'softCommit'=>false, 'expungeDeletes'=>false)
* see http://wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
**/
public function commit($params=null){
if (!$params) $params=new stdClass();
var_dump($params);
return $this->update(array("commit"=>$params));
}
/**
* optimize: a shortcut on update
* @params: can be array('waitSearcher'=>true, 'softCommit'=>false, 'maxSegments'=>1)
**/
public function optimize($params=null){
if (!$params) $params=new stdClass();
return $this->update(array("optimize"=> $params));
}
/**
* delete: a shortcut on update
* @params: can be for example array('query'=>'*', 'commitWithin'=>'1000')
*
**/
public function delete($params){
return $this->update(array('delete'=>$params));
}
public function __call($action, $args){
if (!isset($this->_mapping[$action])) {
$m=array();
} else {
$m=$this->_mapping[$action];
}
$uri=isset($m['uri'])?$m['uri']:str_replace('_', '/', $action);
$method=isset($m['method'])?$m['method']:((count($args)<=2)?'get':'post');
if ($method=='get') {
$params=array_merge(array($uri), $args);
return call_user_func_array( array($this, '_json_get'), $params);
} else {
$params=array_merge(array($method, $uri), $args);
return call_user_func_array( array($this, '_json_custom_method'), $params);
}
}
public function _json_get($action, $params=array(), $options=array()) {
// TODO: we might need add 'ts'=>time() to all get params
// we might also need to add 'indent'=>'true' for debugging
list($info,$content_s)=$this->_http->get($this->prefix.$action, $params+array('wt'=>'json'), $options);
if ($info['code']!=200 || null===($content=json_decode($content_s, 1))) {
var_dump($info);
var_dump($content_s);
throw new Exception('bad response');
}
return $content;
}
public function _json_post($action, $params, $get_params=array(), $options=array()) {
return $this->json_custom_method('post', $action, $params, $get_params, $options);
}
public function _json_custom_method($method, $action, $params, $get_params=array(), $options=array()) {
$options+=array(CURLOPT_HTTPHEADER => array('Content-type: application/json'));
if (!is_string($params)) $params=json_encode($params);
$url=$this->prefix.$action;
$get_params+=array('wt'=>'json');
$url.=(strpos($url, '?') === FALSE ? '?' : '&').http_build_query($get_params);
list($info,$content_s)= call_user_func_array( array($this->_http, $method), array($url, $params, $options));
// TODO: we might need to consider 2xx not just 200
if ($info['code']!=200 || null===($content=json_decode($content_s, 1))) {
var_dump($info);
var_dump($content_s);
throw new Exception('bad response');
}
return $content;
}
}
/*
$solr=new LiteSolr('http://localhost:8983/solr/core0/');
var_dump($solr->ping());
var_dump($solr->update(array(array('id'=>'d10', 'title'=>'Ali Ahmad', 'description'=>'Ali autobio')), array('commit'=>'true')));
$r=$solr->select(array('q'=>'*:*'));
var_dump($r['response']['docs']);
var_dump($solr->get(array('id'=>'d10')));
echo "delete:\n";
//var_dump($solr->delete('d10'));
//var_dump($solr->delete(array('d10')));
var_dump($solr->delete(array('query'=>'*', 'commitWithin'=>100))); // commit within 1 second
echo "commit:\n";
var_dump($solr->commit());
echo "optimize:\n";
var_dump($solr->optimize(array("waitSearcher"=>false)));
echo "get:\n";
var_dump($solr->get(array('id'=>'d10')));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment