.
*
* For more information, contact Colosa Inc, 5304 Ventura Drive,
* Delray Beach, FL, 33484, USA, or email info@colosa.com.
*
*/
/**
* Interface to the Solr Search server
* @author Herbert Saal Gutierrez
*
*/
class BpmnEngine_SearchIndexAccess_Solr
{
const SOLR_VERSION = '&version=2.2';
private $_solrIsEnabled = false;
private $_solrHost = "";
public function __construct($solrIsEnabled = false, $solrHost = "")
{
// use the parameters to initialize class
$this->_solrIsEnabled = $solrIsEnabled;
$this->_solrHost = $solrHost;
}
/**
* Verify if the Solr service is available
* @gearman = false
* @rest = false
* @background = false
*
* @return bool
*/
public function isEnabled()
{
// verify solr server response
return $this->_solrIsEnabled;
}
/**
* Returns the total number of indexed documents
* @gearman = false
* @rest = false
* @background = false
*
* @param
* workspace: workspace name
* @return total
*/
public function getNumberDocuments($workspace)
{
if (! $this->_solrIsEnabled)
return;
// get configuration information in base to workspace parameter
// get total number of documents in registry
$solrIntruct = (substr ($this->_solrHost, - 1) == "/") ? $this->_solrHost : $this->_solrHost . "/";
$solrIntruct .= $workspace;
$solrIntruct .= "/select/?q=*:*";
$solrIntruct .= self::SOLR_VERSION;
$solrIntruct .= "&start=0&rows=0&echoParams=none&wt=json";
$handlerTotal = curl_init ($solrIntruct);
curl_setopt ($handlerTotal, CURLOPT_RETURNTRANSFER, true);
$responseTotal = curl_exec ($handlerTotal);
curl_close ($handlerTotal);
// verify the result of solr
$responseSolrTotal = G::json_decode ($responseTotal);
if ($responseSolrTotal->responseHeader->status != 0) {
throw new Exception ("Error returning the total number of documents in Solr." . $solrIntruct);
}
$numTotalDocs = $responseSolrTotal->response->numFound;
return $numTotalDocs;
}
/**
* Execute a query in base to Requested data
* @gearman = false
* @rest = false
* @background = false
*
* @return solr response
*/
public function executeQuery($solrRequestData)
{
if (! $this->_solrIsEnabled)
return;
$solrIntruct = '';
// get configuration information in base to workspace parameter
$workspace = $solrRequestData->workspace;
// format request
$query = empty ($solrRequestData->searchText) ? '*:*' : $solrRequestData->searchText;
$query = rawurlencode ($query);
$start = '&start=' . $solrRequestData->startAfter;
$rows = '&rows=' . $solrRequestData->pageSize;
$fieldList = '';
$cols = $solrRequestData->includeCols;
if (! empty ($cols)) {
$fieldList = "&fl=" . implode (",", $cols);
}
$sort = '';
if ($solrRequestData->numSortingCols > 0) {
$sort = '&sort=';
for ($i = 0; $i < $solrRequestData->numSortingCols; $i ++) {
$sort .= $solrRequestData->sortCols [$i] . "%20" . $solrRequestData->sortDir [$i] . ",";
}
$sort = substr_replace ($sort, "", - 1);
}
$resultFormat = empty ($solrRequestData->resultFormat) ? '' : '&wt=' . $solrRequestData->resultFormat;
$filters = '';
$aFilters = explode (',', $solrRequestData->filterText);
foreach ($aFilters as $value) {
$filters .= '&fq=' . urlencode ($value);
}
$solrIntruct = (substr ($this->_solrHost, - 1) == "/") ? $this->_solrHost : $this->_solrHost . "/";
$solrIntruct .= $workspace;
$solrIntruct .= "/select/?q=$query";
$solrIntruct .= "&echoParams=none";
$solrIntruct .= self::SOLR_VERSION;
$solrIntruct .= $start;
$solrIntruct .= $rows;
$solrIntruct .= $fieldList;
$solrIntruct .= $sort;
$solrIntruct .= $filters;
$solrIntruct .= $resultFormat;
// send query
// search the cases in base to datatable parameters
$handler = curl_init ($solrIntruct);
curl_setopt ($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($handler);
curl_close ($handler);
// decode
$responseSolr = G::json_decode ($response);
if ($responseSolr->responseHeader->status != 0) {
throw new Exception ("Error executing query to Solr." . $solrIntruct);
}
return $responseSolr;
}
/**
* Insert or Update document index
* @gearman = false
* @rest = false
* @background = false
*
* @return solr response
*/
public function updateDocument($solrUpdateDocument)
{
if (! $this->_solrIsEnabled)
return;
$solrIntruct = '';
// get configuration information in base to workspace parameter
$solrIntruct = (substr ($this->_solrHost, - 1) == "/") ? $this->_solrHost : $this->_solrHost . "/";
$solrIntruct .= $solrUpdateDocument->workspace;
$solrIntruct .= "/update";
$handler = curl_init ($solrIntruct);
curl_setopt ($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($handler, CURLOPT_HTTPHEADER, array (
'Content-type:application/xml'
)); // -H
curl_setopt ($handler, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt ($handler, CURLOPT_POSTFIELDS, $solrUpdateDocument->document); // data
$response = curl_exec ($handler);
curl_close ($handler);
$swOk = strpos ($response, '
";
$resultFormat = '&wt=json';
$solrIntruct = (substr ($this->_solrHost, - 1) == "/") ? $this->_solrHost : $this->_solrHost . "/";
$solrIntruct .= $workspace;
$solrIntruct .= "/select/?q=$query";
$solrIntruct .= "&echoParams=none";
$solrIntruct .= self::SOLR_VERSION;
$solrIntruct .= $start;
$solrIntruct .= $rows;
$solrIntruct .= $facets;
$solrIntruct .= $filters;
$solrIntruct .= $resultFormat;
// send query
// search the cases in base to datatable parameters
$handler = curl_init ($solrIntruct);
curl_setopt ($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($handler);
curl_close ($handler);
// decode
$responseSolr = G::json_decode ($response);
if ($responseSolr->responseHeader->status != 0) {
throw new Exception ("Error getting faceted list from Solr." . $solrIntruct);
}
return $responseSolr;
}
}