sphinx installation and use

download link:

http://sphinxsearch.com/downloads/archive/

 

Unzip and compile the configuration

$ ./configure --prefix=/usr/local/sphinx --with-mysql

$ make

$ make install

 

$ cd /usr/local/sphinx/etc/

$ cp sphinx.conf.dist sphinx.conf

 

######################################################################

Use the example that comes with sphinx

Import test data

$ mysql-u root -p  < /usr/local/sphinx/etc/example.sql

 

Set database information

$ vim sphinx.conf

 

##### Index Source ###########

source src1

{

    type = mysql #####Datasource type

    sql_host = localhost ######mysql host

    sql_user=root ########mysql username

    sql_pass=pwd ############mysql password

    sql_db = test #########mysql database name

    sql_port= 3306 ###########mysql port

    sql_query_pre = SET NAMES UTF8 ###mysql retrieval encoding

    ####Set datasource

    sql_query = sql_query                = \

            SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \

            FROM documents

}

 

set index

index test1

{

    source = src1 #### declare index source

    path = /usr/local/sphinx/var/data/test1 #######Index file storage path and index file name

    docinfo = extern ##### Document information storage method

}

 

generate index

$ bin/indexer -c etc/sphinx.conf test1

 

Call shpinx to search under php (this is the use case of the php cli mode in the source code api, and there are related use cases of other language calls)

$ cd /usr/local/software/sphinx-2.2.11-release

$ php test.php test

######################################################################

 

PHP calls demo

##### Index Source ###########

source src1

{

    type = mysql #####Datasource type

    sql_host=192.168.1.10 ######mysql host

    sql_user=root ########mysql username

    sql_pass=pwd ############mysql password

    sql_db = test #########mysql database name

    sql_port= 3306 ###########mysql port

    sql_query_pre = SET NAMES UTF8 ###mysql retrieval encoding

    sql_query = SELECT * FROM task ####### sql to get data

 

    #####The following properties are used to filter or conditional queries############

 

    ####Customize returned fields

    sql_field_string        = id

    sql_field_string        = name

}

 

### Index ###

index test1

{

    source = article_src #### declare index source

    path = /usr/local/sphinx/var/data/article #######Index file storage path and index file name

    docinfo = extern ##### Document information storage method

    mlock = 0 ###Cache data memory lock

    morphology = none

    min_word_len = 1 #### Indexed word minimum length

    charset_type = utf-8 ##### data encoding

 

    ##### Character table, note: if this method is used, sphinx will segment Chinese characters.

    ##### is the word index, if you want to use Chinese word segmentation, you must use other word segmentation plugins such as coreseek, sfc

}

 

######### Indexer Configuration #####

indexer

{

    mem_limit = 256M ####### memory limit

}

 

############ sphinx service process ########

searchd

{

    listen = 9312 ### listen port

 

    log = /usr/local/sphinx/var/log/searchd.log #### Service process log, once sphinx is abnormal, you can basically query valid information from here, and the problem of rotation can generally be found here to the answer

    query_log = /usr/local/sphinx/var/log/query.log ### Client query log, author's note: If you want to count some keywords, you can analyze this log file

    read_timeout = 5 ## request timeout

    max_children = 30 ### The maximum number of searchd processes that can be executed at the same time

    pid_file = /usr/local/sphinx/var/log/searchd.pid #######Process ID file

    max_matches = 1000 ### The maximum number of returned query results

    seamless_rotate = 1 ### Whether to support seamless switching, usually required for incremental indexing

}

 

Generate index (only generate test1 index file)

$ bin/indexer -c etc/sphinx.conf test1

 

If the daemon process is running when the index is rebuilt, an error will be reported. You need to run the following command, the index will be rebuilt and the daemon process will be restarted

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

The --all parameter means that all index files will be generated

 

searchd as sphinx daemon on the server

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

stop

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

 

PHP side

<?php

    require "sphinxapi.php"; //sphinx's api directory (eg: /usr/local/software/sphinx-2.2.11-release/api/)

    $cl = new SphinxClient();

    $cl->setServer("localhost", 9312);

    #$cl->setMatchMode(SPH_MATCH_EXTENDED); //Use multi-field mode

    $res = $cl->query($keyword, "test1");

    //$err = $cl->getLastError();

    $res = !empty($res['matches']) ? $res['matches'] : "";

    print_r($res);

 

result:

Array

(

    [1] => Array

        (

            [weight] => 1557

            [attrs] => Array

                (

                    [name] => zhengzhou

                )

 

        )

 

    [3] => Array

        (

            [weight] => 1557

            [attrs] => Array

                (

                    [name] => kaifeng-zhengzhou

                )

 

        )

 

)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326161073&siteId=291194637