-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic RESTful API with support for searching and aggregate
statistics using a Solr backend
- Loading branch information
1 parent
bfc9cc3
commit 67af132
Showing
23 changed files
with
5,263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<IfModule mod_rewrite.c> | ||
RewriteEngine On | ||
RewriteCond %{REQUEST_URI} !dispatch\.php$ | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteRule .* dispatch.php [L,QSA] | ||
</IfModule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
interface SearchAdapter { | ||
|
||
// Input methods | ||
public function getQuery($exclude); | ||
public function search($query, $start, $rows); | ||
|
||
// Output methods | ||
public function getNumResults(); | ||
public function getSingleResult(); | ||
public function getResults(); | ||
public function getAggregateResult($group); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
// load SolrPhpClient library | ||
require_once './lib/Apache/Solr/Service.php'; | ||
|
||
class SearchAdapterSolr implements SearchAdapter { | ||
|
||
/** | ||
* Config | ||
*/ | ||
private $config = array( | ||
'host' => 'localhost', | ||
'port' => 8983, | ||
'path' => '/solr', | ||
); | ||
|
||
/** | ||
* Members | ||
*/ | ||
private $solr; | ||
private $results; | ||
|
||
/** | ||
* Initialise connection to Solr | ||
*/ | ||
public function __construct() { | ||
|
||
$this->solr = new Apache_Solr_Service( | ||
$this->config['host'], | ||
$this->config['port'], | ||
$this->config['path'] | ||
); | ||
|
||
// Test we can connect to Solr | ||
if (!$this->solr->ping()) { | ||
throw new ResponseException('Could not connect to Solr server', Response::INTERNALSERVERERROR); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Return GET search parameters in Lucene query syntax | ||
*/ | ||
public function getQuery($exclude) { | ||
|
||
$params = array(); | ||
|
||
foreach($_GET as $key => $value) { | ||
|
||
if (in_array($key, $exclude)) { | ||
continue; | ||
} | ||
|
||
$params[] = $key . ':' . $value; | ||
|
||
} | ||
|
||
if (count($params)) { | ||
$query = implode(' AND ', $params); | ||
} else { | ||
$query = '*:*'; | ||
} | ||
|
||
return $query; | ||
|
||
} | ||
|
||
/** | ||
* Execute search | ||
*/ | ||
public function search($query, $start, $rows) { | ||
|
||
try { | ||
$this->results = $this->solr->search($query, $start, $rows); | ||
} catch (Exception $e) { | ||
throw new ResponseException('Solr failed with: "' . $e->getMessage() . '"', Response::INTERNALSERVERERROR); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Get number of search results | ||
*/ | ||
public function getNumResults() { | ||
return (int)$this->results->response->numFound; | ||
} | ||
|
||
/** | ||
* Return a single observation | ||
*/ | ||
public function getSingleResult() { | ||
return $this->getObservation($this->results->response->docs[0]); | ||
} | ||
|
||
/** | ||
* Return a list of observations | ||
*/ | ||
public function getResults() { | ||
|
||
$observations = array(); | ||
foreach ($this->results->response->docs as $doc) { | ||
$observations[] = $this->getObservation($doc); | ||
} | ||
|
||
return array( | ||
'results' => $observations | ||
); | ||
|
||
} | ||
|
||
/** | ||
* Return an aggregate result from a grouped query | ||
*/ | ||
public function getAggregateResult($group) { | ||
|
||
// Check group field exists | ||
if (!isset($this->results->response->docs[0]->$group)) { | ||
throw new ResponseException('Unknown group field: ' . $group, Response::BADREQUEST); | ||
} | ||
|
||
$data = array(); | ||
|
||
// Determine group field type | ||
if ($group == 'value') { | ||
|
||
$type = ObservationResource::GROUP_TYPE_MEASURE; | ||
|
||
} else { | ||
|
||
$type = ObservationResource::GROUP_TYPE_DIMENSION; | ||
$data['results'] = array(); | ||
|
||
} | ||
|
||
foreach ($this->results->response->docs as $doc) { | ||
|
||
switch($type) { | ||
|
||
case ObservationResource::GROUP_TYPE_MEASURE: | ||
$data['value'] += $doc->value; | ||
break; | ||
|
||
case ObservationResource::GROUP_TYPE_DIMENSION: | ||
if (isset($data['results'][$doc->$group])) { | ||
$data['results'][$doc->$group] += $doc->value; | ||
} else { | ||
$data['results'][$doc->$group] = $doc->value; | ||
} | ||
break; | ||
|
||
} | ||
|
||
} | ||
|
||
return $data; | ||
|
||
} | ||
|
||
/** | ||
* Helper method to create an array suitable for json_encode from | ||
* an Apache_Solr_Document object | ||
*/ | ||
private function getObservation($result) { | ||
|
||
$observation = array(); | ||
foreach ($result as $field => $value) { | ||
|
||
// Ignore field names with underscores as these are assumed to | ||
// be fields on a separate object that have been flattened for Solr | ||
// e.g. only return "area" not "area_name", "area_location", etc | ||
if (strpos($field, '_') === false) { | ||
$observation[$field] = $value; | ||
} | ||
|
||
} | ||
|
||
return $observation; | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
// load Tonic library | ||
require_once './lib/tonic/tonic.php'; | ||
|
||
// load Observation resource | ||
require_once './observation.php'; | ||
|
||
// handle request | ||
$request = new Request(); | ||
try { | ||
$resource = $request->loadResource(); | ||
$response = $resource->exec($request); | ||
} catch (ResponseException $e) { | ||
$response = $e->response($request); | ||
} | ||
$response->output(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Copyright (c) 2007-2011, Servigistics, Inc. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
* Neither the name of Servigistics, Inc. nor the names of | ||
its contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. |
Oops, something went wrong.