PHP Enable Sphinx Full-Text Search implementation Method _php tips

Source: Internet
Author: User

This example describes the implementation of PHP enabled Sphinx Full-text Search. Share to everyone for your reference. The specific analysis is as follows:

In the compilation of installation Sphinx a lot of Chinese garbled, the last throw error stuck, I went to the official directly download an RPM package, installation is very cool, specific errors do not want to study, busy development.

Install two packages, one is mmseg this is to generate the Chinese dictionary of the program, one is csft is the Chinese version of the Sphinx.

RPM-IVH after installation, very smooth ~ ~ In less than half a minute to install the finished.

Chinese dictionary library, I went directly to the csft official download, very good thought very thoughtful.

Unigram.txt Uni.lib

Unigram.txt dictionary text, you can add your own keywords inside.

Then use: Mmseg-u unigram.txt to generate the dictionary file: Unigram.txt.uni and then rename Uni.lib This is the Dictionary Sphinx know.

Where to put it? Put you in the sphinx.conf inside the dictionary path configuration, and so will say, and then basically almost, under the Sphinx several practical procedures:

Copy Code code as follows:
[Root@beihai365/]# csft-

Csft-indexer Csft-search Csft-searchd

Csft-indexer is the program that generates the Full-text search index

Csft-search is to test whether the search is effective or not, and it works well, as I have no use for client script development, you can use this to see if Full-text search is successful

Csft-searchd This is the Sphinx search daemon. After startup, you can use scripting Php python and so on, open the query.

It's as simple as looking at the two key parts of the thing.

sphinx.conf configuration file:

Copy Code code as follows:
Source Tmsgs


{   


type = MySQL


sql_host = localhost


Sql_user = root


Sql_pass = 1


sql_db = phpwind75sp3


Sql_port = 3306 # optional, default is 3306


#sql_sock =/tmp/mysql3307.sock


sql_query_pre = SET NAMES GBK


Sql_query = SELECT Id,name,type,stock from Pw_tools


#sql_attr_uint = ID


sql_attr_uint = Stock


}   


  


Index Tmsgsindex


{   


Source = Tmsgs


Path =/var/mmseg/searchdata/beihai365


docinfo = extern


Charset_type = ZH_CN.GBK


#min_prefix_len = 0


#min_infix_len = 2


#ngram_len = 2


Charset_dictpath =/var/mmseg/data


#min_prefix_len = 0


#min_infix_len = 0


#min_word_len = 2


}   


  


Indexer


{   


Mem_limit = 128M


}   


  


Searchd


{   


#listen = 3312


log =/var/log/searchd.log


Query_log =/var/log/query.log


read_timeout = 5


Max_children = 30


pid_file =/var/log/searchd.pid


max_matches = 1000


#seamless_rotate = 1


#preopen_indexes = 0


#unlink_old = 1


}  


Source Tmsgs


{


Type = MySQL


sql_host = localhost


Sql_user = root


Sql_pass = 1


sql_db = phpwind75sp3


sql_port = 3306 # optional, default is 3306


#sql_sock =/tmp/mysql3307.sock


sql_query_pre = SET NAMES GBK


sql_query = SELECT id,name,type,stock from Pw_tools


#sql_attr_uint = ID


sql_attr_uint = Stock


}


Index Tmsgsindex


{


Source = Tmsgs


Path =/var/mmseg/searchdata/beihai365


docinfo = extern


Charset_type = ZH_CN.GBK


#min_prefix_len = 0


#min_infix_len = 2


#ngram_len = 2


Charset_dictpath =/var/mmseg/data


#min_prefix_len = 0


#min_infix_len = 0


#min_word_len = 2


}


Indexer


{


mem_limit = 128M


}


Searchd


{


#listen = 3312


log =/var/log/searchd.log


Query_log =/var/log/query.log


read_timeout = 5


Max_children = 30


pid_file =/var/log/searchd.pid


max_matches = 1000


#seamless_rotate = 1


#preopen_indexes = 0


#unlink_old = 1


}



look again, test client code:


Copy Code code as follows:
<?php   
    header ("Content-type:text /html;charset=utf-8 ");   
    include ' sphinxapi.php ';   
& nbsp;   $cl = new Sphinxclient ();   
    $cl->setserver (' localhost ', 3312);   
    $cl->setmatchmode (sph_match_all);   
     $cl->setarrayresult (True);   
    $res = $cl->query ("name card", "*");    
    print_r ($res);   
?>  
<?php br> header ("Content-type:text/html;charset=utf-8");
include ' sphinxapi.php ';
$cl = new Sphinxclient ();
$cl->setserver (' localhost ', 3312);
$cl->setmatchmode (Sph_match_all);
$cl->setarrayresult (true);
$res = $cl->query ("name card", "*");
Print_r ($res);
?>



"name card" This keyword is I manually added to the dictionary, to see if it can be really found, the example code is as follows:


Copy Code code as follows:
Array


(   


[ERROR] =&gt;


[WARNING] =&gt;


[status] =&gt; 0


[Fields] =&gt; Array


        (   


[0] =&gt; name


[1] =&gt; type


        )   


  


[Attrs] =&gt; Array


        (   


[stock] =&gt; 1


        )   


  


[matches] =&gt; Array


        (   


[0] =&gt; Array


                (   


[id] =&gt; 8


[Weight] =&gt; 1


[Attrs] =&gt; Array


                        (   


[stock] =&gt; 100


                        )   


)   


        )   


[Total] =&gt; 1


[Total_found] =&gt; 1


[TIME] =&gt; 0.018


[words] =&gt; Array


        (   


[name card] =&gt; Array


                (   


[docs] =&gt; 1


[hits] =&gt; 1


                )   


        )   


)  


Array


(


[ERROR] =&gt;


[WARNING] =&gt;


[status] =&gt; 0


[Fields] =&gt; Array


        (


[0] =&gt; name


[1] =&gt; type


        )


[Attrs] =&gt; Array


        (


[stock] =&gt; 1


        )


[matches] =&gt; Array


        (


[0] =&gt; Array


                (


[id] =&gt; 8


[weight] =&gt; 1


[Attrs] =&gt; Array


                        (


[stock] =&gt; 100


                        )


                )


        )


[Total] =&gt; 1


[Total_found] =&gt; 1


[TIME] =&gt; 0.018


[Words] =&gt; Array


        (


[name card] =&gt; Array


                (


[docs] =&gt; 1


[hits] =&gt; 1


                )


        )


)



completely no problem, search out, a few key operations:


[root@beihai365/]# csft-searchd--stop stop search Guardian

[root@beihai365/]# csft-indexer--all generates indexes for all nodes, you can also index a node for example: Csft-indexer xx

[root@beihai365/]# csft-search app search keyword app, but look at the following information is not found and did not hit any documents.

Coreseek Full Text Server 3.1
Copyright (c) 2006-2008 coreseek.com
Using config file './csft.conf ' ...
1,
Pt:1, 1; Index ' tmsgsindex ': Query ' App ': returned 0 matches of 0 Total in 0.017 sec
Words
1. ' App ': 0 documents, 0 hits
When you run these commands to find that you need your own manual top--config sphinx.conf configuration file path, very inconvenient, so I simply ln-s a in./, so do not have to typing--config every time.

I hope this article will help you with your PHP program design.

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: info-contact@alibabacloud.com 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.