From e398c17038f0d02a82bca8dfe91f30f9062d2399 Mon Sep 17 00:00:00 2001 From: Hafez Date: Thu, 27 Sep 2018 17:17:03 +0200 Subject: [PATCH] add SimpleQueryString in query builder with test cases --- src/Builders/QueryBuilder.php | 38 ++++++++++++++++++++++++++--- tests/Builders/QueryBuilderTest.php | 6 +++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Builders/QueryBuilder.php b/src/Builders/QueryBuilder.php index 61e619e..d148f9a 100644 --- a/src/Builders/QueryBuilder.php +++ b/src/Builders/QueryBuilder.php @@ -3,6 +3,7 @@ namespace ElasticRepository\Builders; use Elastica\Query\Match; +use Elastica\Query\SimpleQueryString; use ElasticRepository\Contracts\SearchContract; use ElasticRepository\Contracts\SearchInRangeContract; use Elastica\Query\BoolQuery; @@ -25,9 +26,7 @@ class QueryBuilder implements SearchInRangeContract, SearchContract */ protected $exist = []; - /** - * @var array $notExists - */ + /** @var array $notExists */ protected $notExists = []; /** @@ -73,6 +72,9 @@ class QueryBuilder implements SearchInRangeContract, SearchContract /**@var array $match */ protected $match = []; + /**@var array $simpleQueryString */ + protected $simpleQueryString = []; + /** * @var BoolQuery */ @@ -221,6 +223,19 @@ public function match($attribute, $keyword) return $this; } + /** + * SimpleQueryString to Field + * @param $attribute + * @param $keyword + * @return $this + */ + public function simpleQueryString($attribute, $keyword) + { + $this->simpleQueryString [] =[$attribute, $keyword]; + + return $this; + } + /** * Reset repository to it's default * @return $this @@ -237,6 +252,7 @@ public function resetBuilder() $this->whereTerms = []; $this->whereOr = []; $this->match = []; + $this->simpleQueryString = []; $this->query = new BoolQuery(); $this->filter = new BoolQuery(); @@ -301,6 +317,11 @@ public function prepareQuery() $this->prepareMatchQueries($match); } + // add SimpleQueryString + foreach ($this->simpleQueryString as $query) { + $this->prepareSimpleQueryString($query); + } + $this->query->addFilter($this->filter); return $this->query; @@ -424,4 +445,15 @@ private function prepareMatchQueries($match) $matcher->setField($attribute, $keyword); $this->filter->addFilter($matcher); } + + /** + * prepare Simple Query String + * @param $query + */ + private function prepareSimpleQueryString($query) + { + list($attribute, $keyword) = array_pad($query, 2, null); + $queryString = new SimpleQueryString($keyword, $attribute); + $this->filter->addFilter($queryString); + } } diff --git a/tests/Builders/QueryBuilderTest.php b/tests/Builders/QueryBuilderTest.php index 1e850f7..5581b24 100644 --- a/tests/Builders/QueryBuilderTest.php +++ b/tests/Builders/QueryBuilderTest.php @@ -91,6 +91,12 @@ public function test_match_should_retrun_obj() $this->assertInstanceOf(QueryBuilder::class, $match); } + public function test_simple_query_string_obj() + { + $simpleQueryString = $this->queryBuilderObj->simpleQueryString('item', 'keyword'); + $this->assertInstanceOf(QueryBuilder::class, $simpleQueryString); + } + public function test_restBuilder_should_return_obj() { $builderObject = $this->queryBuilderObj->resetBuilder();