Skip to content

Commit

Permalink
Implement an advanced method for searching document (#14)
Browse files Browse the repository at this point in the history
* Provide a custom class SearchParameter that handle all parameter supported by ElasticSearch (only for searching purpose)
* Revert the parameter source of the method searchDocumentin favor of a new method advancedSearchDocument (on this way, there is no breaking change between to minor version)
  • Loading branch information
Nexucis authored Aug 25, 2018
1 parent a48e3ec commit b5eadfc
Show file tree
Hide file tree
Showing 6 changed files with 969 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/php:7.1.8-browsers
- image: circleci/php:7.2.9-apache-stretch-node-browsers
- image: docker.elastic.co/elasticsearch/elasticsearch:6.0.0
working_directory: ~/repo
steps:
Expand Down Expand Up @@ -35,7 +35,7 @@ jobs:

analyze_phpcs:
docker:
- image: circleci/php:7.1.8-browsers
- image: circleci/php:7.2.9-apache-stretch-node-browsers
working_directory: ~/repo
steps:
- checkout
Expand All @@ -59,7 +59,7 @@ jobs:

analyze_phpstan:
docker:
- image: circleci/php:7.1.8-browsers
- image: circleci/php:7.2.9-apache-stretch-node-browsers
working_directory: ~/repo
steps:
- checkout
Expand Down
50 changes: 38 additions & 12 deletions src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elasticsearch\Common\Exceptions\RuntimeException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexAlreadyExistException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexNotFoundException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Parameter\SearchParameter;
use stdClass;

/**
Expand Down Expand Up @@ -441,34 +442,59 @@ public function getAllDocuments($alias, $from = 0, $size = 10)
* @param string $type
* @param int $from the offset from the first result you want to fetch (0 by default)
* @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default)
* @param array|null $source allows you to select what fields to be returned (all by default)
* @return array
* @throws IndexNotFoundException
*/
public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10, $source = null)
public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10)
{
if (!$this->existsAlias($alias)) {
throw new IndexNotFoundException($alias);
$body = null;

if (is_array($query)) {
$body = array(
'query' => $query
);
}

$params = array(
'index' => $alias,
'size' => $size,
'from' => $from,
return $this->advancedSearchDocument(
$alias,
$type,
$body,
(new SearchParameter())
->from($from)
->size($size)
);
}

if (is_array($query)) {
$params['body']['query'] = $query;
/**
* @param $alias [REQUIRED]
* @param string $type
* @param array|null $body
* @param SearchParameter $searchParameter
* @return array
* @throws IndexNotFoundException
*/
public function advancedSearchDocument($alias, $type = null, $body = null, $searchParameter = null)
{
if (!$this->existsAlias($alias)) {
throw new IndexNotFoundException($alias);
}

if (is_array($source)) {
$params['_source'] = $source;
$params = array();

if ($searchParameter !== null) {
$params = $searchParameter->build();
}

$params['index'] = $alias;

if ($type !== null) {
$params['type'] = $type;
}

if (is_array($body)) {
$params['body'] = $body;
}

return $this->client->search($params);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elasticsearch\Common\Exceptions\RuntimeException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexAlreadyExistException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexNotFoundException;
use Nexucis\Elasticsearch\Helper\Nodowntime\Parameter\SearchParameter;

/**
* Class IndexHelperInterface
Expand Down Expand Up @@ -148,6 +149,7 @@ public function getDocument($alias, $type, $id, $refresh = false);
* @param string $alias [REQUIRED]
* @param int $from the offset from the first result you want to fetch (0 by default)
* @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default)
* @throws IndexNotFoundException
* @return array
*/
public function getAllDocuments($alias, $from = 0, $size = 10);
Expand All @@ -158,10 +160,20 @@ public function getAllDocuments($alias, $from = 0, $size = 10);
* @param string $type
* @param int $from the offset from the first result you want to fetch (0 by default)
* @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default)
* @param array|null $source allows you to select what fields to be returned (all by default)
* @throws IndexNotFoundException
* @return array
*/
public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10);

/**
* @param $alias
* @param string $type
* @param array|null $body
* @param SearchParameter $searchParameter
* @return array
* @throws IndexNotFoundException
*/
public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10, $source = null);
public function advancedSearchDocument($alias, $type = null, $body = null, $searchParameter = null);

/**
* @param string $index [REQUIRED] If the alias is associated to an unique index, you can pass an alias rather than an index
Expand Down
Loading

0 comments on commit b5eadfc

Please sign in to comment.