How to enable sphinx full-text search in php

Source: Internet
Author: User
This article describes how to enable full-text search in php. it describes the configuration and usage skills related to sphsf-and provides some reference values, for more information about how to enable sphinx full-text search in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

During the compilation and installation of sphinx, a lot of Chinese characters are garbled, and finally the error is thrown out. I went to the official website to download an rpm package directly, so the installation would be great. I don't want to study the specific error, busy with development.

Install two packages: mmseg, a program that generates a Chinese dictionary, csft, and sphinx of the Chinese version.

After rpm-ivh is installed, it will be smooth ~~ The installation will be completed in less than half a minute.

I directly downloaded the Chinese dictionary Library from csft.

Unigram.txt uni. lib

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

Then, use mmseg-u unigram.txt to generate the Dictionary File: unigram.txt. uni and rename uni. lib, which is the dictionary recognized by sph.pdf.

Where? Put it in the dictionary path configured in the sphinx. conf file. we will talk about it later, and it will be almost the same. let's take a look at several practical sphinx programs:

The code is as follows:

[Root @ beihai365/] # csft-

Csft-indexer csft-search csft-searchd

Csft-indexer is a program for generating full-text search indexes.

Csft-search is used to test whether the search is effective or not. it is better to check whether the full-text search is successful if I haven't used client script development.

Csft-searchd is the daemon for sphsf-search. After startup, you can use scripts such as php and python to start the query.

It's that simple. let's take a look at the two key parts.

Sphworker. conf configuration file:

The code is 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 = 128 M
}

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 = 128 M
}
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
}


Let's take a look at the client code:

The code is as follows:

<? Php
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 );
?>
<? Php
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 );
?>


The keyword "name card" is manually added to the dictionary to check whether it can be found. The instance code is as follows:

The code is as follows:

Array
(
[Error] =>
[Warning] =>
[Status] => 0
[Fields] => Array
(
[0] => name
[1] => type
)

[Attrs] => Array
(
[Stock] => 1
)

[Matches] => Array
(
[0] => Array
(
[Id] => 8
[Weight] => 1
[Attrs] => Array
(
[Stock] = & gt; 100
)
)
)
[Total] => 1
[Total_found] => 1
[Time] = & gt; 0.018
[Words] => Array
(
[Card] => Array
(
[Docs] => 1
[Hits] => 1
)
)
)
Array
(
[Error] =>
[Warning] =>
[Status] => 0
[Fields] => Array
(
[0] => name
[1] => type
)
[Attrs] => Array
(
[Stock] => 1
)
[Matches] => Array
(
[0] => Array
(
[Id] => 8
[Weight] => 1
[Attrs] => Array
(
[Stock] = & gt; 100
)
)
)
[Total] => 1
[Total_found] => 1
[Time] = & gt; 0.018
[Words] => Array
(
[Card] => Array
(
[Docs] => 1
[Hits] => 1
)
)
)


There is no problem at all. The search has been made and several key operations are as follows:
[Root @ beihai365/] # csft-searchd -- stop search daemon

[Root @ beihai365/] # csft-indexer -- all generates an index for all nodes. you can also generate an index for a node, for example, csft-indexer xx

[Root @ beihai365/] # search for the keyword App in the csft-search App. However, the following information is not found and has 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 parameters, 0 hits
When you run these commands, you need to manually pin-config sphtasks. conf configuration file path is inconvenient, so I simply ln-s one in. /, so you do not need to input -- config every time.

I hope this article will help you with php programming.

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.