Skip to content

Commit

Permalink
Update to sphinx3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
misagh authored Apr 5, 2022
1 parent 2d7689b commit 7084f7a
Showing 1 changed file with 52 additions and 35 deletions.
87 changes: 52 additions & 35 deletions src/SphinxSE.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

class SphinxSE {

private $query;
private $limit;
private $offset;
private $mode;
private $sort;
private $index;
private $select;
private $fieldweights;
private $filter;
private $range;
private $floatrange;
private $groupby;
private $groupsort;
private $host;
private $port;
private $ranker;
private $maxmatches;
private $geoanchor;
protected $query;
protected $limit;
protected $offset;
protected $mode;
protected $sort;
protected $index;
protected $fieldweights;
protected $filter;
protected $range;
protected $floatrange;
protected $groupby;
protected $groupsort;
protected $host;
protected $port;
protected $ranker;
protected $maxmatches;

private $selects = [];

public function __construct($config = [])
{
Expand All @@ -39,6 +39,7 @@ public function __construct($config = [])
*
* @param string $host
* @param integer $port
*
*/
public function setServer($host, $port = 0)
{
Expand All @@ -53,6 +54,7 @@ public function setServer($host, $port = 0)
* @param integer $limit
* @param integer $max
* @param integer $cutoff
*
*/
public function setLimits($offset, $limit, $max = 1000, $cutoff = 0)
{
Expand All @@ -65,6 +67,7 @@ public function setLimits($offset, $limit, $max = 1000, $cutoff = 0)
* Set maximum query time, in milliseconds, per-index. 0 means "do not limit"
*
* @param integer $max
*
*/
public function setMaxQueryTime($max)
{
Expand Down Expand Up @@ -126,30 +129,22 @@ public function setFieldWeights(array $weights)
* Set index to use for search
*
* @param $index
*
*/
public function setIndex($index)
{
$this->index = $index;
}

public function __call($name, $arguments)
{
if (empty($arguments))
{
throw new SphinxSEException('Need to provide field query');
}

$this->fieldQuery($name, $arguments[0]);
}

/**
* Add select statement to the query
*
* @param $select
*
*/
public function setSelect($select)
{
$this->select[] = $select;
$this->selects[] = $select;
}

/**
Expand All @@ -159,6 +154,7 @@ public function setSelect($select)
* @param $value
* @param float $quorum
* @param string $operator
*
*/
public function fieldQuery($fields, $value, $quorum = 0.8, $operator = '/')
{
Expand All @@ -180,6 +176,7 @@ public function fieldQuery($fields, $value, $quorum = 0.8, $operator = '/')
* @param string $attribute attribute name
* @param array $values value set
* @param boolean $exclude exclude results
*
*/
public function setFilter($attribute, array $values, $exclude = false)
{
Expand Down Expand Up @@ -212,6 +209,7 @@ public function setFilterRange($attribute, $min, $max, $exclude = false)
* @param float $min minimum attribute value
* @param float $max maximum attribute value
* @param boolean $exclude exclude results
*
*/
public function setFilterFloatRange($attribute, $min, $max, $exclude = false)
{
Expand All @@ -225,7 +223,6 @@ public function setFilterFloatRange($attribute, $min, $max, $exclude = false)
* @param integer $func grouping function
* @param string $groupsort group sorting clause
*
* @return SphinxClient
*/
public function setGroupBy($attribute, $func, $groupsort = '@group desc')
{
Expand All @@ -238,16 +235,25 @@ public function setGroupBy($attribute, $func, $groupsort = '@group desc')
*
* @param string $attribute attribute name
*
* @return SphinxClient
*/
public function setGroupDistinct($attribute)
{
$this->distinct = $attribute;
}

function setGeoAnchor($attrlat, $attrlong, $lat, $long)
/**
* Set GEO anchor in select query
*
* @param string $attrlat lat attr name
* @param string $attrlong lng attr name
* @param string $lat lat value
* @param string $long lng value
* @param string $alias select alias
*
*/
public function setGeoAnchor($attrlat, $attrlong, $lat, $long, $alias = 'geodist')
{
$this->geoanchor = implode(',', compact('attrlat', 'attrlong', 'lat', 'long'));
$this->setSelect('GEODIST(' . $attrlat . ', ' . $attrlong . ', ' . $lat . ', ' . $long . ') AS ' . $alias);
}

public function toQuery()
Expand All @@ -256,13 +262,13 @@ public function toQuery()

$properties = $reflection->getProperties();

$query = '';
$query = empty($this->selects) ? '' : 'select=' . implode(',', $this->selects) . ';';

foreach ($properties as $property)
{
$element = $this->{$property->name};

if (! empty($element))
if ($property->isProtected() && ! empty($element))
{
if (! is_array($element))
{
Expand Down Expand Up @@ -307,6 +313,7 @@ public function escapeString($string)

/**
* Return only query attribute.
*
* @return mixed
*/
public function getQuery()
Expand All @@ -329,4 +336,14 @@ public function appendQuery($query)
{
$this->query .= str_replace(';', '', $query);
}

public function __call($name, $arguments)
{
if (empty($arguments))
{
throw new SphinxSEException('Need to provide field query');
}

$this->fieldQuery($name, $arguments[0]);
}
}

0 comments on commit 7084f7a

Please sign in to comment.