Full text Search with Sphinx and PHP

Back to Blog
sphinx php developer mumbai - ezeelive

Full text Search with Sphinx and PHP

Sphinx is an open source search server which combines very fast full-text search with a great scalability. It’s growing up fast and we not only inventing new features for the full-text search itself but also creating new ways to integrate Sphinx into PHP based applications.If you are not yet using Sphinx, we’ll be highlighting some key steps on how to serve Full-Text queries to your PHP based applications from start to finish.

If you are an advanced Sphinx Search user, this talk will describe where one can offload MySQL or standard Database functions to Sphinx. From some specific queries, one can run on the Sphinx to how to increase both performance and scalability of your applications (web-based or otherwise).

Besides tips and tricks centered around integration with PHP/MySQL based applications, you will learn about the recent 2.0.1 release straight from the proverbial ‘horse’s mouth.’ This talk will utilize and focus on real-world examples from installation and integration to handling search results. Sphinx is the acronym for SQL Phrase Index.

Some key features of this search engine are (from official site):

  1. high indexing speed (up to 10 MB/sec on modern CPUs).
  2. high search speed (avg query is under 0.1 sec on 2-4 GB text collections).
  3. high scalability (up to 100 GB of text, up to 100 M documents on a single CPU).
  4. supports distributed searching (since v.0.9.6).
  5. supports MySQL natively (MyISAM and InnoDB tables are both supported).
  6. supports phrase searching.
  7. supports phrase proximity ranking, providing good relevance.
  8. supports English and Russian stemming.
  9. supports any number of document fields (weights can be changed on the fly).
  10. supports document groups.
  11. supports stop words.
  12. supports different search modes (“match all”, “match phrase” and “match any” as of v.0.9.5).
  13. generic XML interface which greatly simplifies custom integration.
  14. pure-PHP (ie. NO module compiling etc) search client API.

1. Install Sphinx

On Linux (Ubuntu/Debian)

sudo apt update
sudo apt install sphinxsearch

On CentOS

sudo yum install sphinx

2. Configure Sphinx

Edit the Sphinx configuration file (/etc/sphinxsearch/sphinx.conf or sphinx.conf in your working directory):

source my_source
{
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass = password
    sql_db = my_database
    sql_port = 3306
    sql_query = SELECT id, title, content FROM articles
}

index my_index
{
    source = my_source
    path = /var/lib/sphinx/my_index
    morphology = stem_en
    min_word_len = 3
}

searchd
{
    listen = 9312
    log = /var/log/sphinx/searchd.log
    query_log = /var/log/sphinx/query.log
    read_timeout = 5
    max_children = 30
    pid_file = /var/run/sphinx/searchd.pid
}

Run Indexing

indexer --all

Start Sphinx Daemon

searchd

3. Install the PHP Client for Sphinx

You can use the SphinxQL API with MySQL or use the php-sphinx extension.

Using MySQL and SphinxQL

Sphinx provides a MySQL-compatible protocol to execute queries.

Using the PHP Sphinx Client

Download the official Sphinx PHP client:

composer require foolz/sphinxql

4. Search in PHP

Using SphinxQL

$mysqli = new mysqli("127.0.0.1", "", "", "", 9312);
$query = "SELECT * FROM my_index WHERE MATCH('search term') LIMIT 10";
$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
    print_r($row);
}

Using the Sphinx PHP Client

require 'vendor/autoload.php';

use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Connection;

$conn = new Connection();
$conn->setParams(['host' => '127.0.0.1', 'port' => 9312]);

$sphinxql = new SphinxQL($conn);
$result = $sphinxql->select('*')
    ->from('my_index')
    ->match('content', 'search term')
    ->limit(10)
    ->execute();

print_r($result);

Watch Online:
//www.ustream.tv/embed/recorded/14937201

Reference Links:

//sphinxsearch.com/

//www.php.net/manual/en/book.sphinx.php

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Blog