apache solr php client

阅读更多

1,建立一个文件夹apache_solr,将下载好的solr解压,5个文件夹拷贝到里面。

 

2SolrPhpClient下载解压,将SolrPhpClient文件夹考到里面,此时apache_solr里面看起来像是这样

apache solr php client_第1张图片

 

3,安装jdk,命令行进入cd example,start.jar,如图,

回车,启动solr内置服务器,默认端口是8983,访问http://localhost:8983/solr/admin/进入实例图。

apache solr php client_第2张图片

 

,说明服务器启动成功!此时搜索热和内容都看不到任何值。

4.如图,提交配置文件.xml,即可搜索到内容

 

 

5.http://localhost:8983/solr/browse,例子。第四步执行完毕后,可搜索。

 

6.在根目录下建立search.php文件。

  require_once('SolrPhpClient/Apache/Solr/Service.php');

  $solr = new Apache_Solr_Service( 'localhost', '8983', '/solr' );

  if ( ! $solr->ping() ) {

    echo 'Solr service not responding.';

    exit;

  }

如果没报错,证明php调用成功!

 

 

代码

/**
 * solr 测试
 */
require_once(dirname(__FILE__).'SolrPhpClient/Apache/Solr/Service.php');
require_once(dirname(__FILE__).'SolrPhpClient/Apache/Solr/HttpTransport/Curl.php');

//测试使用基本连接 solr
defined('SOLR_HOST') || define('SOLR_HOST','localhost');
defined('SOLR_PORT') || define('SOLR_PORT','8983');
defined('SOLR_PATH') || define('SOLR_PATH','/solr/');

$solr = new Apache_Solr_Service(SOLR_HOST, SOLR_PORT, SOLR_PATH); 
$newTransport = new Apache_Solr_HttpTransport_Curl();
$solr->setHttpTransport($newTransport);

($solr->ping()) || die('not connect solr');

echo 'solr runing';

if(!empty($_POST['do']) && 'add' == $_POST['do']){
    // 开测构建document (需要被索引的文档, 这个可以来源于xml ,也可以是字符串,也可以来自数据库(DataImportHandler)
    // 这里我使用字符串进行测试(刚开始学 :D)

    $document = new Apache_Solr_Document();

    $document->id = uniqid();  
    $document->title = $_POST['title'];
    $document->name  = $_POST['name'];
    $document->manu  = $_POST['manu'];
    $document->cat   = $_POST['cat'];
    //$document->content = 'Some content for learn solr ,haha ,wo e le...';

    $solr->addDocument($document);
    $solr->commit();
}

$limit = 50;
// 显示内容
$query = !empty($_GET['q']) ? $_GET['q'] : '*:*';
if (get_magic_quotes_gpc() == 1)
{
    $query = stripslashes($query);
}

try
{
    $results = $solr->search($query, 0, $limit);
}
catch (Exception $e)
{
    die("查询失 败;

{$e->__toString()}
");
}

// display results
if ($results)
{
  $total = (int) $results->response->numFound;
  $start = min(1, $total);
  $end = min($limit, $total);
}
?>

  
    
    Solr
  
  
    
      查询:
      "/>
      
    
    
返回: 从  到  个结果, 共::

    
      // iterate result documents
  foreach ($results->response->docs as $doc)
  {
?>
        
    // iterate document fields / values
    foreach ($doc as $field => $value)
    {
?>
          
            
            
          
    }
?>
        
     
  }
?>
  
      

添加数据


      
       标题:


       姓名:


       manu:


        猫:


      
    
   



你可能感兴趣的:(apache solr php client)