From b263d6b9f6d942fcfea9568beff263324b1b1879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com> Date: Wed, 5 Aug 2015 10:47:01 +0300 Subject: [PATCH] reducing search endpoint complexity --- src/Search.php | 283 +++++++++++++------------------------------------ 1 file changed, 74 insertions(+), 209 deletions(-) diff --git a/src/Search.php b/src/Search.php index f63728b..e67cf4a 100644 --- a/src/Search.php +++ b/src/Search.php @@ -12,16 +12,19 @@ namespace ONGR\ElasticsearchDSL; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use ONGR\ElasticsearchDSL\Filter\BoolFilter; use ONGR\ElasticsearchDSL\Highlight\Highlight; +use ONGR\ElasticsearchDSL\Query\BoolQuery; +use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint; +use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface; +use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer; use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer; -use ONGR\ElasticsearchDSL\Sort\AbstractSort; -use ONGR\ElasticsearchDSL\Sort\Sorts; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; /** @@ -108,174 +111,130 @@ class Search } /** - * Adds query to search. - * - * @param BuilderInterface $query - * @param string $boolType - * - * @return int Key of query. - */ - public function addQuery(BuilderInterface $query, $boolType = '') - { - return $this - ->getEndpoint('query') - ->addBuilder($query, ['bool_type' => $boolType]); - } - - /** - * Sets parameters for bool query. + * Returns endpoint instance. * - * @param array $params Example values: - * - minimum_should_match => 1 - * - boost => 1. + * @param string $type Endpoint type. * - * @return $this + * @return SearchEndpointInterface */ - public function setBoolQueryParameters(array $params) + public function getEndpoint($type) { - /** @var QueryEndpoint $endpoint */ - $endpoint = $this->getEndpoint('query'); - $endpoint->setParameters($params); + if (!array_key_exists($type, $this->endpoints)) { + $this->endpoints[$type] = SearchEndpointFactory::get($type); + } - return $this; + return $this->endpoints[$type]; } /** - * Returns contained query. + * Destroys search endpoint. * - * @return BuilderInterface[] + * @param string $type Endpoint type. */ - public function getQuery() + public function destroyEndpoint($type) { - return $this - ->getEndpoint('query') - ->getBuilders(); + unset($this->endpoints[$type]); } /** - * Destroys query part. + * Adds query to the search. + * + * @param BuilderInterface $query + * @param string $boolType + * @param string $key * * @return $this */ - public function destroyQuery() + public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) { - $this->destroyEndpoint('query'); + $endpoint = $this->getEndpoint(QueryEndpoint::NAME); + $endpoint->addToBool($query, $boolType, $key); return $this; } /** - * Adds a filter to search. + * Adds a filter to the search. * * @param BuilderInterface $filter Filter. - * @param string $boolType Possible boolType values: + * @param string $boolType Example boolType values: * - must * - must_not * - should. + * @param string $key * - * @return int Key of query. + * @return $this */ - public function addFilter(BuilderInterface $filter, $boolType = '') + public function addFilter(BuilderInterface $filter, $boolType = BoolFilter::MUST, $key = null) { - return $this - ->getEndpoint('filter') - ->addBuilder($filter, ['bool_type' => $boolType]); - } + $this->getEndpoint(QueryEndpoint::NAME); + $endpoint = $this->getEndpoint(FilterEndpoint::NAME); + $endpoint->addToBool($filter, $boolType, $key); - /** - * Returns currently contained filters. - * - * @return BuilderInterface[] - */ - public function getFilters() - { - return $this - ->getEndpoint('filter') - ->getBuilders(); + return $this; } /** - * Sets bool filter parameters. + * Adds aggregation into search. * - * @param array $params Possible values: - * _cache => true - * false. + * @param AbstractAggregation $aggregation * - * @return $this + * @return $this; */ - public function setBoolFilterParameters($params) + public function addAggregation(AbstractAggregation $aggregation) { - /** @var FilterEndpoint $endpoint */ - $endpoint = $this->getEndpoint('filter'); - $endpoint->setParameters($params); + $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName()); return $this; } - /** - * Destroys filter part. - */ - public function destroyFilters() - { - $this->destroyEndpoint('filter'); - } - /** * Adds a post filter to search. * - * @param BuilderInterface $postFilter Post filter. - * @param string $boolType Possible boolType values: - * - must - * - must_not - * - should. + * @param BuilderInterface $filter Filter. + * @param string $boolType Example boolType values: + * - must + * - must_not + * - should. + * @param string $key * * @return int Key of post filter. */ - public function addPostFilter(BuilderInterface $postFilter, $boolType = '') + public function addPostFilter(BuilderInterface $filter, $boolType = BoolFilter::MUST, $key = null) { - return $this - ->getEndpoint('post_filter') - ->addBuilder($postFilter, ['bool_type' => $boolType]); - } + $this + ->getEndpoint(PostFilterEndpoint::NAME) + ->add($filter, $boolType, $key); - /** - * Returns all contained post filters. - * - * @return BuilderInterface[] - */ - public function getPostFilters() - { - return $this - ->getEndpoint('post_filter') - ->getBuilders(); + return $this; } /** - * Sets bool post filter parameters. + * Adds sort to search. * - * @param array $params Possible values: - * _cache => true - * false. + * @param BuilderInterface $sort * * @return $this */ - public function setBoolPostFilterParameters($params) + public function addSort(BuilderInterface $sort) { - /** @var PostFilterEndpoint $endpoint */ - $endpoint = $this->getEndpoint('filter'); - $endpoint->setParameters($params); + $this->getEndpoint(SortEndpoint::NAME)->add($sort); return $this; } /** - * Returns min score value. + * Allows to highlight search results on one or more fields. * - * @return float + * @param Highlight $highlight + * + * @return int Key of highlight. */ - public function getMinScore() + public function addHighlight($highlight) { - return $this->minScore; + $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight); + + return $this; } /** @@ -293,7 +252,17 @@ class Search } /** - * Paginate reed removedlts from. + * Returns min score value. + * + * @return float + */ + public function getMinScore() + { + return $this->minScore; + } + + /** + * Paginate reed removed lts from. * * @param int $from * @@ -340,32 +309,6 @@ class Search return $this->size; } - /** - * Adds sort to search. - * - * @param AbstractSort $sort - * - * @return int Key of sort. - */ - public function addSort(AbstractSort $sort) - { - return $this - ->getEndpoint('sort') - ->addBuilder($sort); - } - - /** - * Returns sorts object. - * - * @return Sorts - */ - public function getSorts() - { - return $this - ->getEndpoint('sort') - ->getBuilders(); - } - /** * Allows to control how the _source field is returned with every hit. * @@ -438,32 +381,6 @@ class Search return $this->scriptFields; } - /** - * Allows to highlight search results on one or more fields. - * - * @param Highlight $highlight - * - * @return int Key of highlight. - */ - public function setHighlight($highlight) - { - return $this - ->getEndpoint('highlight') - ->addBuilder($highlight); - } - - /** - * Returns containing highlight object. - * - * @return $this - */ - public function getHighlight() - { - return $this - ->getEndpoint('highlight') - ->getBuilders(); - } - /** * Sets explain property in request body search. * @@ -512,32 +429,6 @@ class Search return $this->stats; } - /** - * Adds aggregation into search. - * - * @param AbstractAggregation $aggregation - * - * @return int Key of aggregation. - */ - public function addAggregation(AbstractAggregation $aggregation) - { - return $this - ->getEndpoint('aggregations') - ->addBuilder($aggregation); - } - - /** - * Returns contained aggregations. - * - * @return AbstractAggregation[] - */ - public function getAggregations() - { - return $this - ->getEndpoint('aggregations') - ->getBuilders(); - } - /** * Setter for scroll duration, effectively setting if search is scrolled or not. * @@ -591,7 +482,7 @@ class Search * * Controls which shard replicas to execute the search request on. * - * @param mixed $preferenceParams Possible values: + * @param mixed $preferenceParams Example values: * _primary * _primary_first * _local @@ -668,30 +559,4 @@ class Search return $output; } - - /** - * Returns endpoint instance. - * - * @param string $type Endpoint type. - * - * @return SearchEndpointInterface - */ - private function getEndpoint($type) - { - if (!array_key_exists($type, $this->endpoints)) { - $this->endpoints[$type] = SearchEndpointFactory::get($type); - } - - return $this->endpoints[$type]; - } - - /** - * Destroys search endpoint. - * - * @param string $type Endpoint type. - */ - private function destroyEndpoint($type) - { - unset($this->endpoints[$type]); - } } -- GitLab