Sphinx\SphinxClient::setFilter PHP Method

setFilter() public method

Set values filter; only match records where $attribute value is in (or not in) the given set
public setFilter ( string $attribute, array $values, boolean $exclude = false ) : SphinxClient
$attribute string attribute name
$values array value set
$exclude boolean whether the filter is exclusive or inclusive
return SphinxClient
    public function setFilter($attribute, array $values, $exclude = false)
    {
        if (!is_string($attribute)) {
            throw new \InvalidArgumentException('Attribute name must be a string.');
        }
        if (!count($values)) {
            throw new \InvalidArgumentException('Values array must not be empty.');
        }
        foreach ($values as $value) {
            if (!is_numeric($value)) {
                throw new \InvalidArgumentException('Value must be numeric.');
            }
        }
        $exclude = (bool) $exclude;
        $this->filters[] = array('type' => self::SPH_FILTER_VALUES, 'attr' => $attribute, 'exclude' => $exclude, 'values' => $values);
        return $this;
    }

Usage Example

Example #1
0
 public function testFilterExclude()
 {
     $sphinx = new SphinxClient();
     $sphinx->setFilter('attr1', array(1, 2, 3), true);
     $results = $sphinx->query('bb');
     $this->assertEquals($results['total'], 2);
 }
All Usage Examples Of Sphinx\SphinxClient::setFilter