Skip to content

Commit

Permalink
add SimpleQueryString in query builder with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhafezqo committed Sep 27, 2018
1 parent 9e9e2cd commit e398c17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Builders/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +26,7 @@ class QueryBuilder implements SearchInRangeContract, SearchContract
*/
protected $exist = [];

/**
* @var array $notExists
*/
/** @var array $notExists */
protected $notExists = [];

/**
Expand Down Expand Up @@ -73,6 +72,9 @@ class QueryBuilder implements SearchInRangeContract, SearchContract
/**@var array $match */
protected $match = [];

/**@var array $simpleQueryString */
protected $simpleQueryString = [];

/**
* @var BoolQuery
*/
Expand Down Expand Up @@ -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
Expand All @@ -237,6 +252,7 @@ public function resetBuilder()
$this->whereTerms = [];
$this->whereOr = [];
$this->match = [];
$this->simpleQueryString = [];
$this->query = new BoolQuery();
$this->filter = new BoolQuery();

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

0 comments on commit e398c17

Please sign in to comment.