Php+sphinx+mysql Development Search Engine Detailed example explanation

Source: Internet
Author: User
We hear the search engine will feel very difficult to write, a little ideas are not, in fact, PHP can also do search engine development, but PHP needs to combine Sphinx and MySQL can develop the search engine we want to understand how PHP is the search engine development! Take a look at it!!

First we download sphinx This tool, download address official website Download address:, Find,64 bit download this, extract to our PHP run directory below, for the next page to see the results convenient.

Sphinx Introduction:

Sphinx is an abbreviation for SQL Phrase index (query phrase index), Sphinx is a SQL-based full-text search engine that provides API interfaces including: Php,python,perl,ruby,java, etc. At the same time for MySQL also designed an engine plug-in sphinxse, it is a distributed full-text retrieval system.
High-speed indexing to reach 10m/s
High-performance search (on 2-4g text data, the average response time is less than 0.1 seconds per retrieval)
Handles massive amounts of data (currently known to handle 100G of text data and 100M of documents on a single CPU system)
Provides excellent correlation algorithm, based on the phrase similarity and statistical BM2 method of compound ranking
Support Distributed Search
Provides document fragment generation capabilities
Search services available as a MySQL storage engine
Support multiple retrieval modes such as Boolean, phrase, word similarity, etc.
Must have a primary key
Primary key must be an integer
Not responsible for data storage
Configuration not flexible

The Sphinx structure after decompression:

The following is to carry out our related configuration, see the diagram of the, copied to our Bin directory under the convenience of a change in the name of Sphinx.conf,

Modify the content inside:

SOURCE src1{type= mysqlsql_host= localhost #主机地址sql_user = root# account sql_pass=     #密码sql_db = Sphinx  #数据库sql_port = 3306# Database port 3306sql_query= SELECT ID, name, age from Users #查询语句sql_attr_uint = group_idsql_attr_timestamp= Date_addedsql_ Query_pre = set names UTF8   #数据库编码}index test1{source= src1path= d:/myapaphe/www/sphinx/data #这个一定要配置charset_type = Utf-8 #指定编码ngram_len = 1        #要找中文需指定为1. Ngram_chars = u+3000..u+2fa1f}indexer{mem_limit= 128m}searchd{listen= 9312listen= 9306:mysql41log= D:\myapaphe\www\sphinx\log\searchd.log  #进程日志query_log = D:\myapaphe\www\sphinx\ Log\query.log    #查询日志read_timeout = 5max_children= 30pid_file= D:\myapaphe\www\sphinx\log\ seamless_ rotate= 1preopen_indexes= 1unlink_old= 1workers= Threads # for RT to Workbinlog_path= D:\myapaphe\www\sphinx\data}

The above must be configured, the path must conform to their own path.

Next, build the query index:

To install the SEARCHD service:

Next load the configuration file:

Start the service:

OK before the configuration work, as well as the service startup has finished now the start code:

Under Sphinx, under the API folder, create test3.php, run test3.php

<?php require ("sphinxapi.php"); $s = new sphinxclient (); $s->setserver (' localhost ', 9312); $result = $s->query ( ' High seven '); Echo ' <pre> ';p rint_r ($result);

Garbled because the cmd default is GBK encoding, we put in the browser to see:

We saw that Sphinx did not find the complete result but returned the ID to us, so that we could check the data based on the ID.

The following query time comparison:

I was at the time of over 40,000 data tests at 0.001s, and we looked at how long MySQL queries would take:

We see the need for 0.04s, the data is not much, the results are not so obvious, but the 0.039s gap is not small.

To this Sphinx integration to this complete, hope can help everyone.

Related recommendations:

PHP's call to existing search engines is detailed

PHP Determines whether a visitor is a function code for a search engine spider

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.