Skip to content

Commit

Permalink
add queryString to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhafezqo committed Nov 8, 2018
1 parent e398c17 commit fa16af3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/Builders/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -253,6 +275,7 @@ public function resetBuilder()
$this->whereOr = [];
$this->match = [];
$this->simpleQueryString = [];
$this->queryString = [];
$this->query = new BoolQuery();
$this->filter = new BoolQuery();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions tests/Builders/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fa16af3

Please sign in to comment.