Skip to content

Commit

Permalink
Merge pull request #20 from webstollen/master
Browse files Browse the repository at this point in the history
fix #19
  • Loading branch information
carlosescri authored Sep 6, 2017
2 parents b1d18ab + 7970b32 commit b4873a5
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions src/Search/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Client {
private $queryParameter = 'query';
private $allowedParameters = array('page', 'rpp', 'timeout', 'types',
'filter', 'query_name', 'transformer',
'sort'); // Valid parameters
'sort', 'exclude'); // Valid parameters

/**
* Constructor.
Expand Down Expand Up @@ -190,6 +190,7 @@ public function getOptions() {
* defaults to 10 seconds
* - 'types' => types of index to search at. default: all.
* - 'filter' => filter to apply. ['color'=>['red','blue'], 'price'=>['from'=>33]]
* - 'exclude' => exclude ['color' => ['yellow']]
* - any other param will be sent as a request parameter
* @return DoofinderResults results
*/
Expand All @@ -209,15 +210,23 @@ public function query($query = null, $page = null, $options = array()) {
$params = $this->search_options;

// translate filters
if(!empty($params['filter']))
if (!empty($params['filter']))
{
foreach($params['filter'] as $filterName => $filterValue){
$params['filter'][$filterName] = $this->updateFilter($filterValue);
}
}

// translate excludes
if (!empty($params['exclude']))
{
foreach($params['exclude'] as $excludeName => $excludeValue){
$params['exclude'][$excludeName] = $this->updateFilter($excludeValue);
}
}

// no query? then match all documents
if(!$this->optionExists('query') || !trim($this->search_options['query'])){
if (!$this->optionExists('query') || !trim($this->search_options['query'])){
$params['query_name'] = 'match_all';
}

Expand All @@ -230,6 +239,7 @@ public function query($query = null, $page = null, $options = array()) {
$params['query_name'] = $dfResults->getProperty('query_name');
$params['filter'] = $filter;
}

$dfResults = new Results($this->apiCall('search', $params));
$this->page = $dfResults->getProperty('page');
$this->total = $dfResults->getProperty('total');
Expand Down Expand Up @@ -270,6 +280,49 @@ public function getPage(){
return $this->page;
}

/**
* setExclude
*
* set a exclude for the query
* @param string excludeName the name of the exclude to set
* @param array exclude if simple array, terms exclude assumed
* if 'from', 'to' in keys, range exclude assumed
*/
public function setExclude($excludeName, $exclude){
$this->search_options['exclude'][$excludeName] = (array) $exclude;
}

/**
* getExclude
*
* get conditions for certain exclude
* @param string excludeName
* @return array exclude conditions: - simple array if terms exclude
* - 'from', 'to' assoc array if range f.
* @return false if no exclude definition found
*/
public function getExclude($excludeName){
if (isset($this->search_options['exclude'][$excludeName])) {
return $this->search_options['exclude'][$excludeName];
}

return false;
}

/**
* getExclude
*
* get all excludes and their configs
* @return array assoc array excludeName => excludeConditions
*/
public function getExcludes() {
if (isset($this->search_options['exclude'])) {
return $this->search_options['exclude'];
}

return array();
}

/**
* setFilter
*
Expand Down Expand Up @@ -459,6 +512,8 @@ private function sanitize($params) {
$result[$name] = $this->sanitize($value);
} else if (trim($value)) {
$result[$name] = $value;
} else if($value === 0) {
$result[$name] = $value;
}
}

Expand Down

0 comments on commit b4873a5

Please sign in to comment.