Sphinx + PHPで入門検索エンジンを作成する

背景



以前は、通常の全文検索を使用してサイトを検索しました。 しかし、ある時点で、彼は私たちに合うのをやめ、私たちは代替の検索技術であるスフィンクスを試してみることにしました。 残念ながら、スフィンクスにはロシア語のドキュメントがまったくありません。したがって、この記事はロシア語とローカル環境(windows 7、mysql / php)でのみPHPカスタム検索エンジンを構築する記事に類似しています

この記事は4つのパートで構成されています。

  1. 検索のベースを準備することに関する短い話。
  2. スフィンクスの初期インストールと設定に関するストーリー
  3. コマンドラインからのデータベースのインデックス作成とテスト検索
  4. PHPからの検索のテスト




1.検索用のデータベースを準備する



スフィンクスの使用を開始する前に、データベースをすぐに準備して、より便利なインデックス作成と検索を行うことをお勧めします。

製品、企業、カテゴリ、それぞれ製品、企業、製品カテゴリの3つのテーブルがあります。 検索する3つのテーブルすべてのデータを結合するビューを作成します。

CREATE OR REPLACE VIEW `catalog` AS SELECT p.`id` , p.`id_company` , p.`id_category` , p.`name` , p.`keyword` , p.`short_desc` , com.`name` AS company_name, cat.`name` AS category_name FROM `products` p JOIN `categories` cat ON p.id_category = cat.id JOIN `companies` com ON p.id_company = com.id WHERE 1
      
      





私の場合の検索は、名前、キーワード、製品の簡単な説明、製造業者の名前、製品カテゴリの名前のフィールドで実行されます。 WHEREでは、たとえば、追加の条件を追加して、アクティブな会社と製品のみを検索することができます。 これで、データベースの準備作業が完了しました。



2. Sphinxをインストールする



オフサイトから新鮮な配布キットをダウンロードする必要があります: www.sphinxsearch.com/downloads.html

MySQLサポート1.10ベータ版のWin32バイナリのバージョンをダウンロードしました。 次に、アーカイブの内容をフォルダーに解凍します(C:\ Sphinxで解凍しました)



Sphinxの構成


アーカイブをダウンロードして解凍したら、次のステップはセットアップです。 私はsphinx.conf.inファイルを基礎として取りました

このセクションでは、設定に小さなコメントを付けて簡単に説明しますが、すべてが明確になっているようです。 このセクションのすべてのテキストは1つのsphinx.conf.inファイルにあります



データソース


 #     source catalog { #   #  : mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc type = mysql #        sql_host = localhost sql_user = root sql_pass = sql_db = products sql_port = 3306 # ,   3306 # -,           #       UTF-8,           sql_query_pre = SET NAMES utf8 # ,       #        ID  sql_query = \ SELECT * \ FROM catalog # ,     ,    sql_attr_uint = id_company sql_attr_uint = id_category # document info query, ONLY for CLI search (ie. testing and debugging) # optional, default is empty # must contain $id macro and must fetch the document by that id sql_query_info = SELECT * FROM products WHERE id=$id }
      
      







製品のインデックス作成オプション


 #      index catalog { #     source = catalog # ,      path = C:\Sphinx/data/catalog #     morphology = stem_ru #      min_word_len = 1 #  charset_type = utf-8 }
      
      







検索デーモンの設定(サービス)


 #   () searchd { #      ""  listen = 9312 #    log = C:\Sphinx/log/searchd.log #      query_log = C:\Sphinx/log/query.log # PID file, searchd process ID file name # mandatory pid_file = C:\Sphinx/log/searchd.pid }
      
      







これで、スフィンクスの構成が完了しました。

次に、サービスをインストールします。 これを行うには、アドレスバーにC:\ Sphinx \ bin \ searchd --install --config C:\ Sphinx \ sphinx.conf.in --servicename SphinxSearchと記述します。

サービスがインストールされ、管理-サービスに表示されるはずです。 まだ開始していません-データはまだインデックスに登録されていません。



3.インデックス作成とテスト検索



コマンドラインでC:\ Sphinx \ bin \ indexer --all --config C:\ Sphinx \ sphinx.conf.inを書きます。 次のような結果が得られます。

 C:\Users\Iskander> C:\Sphinx\bin\indexer --all --config C:\Sphinx\sphinx.conf.in Sphinx 1.10-beta (r2420) Copyright (c) 2001-2010, Andrew Aksyonoff Copyright (c) 2008-2010, Sphinx Technologies Inc (http://sphinxsearch.com) using config file 'C:\Sphinx\sphinx.conf.in'... indexing index 'catalog'... collected 572 docs, 0.1 MB sorted 0.0 Mhits, 100.0% done total 572 docs, 115192 bytes total 0.380 sec, 303124 bytes/sec, 1505.19 docs/sec total 2 reads, 0.000 sec, 31.6 kb/call avg, 0.0 msec/call avg total 9 writes, 0.001 sec, 14.6 kb/call avg, 0.1 msec/call avg
      
      







これで、インデックスが作成されました。 ここでSphinxSearchサービスを開始し、検索バーに次のように書き込みます

C:\ Sphinx \ bin \ search --config C:\ Sphinx \ sphinx.conf.inコンピューターで結果を取得

 C:\Users\Iskander> C:\Sphinx\bin\search --config C:\Sphinx\sphinx.conf.in computer Sphinx 1.10-beta (r2420) Copyright (c) 2001-2010, Andrew Aksyonoff Copyright (c) 2008-2010, Sphinx Technologies Inc (http://sphinxsearch.com) using config file 'C:\Sphinx\sphinx.conf.in'... index 'catalog': query 'computer': returned 1 matches of 1 total in 0.000 sec displaying matches: 1. document=1, weight=2812, id_company=2, id_category=1263 id=1 id_company=2 id_category=1263 words: 1. 'computer': 1 documents, 2 hits
      
      







検索は機能します!



4. phpから検索



まあ、すべてが簡単です。 たとえば、「コンピューター」を検索します

 <?php //    api include('C:\Sphinx\api\sphinxapi.php'); //   -        $cl = new SphinxClient(); $cl->SetServer( "localhost", 9312 ); //   $cl->SetMatchMode( SPH_MATCH_ANY ); //    1     $result = $cl->Query("computer"); //   //    if ( $result === false ) { echo "Query failed: " . $cl->GetLastError() . ".\n"; //     } else { if ( $cl->GetLastWarning() ) { echo "WARNING: " . $cl->GetLastWarning() . " //      "; } if ( ! empty($result["matches"]) ) { //     -   foreach ( $result["matches"] as $product => $info ) { echo $product . "<br />"; //   id   } } } exit;
      
      







以上で、sphinxをインストールし、データベースのインデックスを作成し、データベースを検索して見つかった製品のIDを表示するphpコードを作成しました。



ドキュメンテーションをさらに研究し、プロジェクトの必要に応じてランキングを調整し、通常は全文検索からスフィンクスに機能を移行します。



私の意見では、このかなり複雑な製品に関する通常のロシアの記事やドキュメントが本当に不十分だと思うので、この記事が誰かがSphinxを「クイックスタート」するのに役立つことを願っています。



All Articles