Query Builder library for working with SphinxQL

One of the most important tasks facing the development of a site is the implementation of full-text search. One of the popular and simpler implementations is using Sphinx. The habr already has articles dedicated to it, but the Query Builder library is not deservedly not mentioned. This is what I will try to fix.

image

Introduction

One of the most important tasks facing the development of a site is the implementation of full-text search. One of the popular and simpler implementations is using Sphinx. The habr already has articles dedicated to it, but the Query Builder library is not deservedly not mentioned. This is what I will try to fix.

This text is a free translation of the article "SphinxQL Query Builder for PHP" .

Why is it worth using?

  • We minimize risks from possible sql injections against your indexes;
  • Makes your queries more readable and easier to maintain;
  • Allows you to create simpler and more flexible queries;
  • It has all the functions implemented in the Sphinx API library and most of the functions used when working with SphinxQL;
  • It has been tested in several versions of SphinxSearch and php.


How to install?

If you are using Composer, then you can install it with the following command:
$ composer install foolz/sphinxql-query-builder --save

Or clone it from the github repository.

Comparison of approaches

Example without using Query Builder:
connect_error) {
throw new Exception('Connection Error: ['.$conn->connect_errno.'] '.$conn->connect_error, $conn->connect_errno);
}
$resource = $conn->query('SELECT * FROM anime_index WHERE MATCH(\'@character Asuka\') AND age BETWEEN 12 AND 19');
$results = array();
while ($row = $resource->fetch_assoc()) {
$results[] = $row;
}
$resource->free_result();
// Теперь, можно вывести массив с полученными результатами
var_dump($results);


And here is a piece of code using Query Builder:
setParams(array('host' => 'localhost', 'port' => 9306));
$query = SphinxQL::create($conn)
->select('*')
->from('anime_index')
->match('character', 'Asuka');
// В отличии от предыдущего примера, вы с лёгкостью сможете изменить запрос
$query->where('age', 'between', array(12, 19));
// Массив с полученными результатами
$result = $query->execute();

Is it not so much clearer and simpler?

To summarize a bit

Although we can write queries using MySQLi, Query Builder allows you to write more understandable and easily supported queries. I think the example above shows this very clearly. You can find more examples and documentation on using the library on the github .

Also popular now: