Solr 学習 (6) - Solr の PHP クライアント

WBOY
リリース: 2016-06-13 13:25:13
オリジナル
756 人が閲覧しました

Solr の学習 (6) —— Solr PHP クライアント

solr クエリは、xml 形式または json 形式のみを返します。実際、solr は、Google や Baidu を使用するときに使用する美しくてすっきりしたインターフェイスとは異なります。 Google のようなインターフェイスを生成したい場合は、Solr の PHP クライアントを使用してクエリを実行すると、PHP コードによって表示されます。

この記事のアプローチは、一方のサーバーが tomcat を使用して solr を実行し、もう一方のサーバーが Apache を使用してユーザーとの対話と表示を担当するというものです。

?

solr にはいくつかの PHP クライアントがありますが、この記事ではシンプルで使いやすいもの、?php-solr-client を選択しました。プロジェクトのアドレスは ?http://code です。 google.com/p/solr-php-client/ をダウンロードして解凍し、Apache Web サイトのルート ディレクトリに置きます。

?

?以下は簡単なクエリの例です:

?

?

<?php

// make sure browsers see this page as utf-8 encoded HTML
header('Content-Type: text/html; charset=utf-8');

$limit = 10;
$query = isset($_REQUEST['q']) ? $_REQUEST['q'] : false;
$results = false;

if ($query)
{
  // The Apache Solr Client library should be on the include path
  // which is usually most easily accomplished by placing in the
  // same directory as this script ( . or current directory is a default
  // php include path entry in the php.ini)
  require_once('Apache/Solr/Service.php');

  // create a new solr service instance - host, port, and webapp
  // path (all defaults in this example)
  $solr = new Apache_Solr_Service('localhost', 8983, '/solr/');

  // if magic quotes is enabled then stripslashes will be needed
  if (get_magic_quotes_gpc() == 1)
  {
    $query = stripslashes($query);
  }

  // in production code you'll always want to use a try /catch for any
  // possible exceptions emitted  by searching (i.e. connection
  // problems or a query parsing error)
  try
  {
    $results = $solr->search($query, 0, $limit);
  }
  catch (Exception $e)
  {
    // in production you'd probably log or email this error to an admin
        // and then show a special message to the user but for this example
        // we're going to show the full exception
        die("<html><head><title>SEARCH EXCEPTION</title><body><pre class="brush:php;toolbar:false">{$e->__toString()}
"); } } ?> PHP Solr Client Example
response->numFound; $start = min(1, $total); $end = min($limit, $total); ?>
Results - of :
    response->docs as $doc) { ?>
  1. $value) { ?>
ログイン後にコピー
?

たとえば、「car」と入力してクエリを実行すると、結果は次のようになります?


クエリのために Solr に正常に接続されました。芸術的な最適化により、Baidu のようなクエリ インターフェイスが得られます

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!