PHP Class Sphinx\SphinxClient

Show file Open project: gigablah/sphinxphp Class Usage Examples

Public Properties

Property Type Description
$anchor distributed retries delay
$arrayresult stored mbstring encoding
$connerror last warning message
$cutoff max matches to retrieve
$error select-list (attributes or expressions, with optional aliases)
$fieldweights max query time, milliseconds (default is 0, do not limit)
$filters max ID to match (default is 0, which means no limit)
$groupby search filters
$groupdistinct group-by sorting clause (to sort groups in result set with)
$groupfunc group-by attribute name
$groupsort group-by function (to pre-process group-by attribute value with)
$host
$indexweights geographical anchor point
$limit how many records to seek from result-set start (default is 0)
$maxid min ID to match (default is 0, which means no limit)
$maxmatches group-by count-distinct attribute
$maxquerytime ranking mode expression (for SPH_RANK_EXPR)
$mbenc requests array for multi-query
$minid attribute to sort by (default is "")
$mode how many records to return from result-set starting at offset (default is 20)
$offset socket connection
$overrides per-field-name weights
$path searchd port (default is 9312)
$port searchd host (default is "localhost")
$ranker per-index weights
$rankexpr ranking mode (default is SPH_RANK_PROXIMITY_BM25)
$reqs connection error vs remote error flag
$retrycount cutoff to stop searching at (default is 0)
$retrydelay distributed retries count
$select per-query attribute values overrides
$socket socket path
$sort per-field weights (default is 1 for all fields)
$sortby match sorting mode (default is SPH_SORT_RELEVANCE)
$timeout whether $result["matches"] should be a hash or an array
$warning last error message
$weights query matching mode (default is SPH_MATCH_ALL)

Public Methods

Method Description
__construct ( ) Create a new client object and fill defaults
__destruct ( ) Close the socket upon exit
addQuery ( string $query, string $index = '*', string $comment = '' ) : integer Add a query to a multi-query batch. Returns index into results array from runQueries() call
buildExcerpts ( array $docs, string $index, string $words, array $opts = [] ) : array | false Connect to searchd and generate excerpts (snippets) from given documents for a given query
buildKeywords ( string $query, string $index, boolean $hits ) : array | false Extracts keywords from query using tokenizer settings for a given index
close ( ) : boolean Close a persistent connection
create ( ) : SphinxClient Creates a SphinxClient object. Chainable
escapeString ( string $string ) : string Escapes characters that are treated as special operators by the query language parser
fixUint ( integer $value ) : integer Fix broken unsigned int
flushAttributes ( ) : integer Forces searchd to flush pending attribute updates to disk, and blocks until completion
getLastError ( ) : string Get last error message
getLastWarning ( ) : string Get last warning message
isConnectError ( ) : boolean Get last error flag, to tell network connection errors from searchd errors or broken responses
open ( ) : boolean Open a persistent connection
packI64 ( integer | string $v ) : string Pack 64-bit signed
packU64 ( integer | string $v ) : string Pack 64-bit unsigned
query ( string $query, string $index = '*', string $comment = '' ) : array | false Connect to searchd server, run given search query through given indexes, and return the results
resetFilters ( ) : SphinxClient Clear all filters (for multi-queries)
resetGroupBy ( ) : SphinxClient Clear groupby settings (for multi-queries)
resetOverrides ( ) : SphinxClient Clear all attribute value overrides (for multi-queries)
runQueries ( ) : array Connect to searchd, run batch queries, and return an array of results
setArrayResult ( boolean $arrayresult ) : SphinxClient Set resultset format to either hash or array; hash is the default format
setConnectTimeout ( integer $timeout ) : SphinxClient Set server connection timeout
setFieldWeights ( array $weights ) : SphinxClient Bind per-field weights by name
setFilter ( string $attribute, array $values, boolean $exclude = false ) : SphinxClient Set values filter; only match records where $attribute value is in (or not in) the given set
setFilterFloatRange ( string $attribute, float $min, float $max, boolean $exclude = false ) : SphinxClient Set float range filter; only match records if $attribute value between $min and $max (inclusive)
setFilterRange ( string $attribute, integer $min, integer $max, boolean $exclude = false ) : SphinxClient Set range filter; only match records if $attribute value between $min and $max (inclusive)
setGeoAnchor ( string $attrlat, string $attrlong, float $lat, float $long ) : SphinxClient Set up anchor point for geosphere distance calculations. Required to use @geodist in filters and sorting
setGroupBy ( string $attribute, integer $func, string $groupsort = '@group desc' ) : SphinxClient Set grouping attribute and function
setGroupDistinct ( string $attribute ) : SphinxClient Set count-distinct attribute for group-by queries
setIdRange ( integer $min, integer $max ) : SphinxClient Limit the ID range; only match records if document ID is between $min and $max (inclusive)
setIndexWeights ( array $weights ) : SphinxClient Bind per-index weights by name
setLimits ( integer $offset, integer $limit, integer $max, integer $cutoff ) : SphinxClient Set offset and count into result set, optionally set max-matches and cutoff limits
setMatchMode ( integer $mode ) : SphinxClient Set matching mode
setMaxQueryTime ( integer $max ) : SphinxClient Set maximum query time, in milliseconds, per-index. 0 means "do not limit"
setOverride ( string $attrname, integer $attrtype, array $values ) : SphinxClient Set attribute values override. Only one override per attribute
setRankingMode ( integer $ranker, string $rankexpr = '' ) : SphinxClient Set ranking mode
setRetries ( integer $count, integer $delay ) : SphinxClient Set distributed retries count and delay
setSelect ( string $select ) : SphinxClient Set select-list (attributes or expressions), SQL-like syntax
setServer ( string $host, integer $port ) : SphinxClient Set searchd host name and port
setSortMode ( integer $mode, string $sortby = '' ) : SphinxClient Set matches sorting mode
setWeights ( array $weights ) : SphinxClient Bind per-field weights by order
status ( ) : array | false Queries searchd status
unpackI64 ( string $v ) : integer | string Unpack 64-bit signed
unpackU64 ( string $v ) : integer | string Unpack 64-bit unsigned
updateAttributes ( string $index, array $attrs, array $values, boolean $mva = false ) : integer Batch update given attributes in given documents

Private Methods

Method Description
connect ( ) : resource | false Connect to searchd server
getResponse ( resource $fp, string $clientVer ) : string | false Get and check response packet from searchd server
mbPop ( ) Leave mbstring workaround mode
mbPush ( ) Enter mbstring workaround mode, when function overloading is enabled
packFloat ( float $float ) : string Helper to pack floats in network byte order
parseSearchResponse ( string $response, integer $nreqs ) : array Parse and return search query (or queries) response
send ( resource $handle, string $data, integer $length ) : boolean Write message to socket

Method Details

__construct() public method

Create a new client object and fill defaults
public __construct ( )

__destruct() public method

Close the socket upon exit
public __destruct ( )

addQuery() public method

Add a query to a multi-query batch. Returns index into results array from runQueries() call
public addQuery ( string $query, string $index = '*', string $comment = '' ) : integer
$query string
$index string
$comment string
return integer Results array index.

buildExcerpts() public method

Connect to searchd and generate excerpts (snippets) from given documents for a given query
public buildExcerpts ( array $docs, string $index, string $words, array $opts = [] ) : array | false
$docs array array of strings that carry the document contents
$index string name of the index
$words string string that contains the keywords to highlight
$opts array hash which contains additional optional highlighting parameters
return array | false Array of snippets, or false on failure.

buildKeywords() public method

Extracts keywords from query using tokenizer settings for a given index
public buildKeywords ( string $query, string $index, boolean $hits ) : array | false
$query string query to extract keywords from
$index string name of the index to get tokenizing settings and keyword occurrence statistics from
$hits boolean whether keyword occurrence statistics are required
return array | false Array of hashes with per-keyword information, or false on failure.

close() public method

Close a persistent connection
public close ( ) : boolean
return boolean

create() public static method

Creates a SphinxClient object. Chainable
public static create ( ) : SphinxClient
return SphinxClient

escapeString() public method

Escapes characters that are treated as special operators by the query language parser
public escapeString ( string $string ) : string
$string string unescaped string
return string Escaped string.

fixUint() public method

Fix broken unsigned int
public fixUint ( integer $value ) : integer
$value integer
return integer

flushAttributes() public method

Forces searchd to flush pending attribute updates to disk, and blocks until completion
public flushAttributes ( ) : integer
return integer Flush tag, -1 on error.

getLastError() public method

Get last error message
public getLastError ( ) : string
return string

getLastWarning() public method

Get last warning message
public getLastWarning ( ) : string
return string

isConnectError() public method

Get last error flag, to tell network connection errors from searchd errors or broken responses
public isConnectError ( ) : boolean
return boolean

open() public method

Open a persistent connection
public open ( ) : boolean
return boolean

packI64() public method

Pack 64-bit signed
public packI64 ( integer | string $v ) : string
$v integer | string
return string

packU64() public method

Pack 64-bit unsigned
public packU64 ( integer | string $v ) : string
$v integer | string
return string

query() public method

Connect to searchd server, run given search query through given indexes, and return the results
public query ( string $query, string $index = '*', string $comment = '' ) : array | false
$query string query string
$index string index name
$comment string optional comment
return array | false Results array, or false upon error.

resetFilters() public method

Clear all filters (for multi-queries)
public resetFilters ( ) : SphinxClient
return SphinxClient

resetGroupBy() public method

Clear groupby settings (for multi-queries)
public resetGroupBy ( ) : SphinxClient
return SphinxClient

resetOverrides() public method

Clear all attribute value overrides (for multi-queries)
public resetOverrides ( ) : SphinxClient
return SphinxClient

runQueries() public method

Connect to searchd, run batch queries, and return an array of results
public runQueries ( ) : array
return array Result array.

setArrayResult() public method

PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs
public setArrayResult ( boolean $arrayresult ) : SphinxClient
$arrayresult boolean whether to return results as array keyed by ID
return SphinxClient

setConnectTimeout() public method

Set server connection timeout
public setConnectTimeout ( integer $timeout ) : SphinxClient
$timeout integer
return SphinxClient

setFieldWeights() public method

Bind per-field weights by name
public setFieldWeights ( array $weights ) : SphinxClient
$weights array
return SphinxClient

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

setFilterFloatRange() public method

Set float range filter; only match records if $attribute value between $min and $max (inclusive)
public setFilterFloatRange ( string $attribute, float $min, float $max, boolean $exclude = false ) : SphinxClient
$attribute string attribute name
$min float minimum attribute value
$max float maximum attribute value
$exclude boolean whether the filter is exclusive or inclusive
return SphinxClient

setFilterRange() public method

Set range filter; only match records if $attribute value between $min and $max (inclusive)
public setFilterRange ( string $attribute, integer $min, integer $max, boolean $exclude = false ) : SphinxClient
$attribute string attribute name
$min integer minimum attribute value
$max integer maximum attribute value
$exclude boolean whether the filter is exclusive or inclusive
return SphinxClient

setGeoAnchor() public method

Set up anchor point for geosphere distance calculations. Required to use @geodist in filters and sorting
public setGeoAnchor ( string $attrlat, string $attrlong, float $lat, float $long ) : SphinxClient
$attrlat string latitude attribute name
$attrlong string longitude attribute name
$lat float anchor point latitude (in radians)
$long float anchor point longitude (in radians)
return SphinxClient

setGroupBy() public method

Set grouping attribute and function
public setGroupBy ( string $attribute, integer $func, string $groupsort = '@group desc' ) : SphinxClient
$attribute string attribute name
$func integer grouping function
$groupsort string group sorting clause
return SphinxClient

setGroupDistinct() public method

Set count-distinct attribute for group-by queries
public setGroupDistinct ( string $attribute ) : SphinxClient
$attribute string attribute name
return SphinxClient

setIdRange() public method

Limit the ID range; only match records if document ID is between $min and $max (inclusive)
public setIdRange ( integer $min, integer $max ) : SphinxClient
$min integer minimum document ID
$max integer maximum document ID
return SphinxClient

setIndexWeights() public method

Bind per-index weights by name
public setIndexWeights ( array $weights ) : SphinxClient
$weights array
return SphinxClient

setLimits() public method

Set offset and count into result set, optionally set max-matches and cutoff limits
public setLimits ( integer $offset, integer $limit, integer $max, integer $cutoff ) : SphinxClient
$offset integer
$limit integer
$max integer
$cutoff integer
return SphinxClient

setMatchMode() public method

Set matching mode
public setMatchMode ( integer $mode ) : SphinxClient
$mode integer
return SphinxClient

setMaxQueryTime() public method

Set maximum query time, in milliseconds, per-index. 0 means "do not limit"
public setMaxQueryTime ( integer $max ) : SphinxClient
$max integer
return SphinxClient

setOverride() public method

Set attribute values override. Only one override per attribute
public setOverride ( string $attrname, integer $attrtype, array $values ) : SphinxClient
$attrname string attribute name
$attrtype integer attribute type
$values array hash that maps document IDs to attribute values
return SphinxClient

setRankingMode() public method

Set ranking mode
public setRankingMode ( integer $ranker, string $rankexpr = '' ) : SphinxClient
$ranker integer
$rankexpr string
return SphinxClient

setRetries() public method

Set distributed retries count and delay
public setRetries ( integer $count, integer $delay ) : SphinxClient
$count integer
$delay integer
return SphinxClient

setSelect() public method

Set select-list (attributes or expressions), SQL-like syntax
public setSelect ( string $select ) : SphinxClient
$select string select list
return SphinxClient

setServer() public method

Set searchd host name and port
public setServer ( string $host, integer $port ) : SphinxClient
$host string
$port integer
return SphinxClient

setSortMode() public method

Set matches sorting mode
public setSortMode ( integer $mode, string $sortby = '' ) : SphinxClient
$mode integer
$sortby string
return SphinxClient

setWeights() public method

DEPRECATED; use SetFieldWeights() instead
public setWeights ( array $weights ) : SphinxClient
$weights array
return SphinxClient

status() public method

Queries searchd status
public status ( ) : array | false
return array | false Status variable name and value pairs, false on error.

unpackI64() public method

Unpack 64-bit signed
public unpackI64 ( string $v ) : integer | string
$v string
return integer | string

unpackU64() public method

Unpack 64-bit unsigned
public unpackU64 ( string $v ) : integer | string
$v string
return integer | string

updateAttributes() public method

Batch update given attributes in given documents
public updateAttributes ( string $index, array $attrs, array $values, boolean $mva = false ) : integer
$index string search index
$attrs array array of attribute names
$values array hash of arrays of new attribute values keyed by document ID
$mva boolean whether to treat attributes as MVAs
return integer Amount of updated documents (0 or more) on success, -1 on failure

Property Details

$anchor public property

distributed retries delay
public $anchor

$arrayresult public property

stored mbstring encoding
public $arrayresult

$connerror public property

last warning message
public $connerror

$cutoff public property

max matches to retrieve
public $cutoff

$error public property

select-list (attributes or expressions, with optional aliases)
public $error

$fieldweights public property

max query time, milliseconds (default is 0, do not limit)
public $fieldweights

$filters public property

max ID to match (default is 0, which means no limit)
public $filters

$groupby public property

search filters
public $groupby

$groupdistinct public property

group-by sorting clause (to sort groups in result set with)
public $groupdistinct

$groupfunc public property

group-by attribute name
public $groupfunc

$groupsort public property

group-by function (to pre-process group-by attribute value with)
public $groupsort

$host public property

public $host

$indexweights public property

geographical anchor point
public $indexweights

$limit public property

how many records to seek from result-set start (default is 0)
public $limit

$maxid public property

min ID to match (default is 0, which means no limit)
public $maxid

$maxmatches public property

group-by count-distinct attribute
public $maxmatches

$maxquerytime public property

ranking mode expression (for SPH_RANK_EXPR)
public $maxquerytime

$mbenc public property

requests array for multi-query
public $mbenc

$minid public property

attribute to sort by (default is "")
public $minid

$mode public property

how many records to return from result-set starting at offset (default is 20)
public $mode

$offset public property

socket connection
public $offset

$overrides public property

per-field-name weights
public $overrides

$path public property

searchd port (default is 9312)
public $path

$port public property

searchd host (default is "localhost")
public $port

$ranker public property

per-index weights
public $ranker

$rankexpr public property

ranking mode (default is SPH_RANK_PROXIMITY_BM25)
public $rankexpr

$reqs public property

connection error vs remote error flag
public $reqs

$retrycount public property

cutoff to stop searching at (default is 0)
public $retrycount

$retrydelay public property

distributed retries count
public $retrydelay

$select public property

per-query attribute values overrides
public $select

$socket public property

socket path
public $socket

$sort public property

per-field weights (default is 1 for all fields)
public $sort

$sortby public property

match sorting mode (default is SPH_SORT_RELEVANCE)
public $sortby

$timeout public property

whether $result["matches"] should be a hash or an array
public $timeout

$warning public property

last error message
public $warning

$weights public property

query matching mode (default is SPH_MATCH_ALL)
public $weights