Skip to content

Commit

Permalink
add mismatch filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Sallam committed Nov 11, 2018
1 parent fa16af3 commit 45564c3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Builders/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class QueryBuilder implements SearchInRangeContract, SearchContract
/**@var array $match */
protected $match = [];

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

/**
* @var array $simpleQueryString
*/
Expand Down Expand Up @@ -231,6 +234,19 @@ public function match($attribute, $keyword)
return $this;
}

/**
* mismatch words to field
* @param $attribute
* @param $keyword
* @return $this
*/
public function mismatch($attribute, $keyword)
{
$this->mismatch[] = [$attribute, $keyword];

return $this;
}

/**
* SimpleQueryString to Field
* @param $attribute
Expand Down Expand Up @@ -274,6 +290,7 @@ public function resetBuilder()
$this->whereTerms = [];
$this->whereOr = [];
$this->match = [];
$this->mismatch = [];
$this->simpleQueryString = [];
$this->queryString = [];
$this->query = new BoolQuery();
Expand Down Expand Up @@ -340,6 +357,11 @@ public function prepareQuery()
$this->prepareMatchQueries($match);
}

// add mismatcher queries
foreach ($this->mismatch as $mismatch) {
$this->prepareMismatchQueries($mismatch);
}

// add SimpleQueryString
foreach ($this->simpleQueryString as $query) {
$this->prepareSimpleQueryString($query);
Expand Down Expand Up @@ -474,6 +496,19 @@ private function prepareMatchQueries($match)
$this->filter->addFilter($matcher);
}

/**
* prepare mismatch query
* @param $mismatch
*/
private function prepareMismatchQueries($mismatch)
{
list($attribute, $keyword) = array_pad($mismatch, 2, null);

$mismatcher = new Match();
$mismatcher->setField($attribute, $keyword);
$this->filter->addMustNot($mismatcher);
}

/**
* prepare Simple Query String
* @param $query
Expand Down

0 comments on commit 45564c3

Please sign in to comment.