Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M20-90 add response and mapper for msearch #87

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions src/Lib/Elastic/Builders/MSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,60 @@


use SphereMall\MS\Lib\Elastic\Interfaces\SearchInterface;
use SphereMall\MS\Lib\Elastic\Queries\MultiMatchQuery;
use SphereMall\MS\Lib\Elastic\Queries\MustQuery;

/**
* Class MSearch
*
* @package SphereMall\MS\Lib\Elastic\Builders
*/
class MSearch implements SearchInterface
{
private $builders = [];

/**
* MSearch constructor.
*
* @param array $items
*/
public function __construct(array $items)
{
foreach ($items as $item) {
$this->setItem($item);
}
}

/**
* @param BodyBuilder $builder
*
* @return $this
*/
private function setItem(BodyBuilder $builder)
{
$this->builders[] = $builder;

return $this;
}

/**
* @return array
*/
public function getParams()
{
$result = [];
foreach ($this->builders as $builder) {
if ($filter = $builder->getFilter()) {
$params = $this->getParamsFromFilter($filter);
}

/**@var \SphereMall\MS\Lib\Elastic\Builders\BodyBuilder $builder * */
$result[] = [
"index" => $builder->getIndexes(),
"index" => $params['index'] ?? $builder->getIndexes(),
];

if ($q = $builder->getQuery()) {
$res['query'] = $q;
$res['query'] = array_merge_recursive($q, $params['query'] ?? []);
}

if ($size = $builder->getLimit()) {
Expand All @@ -59,15 +83,47 @@ public function getParams()
}

if ($sort = $builder->getSort()) {
$res
['sort'] = $sort;
$res['sort'] = $sort;
}

$result[] = $res ?? [];
if (isset($res)) {
$result[] = $res;
}
}

return [
'body' => $result,
];
}

/**
* @param $filter
*
* @return array
*/
private function getParamsFromFilter(FilterBuilder $filter)
{
$result = [];
$query = [];
$params = $filter->getParams();

if ($params['entities']) {
$result['index'] = $params['entities'];
}

if (isset($params['keyword'])) {
$query[] = new MultiMatchQuery($params['keyword']['value'], $params['keyword']['fields']);
}

foreach ($params['params'] ?? [] as $param) {
/**@var \SphereMall\MS\Lib\Elastic\Interfaces\ElasticParamBuilderInterface $param**/
$query[] = $param->createFilter();
}

if ($query) {
$result['query'] = (new QueryBuilder())->setMust(new MustQuery($query))->toArray();
}

return $result;
}
}
10 changes: 9 additions & 1 deletion src/Lib/Http/ElasticSearch/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ public function handle(string $method, $body = false, $uriAppend = false, array
}

try {
$response = new Response($client->{$endPoint}($queryParams), $queryParams['body']['size'] ?? 0, $queryParams['body']['from'] ?? 0);
$elasticData = $client->{$endPoint}($queryParams);

if ($endPoint == 'msearch') {
foreach ($elasticData['responses'] as $key => $responseItem) {
$response[] = new Response($responseItem);
}
} else {
$response = new Response($elasticData, $queryParams['body']['size'] ?? 0, $queryParams['body']['from'] ?? 0);
}
} catch (\Exception $ex) {
$error = json_decode($ex->getMessage());
throw new \Exception($error->error->reason ?? $ex->getMessage());
Expand Down
6 changes: 3 additions & 3 deletions src/Lib/Http/ElasticSearch/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Response extends \SphereMall\MS\Lib\Http\Response
{
public function __construct(array $response, $limit = 0, $offset = 0)
{
$this->statusCode = (!$response['timed_out'] ? 200 : 404);
$this->statusCode = $response['status'] ?? 200;
$this->headers = [];

try {
$this->data = $responses ?? $response;
$this->status = !$response['timed_out'] ? 'OK' : 'ERROR';
$this->data = $response;
$this->status = isset($response['error']) ? 'ERROR' : 'OK';
$this->errors = $response['error'] ?? null;
$this->debug = $contents['debug'] ?? null;
$this->version = 1;
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/ElasticSearch/ElasticResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function all()
$handler = new \SphereMall\MS\Lib\Http\ElasticSearch\Request($this->client, $this);
$response = $handler->handle('GET', false, false, [$this->search]);

if (is_array($response)) {
foreach ($response as $responseItem) {
$result[] = $this->make($responseItem, true, new ElasticSearchMaker());
}

return $result ?? [];
}

return $this->make($response, true, new ElasticSearchMaker());
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Lib/Elastic/SimpleFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function testBodyFilter()
)
)->limit(1)->offset(1);

$data = $this->client->elastic()->search($body)->withMeta()->all();
$data = $this->client->elastic()->msearch([$body])->all();
// $data = $this->client->elastic()->search($body)->all();
$r = 1;
}
}