From fa16af33845a877605ade68bc11bc09a0c69a31e Mon Sep 17 00:00:00 2001 From: Hafez Date: Thu, 8 Nov 2018 11:25:54 +0200 Subject: [PATCH] add queryString to repo --- src/Builders/QueryBuilder.php | 44 ++++++++++++++++++++++++++++- tests/Builders/QueryBuilderTest.php | 6 ++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Builders/QueryBuilder.php b/src/Builders/QueryBuilder.php index d148f9a..bc10cb7 100644 --- a/src/Builders/QueryBuilder.php +++ b/src/Builders/QueryBuilder.php @@ -4,6 +4,7 @@ use Elastica\Query\Match; use Elastica\Query\SimpleQueryString; +use Elastica\Query\QueryString; use ElasticRepository\Contracts\SearchContract; use ElasticRepository\Contracts\SearchInRangeContract; use Elastica\Query\BoolQuery; @@ -72,9 +73,16 @@ class QueryBuilder implements SearchInRangeContract, SearchContract /**@var array $match */ protected $match = []; - /**@var array $simpleQueryString */ + /** + * @var array $simpleQueryString + */ protected $simpleQueryString = []; + /** + * @var array $queryString + */ + protected $queryString = []; + /** * @var BoolQuery */ @@ -236,6 +244,20 @@ public function simpleQueryString($attribute, $keyword) return $this; } + /** + * QueryString to Field + * @param array $attributes + * @param string $defaultOperator + * @param string $keyword + * @return $this + */ + public function queryString(array $attributes, string $defaultOperator = null, string $keyword = null) + { + $this->queryString [] =[$attributes, $defaultOperator, $keyword]; + + return $this; + } + /** * Reset repository to it's default * @return $this @@ -253,6 +275,7 @@ public function resetBuilder() $this->whereOr = []; $this->match = []; $this->simpleQueryString = []; + $this->queryString = []; $this->query = new BoolQuery(); $this->filter = new BoolQuery(); @@ -322,6 +345,11 @@ public function prepareQuery() $this->prepareSimpleQueryString($query); } + // add QueryString + foreach ($this->queryString as $query) { + $this->prepareQueryString($query); + } + $this->query->addFilter($this->filter); return $this->query; @@ -456,4 +484,18 @@ private function prepareSimpleQueryString($query) $queryString = new SimpleQueryString($keyword, $attribute); $this->filter->addFilter($queryString); } + /** + * prepare Simple Query String + * @param $query + */ + private function prepareQueryString($query) + { + list($attributes, $defaultOperator, $keyword) = array_pad($query, 3, null); + $queryString = new QueryString($keyword); + $queryString + ->setFields($attributes) + ->setDefaultOperator($defaultOperator) + ; + $this->filter->addFilter($queryString); + } } diff --git a/tests/Builders/QueryBuilderTest.php b/tests/Builders/QueryBuilderTest.php index 5581b24..18b610f 100644 --- a/tests/Builders/QueryBuilderTest.php +++ b/tests/Builders/QueryBuilderTest.php @@ -97,6 +97,12 @@ public function test_simple_query_string_obj() $this->assertInstanceOf(QueryBuilder::class, $simpleQueryString); } + public function test__query_string_obj() + { + $queryString = $this->queryBuilderObj->queryString([], 'AND', 'keyword'); + $this->assertInstanceOf(QueryBuilder::class, $queryString); + } + public function test_restBuilder_should_return_obj() { $builderObject = $this->queryBuilderObj->resetBuilder();