>  기사  >  백엔드 개발  >  PHP에서 스핑크스 검색 엔진을 사용하는 방법

PHP에서 스핑크스 검색 엔진을 사용하는 방법

小云云
小云云원래의
2018-03-21 14:12:102494검색

스핑크스는 단어 분할 검색 속도가 상대적으로 빠르며, 자체 데이터베이스 세트가 내장되어 있어 하드 디스크 파일에 저장됩니다. 그것이 모두에게 도움이 되기를 바랍니다.

php에서 사용되는 스핑크스 검색 엔진

스핑크스는 단어 분할 검색 속도가 비교적 빠르며 하드 디스크 파일에 저장되며 데이터베이스에 방해가 되지 않습니다. 자체 데이터베이스 내장

1. ubuntu에 sphinx 설치

aptitude가 설치되어 있지 않으면 apt get install을 사용하여 다음 명령을 설치할 때 문제가 발생하므로 먼저 aptitude를 설치해야 합니다.<br>sudo apt -설치 적성 얻기sudo apt-get install aptitude<br>sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -ysudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx -common -y

2.Configure

<br>

1

2

<br>

CD / etc/sphinxsearch/

cp sphinx.conf.sample sphinx.conf

구성 파일을 다음과 같이 수정하세요<br>

<br>

1

2

3

4

5

6

7

8

9

1 0

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

4 1

4 2

43

44

45

46

47

48

49

50

51

<br>

source src1

{

type = mysql

sql_host = localhost

sql_user = 루트

sql_pass = 매직모마

sql_db = 쿠폰_20160901

sql_port = 3306 # 선택 사항, 기본값은 3306

sql_query = SELECT 쿠폰 ID,제목,설명 FROM app_coupon_api

##### 기본 키, 단어 분할 색인 필드를 포함해야 함 ####### #

}

index test1

{

source = src1

path = /var/lib/sphinxsearch/data/test1 #인덱스 저장 디렉터리

docinfo = extern

mlock = 0

형태학 = none

min_word_len = 1

charset_type = utf-8

min_prefix_len = 0

min_infix_len = 0

ngram_len = 1

html_strip = 0

}

indexer

{

mem_limit = 2048M

}

<br>

searchd

{

listen = 9312

listen = 9306:mysql41

log = /var/log/sphinxsearch/searchd.log

query_log = /var/log/sphinxsearch/query.log

read_ 시간 초과 = 5

client_timeout = 30 0

max_children = 30

pid_file = /var/run/sphinxsearch/searchd.pid

max_matches = 1000

seamless_rotate = 1

preopen_indexes = 1

unlink_old = 1

mva_updates_ pool = 1M

max_packet_size = 8M

max_filters = 256

max_filter_values ​​​​= 4096

max_batch_queries = 32

workers = RT가 작동할 스레드 #

}

<br>

  1. 명령어 분할을 실행하면 /var/lib/sphinxsearch/data/test1 디렉터리에 여러 개의 색인 파일이 생성됩니다<br>sudo indexer -c /etc/sphinxsearch/sphinx.conf test1sudo indexer -c /etc/sphinxsearch/sphinx.conf test1

    test1为上述配置文件的index名字

4.命令行测试搜索

sudo search -c /etc/sphinxsearch/sphinx.conf google

二.在php中使用

1.安装php sphinx的依赖库

1.安装 aptitude

apt-get install aptitude<br>sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y

2.安装php sphinx的扩展

安装 pecl<br>sudo apt-get install php-pear php5-dev<br>在安装sphinx<br>sudo pecl install sphinx

3.在配置文件php.ini中添加sphinx的扩展,

我的php.ini文件为<br>sudo vim /etc/php5/fpm/php.ini<br>获取自己的php.ini文件位置使用<br>php5-fpm -i|grep ini

test1 is 위 구성 파일의 인덱스 이름입니다

4. 명령줄 테스트 검색

🎜sudo 검색 -c /etc/sphinxsearch/sphinx.conf google🎜

2. PHP에서 사용

1. PHP 스핑크스 종속 라이브러리 설치

1. 적성 설치 h5 >🎜apt-get 설치 적성🎜sudo 적성 설치 libsphinxclient-dev libsphinxclient-0.0.1 -y🎜
2. PHP 스핑크스 확장 설치
🎜pecl 설치🎜sudo apt-get install php-pear php5-dev🎜스핑크스 설치🎜sudo pecl install sphinx🎜
3. 구성 파일 php.ini에 스핑크스 확장을 추가합니다.
🎜내 php.ini 파일은🎜sudo vim /etc/php5/fpm / php.ini🎜🎜php5-fpm -i|grep ini🎜

추가:<br>extension=sphinx.soextension=sphinx.so<br>4.重启php5-fpm,查看php是否加载sphinx模块<br>sudo /etc/init.d/php5-fpm restart<br>5.将search程序运行在后台<br>sudo searchd -c /etc/sphinxsearch/sphinx.conf<br>默认监听配置文件中的端口:9312

6.在thinkphp中调用搜索<br>

<br>

1

2

3

4

5

6

7

8

9

10

11

12

13

<br>

public function testSphinx()

{

$s = new SphinxClient;

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

$s->SetArrayResult (true );

$s->setMatchMode(SPH_MATCH_ANY);

$s->setMaxQueryTime(3);

$result = $s->query("test");

$result = $result['matches'];

$result = array_column($result,'id');

$list = M('CouponApi')->field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();

dump($list);

}

搜索完毕,返回结果(默认返回20条,修改返回条数用添加 $s->SetLimits(0, 1000, 1000);4. php5-fpm을 다시 시작하고 php가 스핑크스 모듈을 로드하는지 확인하세요

sudo /etc/init.d/php5-fpm restart🎜5. 검색 프로그램은 백그라운드에서 실행됩니다🎜sudo searchd -c /etc/sphinxsearch/sphinx.conf🎜구성 파일의 기본 수신 포트: 9312🎜🎜6. thinkphp🎜🎜에서 검색 호출
<br>

1🎜

2 🎜

3🎜

4🎜

5🎜

6🎜

7 🎜

8🎜

9🎜

10🎜

11🎜

12 🎜

13🎜

<br>

공용 함수 testSphinx()🎜

{🎜

$s = new SphinxClient;🎜

$ s ->setServer("localhost", 9312);🎜

$s->SetArrayResult (true );🎜

$s->setMatchMode(SPH_MATCH_ANY) ; 🎜

$s->setMaxQueryTime(3);🎜

$result = $s->query("test");🎜

$result = $result['matches'];🎜

$result = array_column($result,'id');🎜

$list = M( ' CouponApi')->field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();🎜dump($list);🎜

}🎜

검색이 완료되고 결과가 반환됩니다( 기본값은 20입니다. 반환되는 항목 수를 수정하려면 $s->SetLimits(0, 1000, 1000);) 검색 속도가 상당히 빠르고, 색인을 생성하는 데 10초도 채 걸리지 않습니다. 800,000개 데이터의 제목 및 설명 필드 검색 엔진은 증분 색인, 다양한 검색 모드를 지원하며 인터넷에는 많은 정보가 있습니다🎜

위 내용은 PHP에서 스핑크스 검색 엔진을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.