Download the PHP package ripaclub/sphinxsearch without Composer
On this page you can find all versions of the php package ripaclub/sphinxsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ripaclub/sphinxsearch
More information about ripaclub/sphinxsearch
Files in ripaclub/sphinxsearch
Package sphinxsearch
Short Description Sphinx Search library provides SphinxQL indexing and searching features
License BSD-2-Clause
Homepage https://github.com/ripaclub/sphinxsearch
Informations about the package sphinxsearch
Sphinx Search
Sphinx Search library provides SphinxQL indexing and searching features.
- Introduction
- Installation
- Configuration (simple)
- Usage
- Search
- Indexer
- Advanced
- Adapter Service Factory
- Prepared statement
- Working with types
- SQL Objects
- Query expression
- Testing
- Code quality
Introduction
This Library aims to provide:
- A SphinxQL query builder based upon Zend\Db\Sql
- A simple Search class
- An Indexer class to work with RT indices
- Factories for SphinxQL connection through Zend\Db\Adapter
We have also prepared a set of related useful tools. You can use them in conjuction with this library.
-
ripaclub/zf2-sphinxsearch-tool
A set of tools for SphinxSearch's config files creation and automation
-
A module for fast bootstrapping and integration of SphinxSearch library with Zend Framework
-
A bundle for fast bootstrapping and integration of SphinxSearch library with Symfony
-
SphinxSearch docker image (tags for various SphinxSearch's releases and betas)
Note
This library does not use SphinxClient
PHP extension because everything available through the Sphinx API is also available via SphinxQL but not vice versa (i.e., writing to RT indicies is only available via SphinxQL).
Installation
Using composer:
Add the following to your composer.json
file:
Note
Since version 0.8.1, PHP 7 and Zend Framework's components of 3.x series are fully supported.
Starting from 0.8.x series the minimum requirements are PHP >= 5.5 and Zend Framework dependencies >= 2.4.
When forced to use a PHP version less (or equal) than 5.4 and/or a Zend Framework dependencies less (or equal) then 2.3 you can use 0.7.1 version.
Configuration (simple)
In order to work with library components you need an adapter instance. You can simply obtain configured adapter by using the built-in factory like the following example:
Note
Only two drivers are supported:
pdo_mysql
mysqli
For more details see the Adapter Service Factory section.
Usage
Search
Assuming $adapter
has been retrivied via ServiceManager
:
The search()
method takes as first argument the index name (or an array of indicies) and the second one accepts a where condition (same as Zend\Db\Sql\Select::where()
).
Furthermore search()
second argument can accept a closure, which in turn, will be passed the current Select
object that is being used to build the SELECT
query.
The following usage is possible:
The SphinxSearch\Db\Sql\Select
class (like Zend\Db\Sql\Select
which we extend from) supports the following methods related to SQL standard clauses:
Thus it adds some SphinxQL specific methods:
Other utility methods like setSpecifications
, getRawState
and reset
are fully supported.
Instead quantifier
, join
and combine
are just ignored because SphinxQL syntax doesn't have them.
Indexer
Assuming $adapter
has been retrivied via ServiceManager
we can perform indexing of documents, provided that the indices on which we act are real time.
Note that third parameter of insert
method is a boolean flag indicating wheter a "upsert" rather than an insert have to be done.
Furthermore, an Indexer
instance allows to update and delete rows from real time indices (using the methods update
and delete
, respectively).
Advanced
Adapter Service Factory
This library come with two factories in bundle in order to properly configure the Zend\Db\Adapter\Adapter
to work with Sphinx Search.
Use SphinxSearch\Db\Adapter\AdapterServiceFactory
(see Configuration section above) for a single connection else if you need to use multiple connections use the shipped SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory
registering it in the ServiceManager
as below:
For the abstract factory configuration refer to Zend Db Adpater Abstract Factory documentation.
Prepared statement
SphinxQL does not support prepared statement, but PDO drivers are able to emulate prepared statement client side. To achive prepared query benefits this library fully supports this feature.
Note
The PDO driver supports prepared and non-prepared queries. The Mysqli
driver does not support prepared queries.
For both SphinxSearch\Search
and SphinxSearch\Indexer
you can choose the working mode via setQueryMode()
using one of the following flags:
With the auto
option the component will use the best execution mode available, prefering prepared mode if supported by the driver.
Working with types
This library aims to normalize API usage among supported drivers and modes, but due to SphinxQL limitations there are some considerations:
-
NULL
Not supported by SphinxQL. The library transparently handle it for SQL compatibility: an exception will be thrown by the driver if you try to use a value =
NULL
. -
boolean
SphinxQL does not have a native boolean type but if you try to use a PHP
bool
the library and the driver will cast the value to0
or1
respectively. -
integer
PHP native integers work properly when SphinxQL expects an
uint
. Note that strings containing integers do not work in filters (i.e.WHERE
clause).
WARNING: PHP integers are signed, instead SphinxQL supports only UNSIGNED integers and UNIX timestamp. -
float
Due to SphinxQL specific issues related to
float
values (especially inWHERE
clause), by default them are converted to a 32-bit-single-precision compatible string rappresentation which are then included into the SQL query as literals, even in the case where prepared statements are used.This feature works only if value is a native PHP
float
(anyway strings containing floats do not work within Sphinx). If it is needed, this behaviour can be globally disabled using$adapter->getPlatform()->enableFloatConversion(false)
.WARNING: disabling float conversion feature can produce unexpected behaviors, some notable examples:
- Actually Sphinx SQL interpreter treats a number without decimal part as an integer. So, assumming
f1
as float column, if you tryWHERE f1 = 10
you will get42000 - 1064 - index foo: unsupported filter type 'intvalues' on float column
else if you tryWHERE f1 = 10.0
it will work fine. - Due to the fact that SphinxQL does not support float quoted as strings and PDO driver has no way to bind a double (SQL float) parameter in prepared statement mode, PDO driver will just cast to string producing a locale aware conversion (same as PHP
echo
), so it will work only ifLC_NUMERIC
setting is compliant with point as separator in decimal notation (for example you can useLC_NUMERIC='C'
)
- Actually Sphinx SQL interpreter treats a number without decimal part as an integer. So, assumming
For those reasons we suggest to always use proper PHP native types (i.e., not use strings for numeric fields) when building queries.
Useful link: Sphinx Attributes Docs.
SQL Objects
As Zend\Db\Sql this library provides a set of SQL objects:
SphinxSearch\Db\Sql\Select
explained in Search paragraphSphinxSearch\Db\Sql\Insert
SphinxSearch\Db\Sql\Replace
same as insert, but overwrites duplicate IDsSphinxSearch\Db\Sql\Update
with the ability to handleOPTION
clauseSphinxSearch\Db\Sql\Delete
SphinxSearch\Db\Sql\Show
Each of them can be retrivied by SphinxSearch\Db\Sql\Sql
class methods:
Or can be instanziated directly like in the following example:
Then you can perform your query by:
Or using the Search
or the Indexer
components:
Thus, every object (that has where()
) supports the Match
expression, as explained in next paragrah.
Query expression
The SphinxSearch\Query\QueryExpression
class provides a placeholder expression way and a string excape mechanism in order to use safely the Sphinx query syntax.
Also, the component design permits to use it standalone, since it has no dependencies on other library's components.
Some examples:
The SphinxSearch\Db\Sql\Predicate\Match
class uses internally the QueryExpression
, so you can use it in your SQL queries directly:
Testing
The library source code (on master) is 100% covered by unit tests.
Once installed development dependencies through composer you can run phpunit
.
To run also our integration tests execute:
Note
To execute integration tests you need a running instance of SphinxSearch (e.g., using a correctly configured docker image).
All versions of sphinxsearch with dependencies
container-interop/container-interop Version ~1.0
zendframework/zend-db Version ^2.4 || ^3.0
zendframework/zend-servicemanager Version ^2.4 || ^3.0