Centos6 Install Sphinx+PHP to build a high-performance fulltext search engine.

Home Forums Share your experiences / 经验分享 Centos6 Install Sphinx+PHP to build a high-performance fulltext search engine.

Viewing 0 reply threads
  • Author
    Posts
    • Participant
      #1 lightwriter #540432

      Relative URLs:

      Sphinx official URL:

      http://sphinxsearch.com/downloads/current/

      Github repo( only have version 2.0 ):

      https://github.com/sphinxsearch/sphinx

      PHP extension:

      http://pecl.php.net/package/sphinx

      In this guidance, we will install sphinx from source code version 2.0

      1. Install Mysql, PHP, Nginx.

      In this tutor, we will install Mysql 5.7, PHP 7.2, Nginx 1.16.1

       

      2. Install gcc version >=4.7

      gcc 4.7 already included in devtools-1.1, so we install devtools-1.1

      If you installed the old version gcc,  remove it first.

      yum remove gcc

      cd /etc/yum.repos.d

      wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo

      yum –enablerepo=testing-1.1-devtools-6 install devtoolset-1.1-gcc devtoolset-1.1-gcc-c++

      update path

      export PATH=/opt/centos/devtoolset-1.1/root/usr/bin/:$PATH

      Now, check your gcc version.

      gcc –version

      gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)

      Copyright (C) 2012 Free Software Foundation, Inc.

      This is free software; see the source for copying conditions.  There is NO

      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

       

      3. Compile and install Sphinx from source code.

      git clone https://github.com/sphinxsearch/sphinx.git

      cd sphinx

      ./configure –prefix=/usr/local/sphinx LIBS=-liconv

      make

      make install

      After installation, sphinx already installed to /usr/local/sphinx

      cd /usr/local/sphinx

      ll

      drwxr-xr-x 2 root root 4096 Sep 11 21:13 bin

      drwxr-xr-x 2 root root 4096 Sep 11 21:13 etc

      drwxr-xr-x 3 root root 4096 Sep 11 21:13 share

      drwxr-xr-x 4 root root 4096 Sep 11 21:13 var

       

      4. Configuration

      cd etc

      mv sphinx.conf.dist sphinx.conf

      vi sphinx.conf

      Change your configuration as below:

      source src1

      {

      sql_host                = localhost

      sql_user                = test

      sql_pass                = test

      sql_db                  = test

      sql_port                = 3306  # optional, default is 3306

      sql_query_pre           = SET NAMES utf8

      sql_query_pre          = CREATE TABLE IF NOT EXISTS sph_counter ( counter_id int(11) NOT NULL, max_doc_id int(11) NOT NULL ,PRIMARY KEY (counter_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

      sql_query_pre          = REPLACE INTO sph_counter SELECT 1, MAX(ID) FROM you_table

      sql_query               = SELECT ID, text FROM you_table WHERE ID<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1) and ID>=$start AND id<=$end

      sql_query_range         = SELECT MIN(ID),MAX(ID) FROM you_table

      sql_range_step          = 1000

      sql_ranged_throttle     = 0

      }

      source src_delta : src1

      {

      sql_query_pre = set names utf8

      sql_query_post         = REPLACE INTO sph_counter SELECT 1, MAX(ID) FROM you_table

      sql_query = SELECT ID,text FROM you_table where ID > (SELECT max_doc_id FROM sph_counter WHERE counter_id=1) and ID>=$start AND ID<=$end

      sql_query_range = SELECT MIN(ID),MAX(ID) FROM you_table where ID>(SELECT max_doc_id FROM sph_counter WHERE counter_id=1)

      }

      index test1

      {

      source                  = src1

      path                    = /your-data-path

      docinfo                 = extern

      dict                    = keywords

      mlock                   = 0

      morphology              = none

      min_word_len            = 1

      html_strip              = 0

      }

      index index_src_delta: test1

      {

      source                  = src_delta

      path                    = /your-data-path

      }

       

      5. Generate index

      /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf –all –rotate

       

      6. Start service

      /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf

       

      7. Test with mysql

      mysql -h0 -P9306

      SELECT * FROM test1 WHERE MATCH(‘test’); SHOW META;

      +———+

      | id      |

      +———+

      | 1098344 |

      | 2024436 |

      |  109846 |

      |   39471 |

      |  243203 |

      |  244728 |

      |  244768 |

      |  253080 |

      |  363006 |

      |  439345 |

      |  442779 |

      |  655361 |

      |  900524 |

      |  923013 |

      |  997952 |

      | 1001382 |

      | 1020996 |

      | 1022382 |

      | 1024794 |

      | 1235509 |

      +———+

      +—————+——-+

      | Variable_name | Value |

      +—————+——-+

      | total         | 1000  |

      | total_found   | 2405  |

      | time          | 0.001 |

      | keyword[0]    | test  |

      | docs[0]       | 2405  |

      | hits[0]       | 2448  |

      +—————+——-+

      quit mysql

      quit

       

      8. Let sphinx support PHP

      compile libsphinxclient

      cd /root/sphinx/api/libsphinxclient/

      ./configure -prefix=/usr/local/sphinx

      make &&  make install

      Install gcc-c++ if meet this error

      checking how to run the C++ preprocessor… /lib/cpp

      configure: error: C++ preprocessor “/lib/cpp” fails sanity check

      yum install gcc-c++

      Download sphinx PHP extension source code.

      Due to support PHP 7, please download the latest source code here

      http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=d958afb6c587f08eee37602dbd8518afdcd72916;sf=tgz

      the compile and make PHP extension

      tar -xzf sphinx-d958afb.tar.gz

      cd sphinx-d958afb

      /usr/local/php/bin/phpize

      ./configure –with-php-config=/usr/local/php/bin/php-config –with-sphinx=/usr/local/sphinx

      make

      make install

      Edit php.ini to load sphinx

      vi /usr/local/php/etc/php.ini

      Add this line below

      extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/sphinx.so

      service nginx restart

      service php-fpm restart

       

      9. Write a sample PHP to test.

      <?php

      $sphinx = new SphinxClient;

      $sphinx->setServer(“localhost”, 9312);

      $sphinx->setMatchMode(SPH_MATCH_ANY);

      $sphinx->SetArrayResult ( true );

      $result = $sphinx->query(“test”,”*”);

      print_r( $result );

      ?>

Viewing 0 reply threads

You must be logged in to reply to this topic.