1. Prepare MySQL source tree
   5.0
   There are two options:
   a) 
      - Download sources of mysql-5.0.22.tar.gz from dev.mysql.com site.
      - Copy patch file sphinx.5.0.22.diff into source directory and run
         patch -p1 < sphinx.5.0.22.diff
      - run BUILD/autorun.sh
      - make dir sql/sphinx and place files from sphinx-se.tar.gz here
   b)
      - Download prepared sources from sphinxsearch.com site
        mysql-5.0.22-sphinx.tar.gz

   5.1 ( only 5.1.12 or later are supported )
      - place files from sphinx-se.tar.gz into storage/sphinx dir
                  
2. Compile mysql

   - Use configure for 5.0:  --with-sphinx-storage-engine option
                   for 5.1:  --with-plugins=sphinx
		   
   - See detailed info on http://dev.mysql.com/doc/refman/5.0/en/installing-source.html
   - I usually use:
   	 
CFLAGS="-g -O2 -fno-omit-frame-pointer" CXX="gcc" \ 
CXXFLAGS="-g -felide-constructors -fno-exceptions -fno-rtti  -O2 -fno-omit-frame-pointer" \
CXXLDFLAGS="" \
./configure --prefix=/usr/local/mysql-5-sphinx \
	 --enable-assembler \
	 --with-extra-charsets=complex \
	 --enable-thread-safe-client \
	 --with-readline \
	 --with-big-tables  \
	 --enable-local-infile \
	 --with-sphinx-storage-engine (--with-plugins=sphinx in 5.1)

gmake -j4

3. Check installation
   - run mysql client
   - execute SHOW ENGINES;
     You should see something like:
     
mysql> show engines;
+------------+----------+----------------------------------------------------------------+
| Engine     | Support  | Comment                                                        |
+------------+----------+----------------------------------------------------------------+
| MyISAM     | DEFAULT  | Default engine as of MySQL 3.23 with great performance         | 
  ...
| SPHINX     | YES      | Sphinx storage engine                                          | 
  ...
+------------+----------+----------------------------------------------------------------+
13 rows in set (0.00 sec)    

   if you see SPHINX - YES, mysql is ready to use sphinx storage engine
   
4. Create SPHINX table:

   CREATE TABLE t1 (id INT, gid INT, doctime TIMESTAMP, weigth INT, search VARCHAR(255) NOT NULL, KEY qidx (search))
   ENGINE=SPHINX
   CONNECTION="sphinx://localhost:3312/myindex";
   
   This statement declares table will access to sphinx daemon on localhost:3312 and will use index "myindex".
   
   Table name doesn't matter, you can use any allowed;
   Column names doesn't matter, you can use any allowed;
   Column types are IMPORTANT especially 3rd column must be TIMESTAMP, and 5th column must be VARCHAR;
   KEY on 5th column is mandatory;
   
   CONNECTION string:
     "sphinx://HOST:PORT/INDEXNAME"
   sphinx storage engine will try to connect to sphinx deamon on HOST:PORT and use index INDEXNAME;
   if you skip CONNECTION option - default value is 127.0.0.1:3312/*
   
   You can change the connection params later by command
   
   ALTER TABLE t1 CONNECTION="sphinx://NEW_HOST:NEW_PORT/INDEXNAME";
   
   
5. Query to sphinx daemon
   You should always lookup sphix table by <search> key, typically joining it
   with indexed documents table , like:


   SELECT [columnt_list] FROM test.documents docs JOIN t1 fs ON (docs.id=fs.id) 
       WHERE query="query_string"; 
   
   
   query_string has format:
   
   "param1=value;param2=value;param3=value;...";       
   
   Available params:
   query  - text of query
   mode   - search mode; all - MATCH_ALL (default) ; any - MATCH_ANY; phrase - MATCH_PHRASE; boolean - MATCH_BOOLEAN
   sort   - sort mode; 0 - RELEVANCE (default) ; 1 - DATE DESC; 2 - DATE ASC; 3 - TIME SEGMENTS
   limit  - limit of output records (default 20)
   offset - start from record from offset (default 0)
   index  - name of index or * (default)
   minid / maxid - limits for id
   min_ts / max_ts - minimal and maximal timestamp
   min_gid / max_gid - minimal and maxumal group id
   groups - list of groups for lookup (formar groups=1,3,4,56)
   weights - use this weights for columns (format weights=1,10,4)
   
   only query field is mandatory, so simplest query_string is
   
   search="query=sphinx" or even search="sphinx"
   
   more complex examples:
   
   search="sphinx mysql;mode=any" - match any of words 
   search="sphinx oracle;offset=100;limit=50"; - fetch result starting with offset 100 and limit count of records = 50
   search="sphinx postgresql;groups=1,4,5;index=test1;" - search only in groups 1,4,5 in index test1
   search="sphinx & postgresql | (soft & java);mode=boolean" - boolean search
   

   example output:
      
mysql> select * from t1 where query="document;index=test1";    
+------+------+---------------------+--------+----------------------+
| id   | gid  | docdate             | weight | query                |
+------+------+---------------------+--------+----------------------+
|    2 |    1 | 2006-05-13 07:39:02 |      1 | document;index=test1 | 
|    1 |    1 | 2006-05-13 07:39:02 |      1 | document;index=test1 | 
+------+------+---------------------+--------+----------------------+
2 rows in set (0.00 sec)
   
After executed query additinal info available by command:
SHOW ENGINE SPHINX STATUS:
   
mysql> show engine sphinx status;                 
+--------+-------+-------------------------------------------------+
| Type   | Name  | Status                                          |
+--------+-------+-------------------------------------------------+
| SPHINX | stats | total: 25, total found: 25, time: 126, words: 2 | 
| SPHINX | words | sphinx:591:1256 soft:11076:15945                | 
+--------+-------+-------------------------------------------------+
2 rows in set (0.00 sec)
   
6. Example with JOIN.
  Assuming you have table "documents" from example.sql, and table fulltextsearch with SPHINX engine;
  
mysql> SELECT content,docdate FROM test.documents docs JOIN fulltextsearch fs ON (docs.id=fs.id) 
       WHERE query="one document;mode=any"; 
       
+-------------------------------------+---------------------+
| content                             | docdate             |
+-------------------------------------+---------------------+
| this is my test document number two | 2006-06-17 14:04:28 | 
| this is my test document number one | 2006-06-17 14:04:28 | 
+-------------------------------------+---------------------+
2 rows in set (0.00 sec)

mysql> show engine sphinx status;
+--------+-------+---------------------------------------------+
| Type   | Name  | Status                                      |
+--------+-------+---------------------------------------------+
| SPHINX | stats | total: 2, total found: 2, time: 0, words: 2 | 
| SPHINX | words | one:1:2 document:2:2                        | 
+--------+-------+---------------------------------------------+
2 rows in set (0.00 sec)



   
   
   
   
   
   
   
   
   
           
   
       
   

   

