forked from solariumphp/solarium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved streaming expression result and added graph query for GraphML (
solariumphp#582) Improved streaming expression result and added graph query for GraphML
- Loading branch information
Markus Kalkbrenner
authored
Mar 19, 2018
1 parent
853b566
commit fa38360
Showing
10 changed files
with
358 additions
and
32 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
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,10 @@ | ||
<?php | ||
|
||
namespace Solarium\Exception; | ||
|
||
/** | ||
* StreamException exception for Solarium classes. | ||
*/ | ||
class StreamException extends \UnexpectedValueException implements ExceptionInterface | ||
{ | ||
} |
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,72 @@ | ||
<?php | ||
|
||
namespace Solarium\QueryType\Graph; | ||
|
||
use Solarium\Core\Client\Client; | ||
use Solarium\Core\Query\AbstractQuery; | ||
use Solarium\QueryType\Stream\RequestBuilder; | ||
|
||
/** | ||
* Graph query. | ||
*/ | ||
class Query extends AbstractQuery | ||
{ | ||
/** | ||
* Default options. | ||
* | ||
* @var array | ||
*/ | ||
protected $options = [ | ||
'handler' => 'graph', | ||
'resultclass' => 'Solarium\QueryType\Graph\Result', | ||
]; | ||
|
||
/** | ||
* Get type for this query. | ||
* | ||
* @return string | ||
*/ | ||
public function getType() | ||
{ | ||
return Client::QUERY_GRAPH; | ||
} | ||
|
||
/** | ||
* Get a requestbuilder for this query. | ||
* | ||
* @return RequestBuilder | ||
*/ | ||
public function getRequestBuilder() | ||
{ | ||
return new RequestBuilder(); | ||
} | ||
|
||
/** | ||
* No response parser required since we pass through GraphML. | ||
*/ | ||
public function getResponseParser() | ||
{ | ||
} | ||
|
||
/** | ||
* Set the expression. | ||
* | ||
* @param string $expr | ||
* | ||
* @return self Provides fluent interface | ||
*/ | ||
public function setExpression($expr) | ||
{ | ||
return $this->setOption('expr', $expr); | ||
} | ||
|
||
/** | ||
* Get the expression. | ||
* | ||
* @return string | ||
*/ | ||
public function getExpression() | ||
{ | ||
return $this->getOption('expr'); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace Solarium\QueryType\Graph; | ||
|
||
use Solarium\Core\Query\Result\Result as BaseResult; | ||
|
||
/** | ||
* Graph query result. | ||
*/ | ||
class Result extends BaseResult | ||
{ | ||
/** | ||
* Get Solr status code. | ||
* | ||
* @return int | ||
*/ | ||
public function getStatusCode() | ||
{ | ||
return $this->response->getStatusCode(); | ||
} | ||
|
||
/** | ||
* Get Solr response body. | ||
* | ||
* @return string The response body | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->response->getBody(); | ||
} | ||
|
||
/** | ||
* Get Solr response data in GraphML format. | ||
* | ||
* More expressive convenience method that just call getData(). | ||
* | ||
* @return string GraphML XML document | ||
*/ | ||
public function getGraphML() | ||
{ | ||
return $this->getData(); | ||
} | ||
} |
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
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
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
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,71 @@ | ||
<?php | ||
|
||
namespace Solarium\QueryType\Stream; | ||
|
||
use Solarium\Core\Query\AbstractResponseParser as ResponseParserAbstract; | ||
use Solarium\Core\Query\ResponseParserInterface as ResponseParserInterface; | ||
use Solarium\Exception\RuntimeException; | ||
use Solarium\Exception\StreamException; | ||
use Solarium\QueryType\Select\Result\Result; | ||
|
||
/** | ||
* Parse streaming expression response data. | ||
*/ | ||
class ResponseParser extends ResponseParserAbstract implements ResponseParserInterface | ||
{ | ||
/** | ||
* Get result data for the response. | ||
* | ||
* @param Result $result | ||
* | ||
* @throws RuntimeException | ||
* | ||
* @return array | ||
*/ | ||
public function parse($result) | ||
{ | ||
$data = $result->getData(); | ||
|
||
/* | ||
* @var Query | ||
*/ | ||
$query = $result->getQuery(); | ||
|
||
// create document instances | ||
$documentClass = $query->getOption('documentclass'); | ||
$classes = class_implements($documentClass); | ||
if (!in_array('Solarium\QueryType\Select\Result\DocumentInterface', $classes, true)) { | ||
throw new RuntimeException('The result document class must implement a document interface'); | ||
} | ||
|
||
$documents = []; | ||
if (isset($data['result-set']['docs'])) { | ||
foreach ($data['result-set']['docs'] as $doc) { | ||
$fields = (array) $doc; | ||
if (isset($fields['EXCEPTION'])) { | ||
// Use Solr's exception as message. | ||
throw new StreamException($fields['EXCEPTION']); | ||
} | ||
if (isset($fields['EOF'])) { | ||
// End of stream. | ||
break; | ||
} | ||
$documents[] = new $documentClass($fields); | ||
} | ||
if (!isset($fields['EOF'])) { | ||
throw new StreamException('Streaming expression returned an incomplete result-set.'); | ||
} | ||
$data['responseHeader']['QTime'] = $fields['RESPONSE_TIME']; | ||
$data['responseHeader']['status'] = 0; | ||
} else { | ||
throw new StreamException('Streaming expression did not return a result-set.'); | ||
} | ||
|
||
return $this->addHeaderInfo( | ||
$data, | ||
[ | ||
'documents' => $documents, | ||
] | ||
); | ||
} | ||
} |
Oops, something went wrong.