Skip to content

Instantly share code, notes, and snippets.

@serihiro
Created August 15, 2012 01:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serihiro/3354516 to your computer and use it in GitHub Desktop.
Save serihiro/3354516 to your computer and use it in GitHub Desktop.
Groonga through HTTP Utility for PHP
<?php
class GroongaUtil{
/*
* This Class is Utility for Groonga with HTTP.
*
* Groonga is an open-source fulltext search engine and column store.
* For detail of Groonga , see following web site.
* http://groonga.org/
*
*/
const orConditionTerm = ' OR ';
const andConditionTerm = '+';
const minusConditionTerm = ' - ';
private $host = '';
private $port = '';
private $table = '';
private $query = '';
private $outputColumns = '';
private $sortby = '';
private $format = '';
public function __construct($host , $port = '10041' ,$format = 'json') {
if($host == null){
return false;
}
$this->host = $host;
$this->port = $port;
$this->format = $format;
}
public function getHost(){
return $this->host;
}
public function getPort(){
return $this->port;
}
public function setTable($table){
$this->table = $table;
}
public function getTable(){
return $this->table;
}
public function setQuery($query){
$this->query = $query;
}
public function getQuery(){
return $this->query;
}
public function getFormat(){
return $this->format;
}
public function setFormat($format){
$this->format = $format;
}
public function getSortby(){
return $this->sortby;
}
public function setSortBy($sortby){
$this->sortby = $sortby;
}
public function getConditionTerm($condition){
$returnConditionTerm = '';
$condition = trim($condition);
switch($condition){
case 'AND':
case 'and':
$returnConditionTerm = self::andConditionTerm;
break;
case 'OR':
case 'or':
$returnConditionTerm = self::orConditionTerm;
break;
case 'MINUS':
case 'minus':
$returnConditionTerm = self::minusConditionTerm;
break;
default:
$returnConditionTerm = self::andConditionTerm;
break;
}
return $returnConditionTerm;
}
/*
* <parameter format>
*
* $query = array(
* 'id' => '1' ,
* 'title' => '@homhom' )
*
* $condition = 'AND' / 'and'
* 'OR' / 'or'
* 'MINUS' / 'minus'
*
*/
public function setCondition($queries , $condition = 'AND'){
if( !is_array($queries) ){
return false;
}
$conditionTerm = $this->getConditionTerm($condition);
if($this->query != ''){
$this->query .= $conditionTerm;
}
foreach($queries as $query){
$this->query .= ( $query . $conditionTerm );
}
$this->query = rtrim( $this->query , $conditionTerm);
}
/*
*
*/
public function resetCondition(){
$this->query = '';
}
/*
* <parameter format>
*
* $Columnss = array('id' , 'key', '_score')
*
*/
public function setOutputColumns($Columns){
if( !is_array($Columns) ){
return false;
}
if($this->outputColumns != ''){
$this->outputColumns .= ',';
}
$this->outputColumns .= implode(',' , $Columns);
}
/*
*
*/
public function resetOutputColumns(){
$this->outputColumns = '';
}
public function getOutputColumns(){
return $this->outputColumns;
}
/*
* @discription Excuse select query.
*/
public function doSelect(){
if($this->table == '' ){
return false;
}
$parameter = '/d/select.' . $this->format . '?table=' . $this->table;
if($this->query != ''){
$parameter .= ( '&query=' . $this->query );
}
if($this->outputColumns != ''){
$parameter .= ( '&output_columns=' . $this->outputColumns );
}
if($this->sortby != ''){
$parameter .= ( '&sortby=' . $this->sortby );
}
$result = $this->doRequest($parameter);
return $result;
}
/*
* @discription Excuse http request with curl.
*/
private function doRequest($parameter){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, 'http://' . $this->host .':' . $this->port . $parameter);
//echo 'http://' . $this->host .':' . $this->port . $parameter . "\n\n";
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
@henbow
Copy link

henbow commented Feb 14, 2013

Hi,

This is great PHP class for groonga but how to send "load" command to insert records to groonga?

Thanks :)

@serihiro
Copy link
Author

serihiro commented Mar 5, 2013

Hi,

Thank you for your response !
But , now this class does not support "load" command , because I created this class when groonga did not support "load" through HTTP.

I will update this class for latest version soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment