ホームページ  >  記事  >  バックエンド開発  >  PHPでSphinx検索エンジンを使用する方法

PHPでSphinx検索エンジンを使用する方法

小云云
小云云オリジナル
2018-03-21 14:12:102493ブラウズ

sphinx は、単語分割検索の速度が比較的速く、データベースに干渉しません。皆さんのお役に立てれば幸いです。

php では sphinx 検索エンジンが使用されています

sphinx は、単語の分割検索速度が比較的速く、データベースに干渉しません。独自の組み込みデータベースのセット

1. ubuntu に sphinx をインストールします

aptitude がインストールされていない場合は、apt get install を使用して次のコマンドをインストールするときに問題が発生するため、最初に aptitude をインストールする必要があります。<br>sudo apt -get install aptitudesudo 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 = root

sql_pass = magicmoma

sql_db = Coupon_20160901

sql_port = 3306 # オプション、デフォルトは 3306 です

sql_query = SELECT Couponid,title,description FROM app_coupon_api

##### 主キー、単語セグメンテーション インデックス フィールドを含める必要があります ####### #

}

index test1

{

source = src1

path = /var/lib/sphinxsearch/data/test1 #インデックス格納ディレクトリ

docinfo = extern

mlock = 0

morphology = 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

読み取りタイムアウト= 5

client_timeout = 30 0

max_children = 30

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

max_matches = 1000

simless_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上記の設定ファイルのインデックス名です

4. コマンド ライン テスト検索

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

2. php で使用する

1. PHP スフィンクス依存ライブラリをインストールする

1. aptitude をインストールします h5 >🎜apt-get install aptitude🎜sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y🎜
2. php sphinx 拡張機能をインストールします
🎜 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🎜次を使用して独自の 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 が sphinx モジュールをロードするかどうかを確認します

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でSphinx検索エンジンを使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。