PHP Elasticsearch的使用

來源:互聯網
上載者:User

標籤:知識   auto   命令      javase   解壓   plain   啟動   exe   

Elasticsearch是一個基於Lucene的穩定的、分布式、RESTFul的搜尋引擎。其實所謂的RestFul就是它提供URL供你調用(建立索引和進行檢索),不過直接這樣使用實在是太兇殘了。所以,它也提供了一系列client包,相當於將curl請求封裝了,client包支援的語言套件括Java、PHP、Python、Ruby和Perl等等。

PHP版的client包叫做elasticsearch-php,可以在Git_hub上下載。地址如下:https://github.com/elasticsearch/elasticsearch


要使用elasticsearch-php有如下三個要求:

1.PHP的版本在5.3.9以上,我用的是PHP5.3.23

2.在項目中使用Composor來管理組件,如下:https://getcomposer.org/

3.在php.ini中開啟curl和openssl

要使用elasticsearch,需要JDK的版本大於6,最好選擇8吧,因為7有漏洞....

截一張需要的包圖:

 

啟動elasticsearch很簡單,直接進入解壓目錄,運行elasticsearch.bat就可以了,看到最後console輸出start,就啟動成功了。

 

接下來介紹如何使用elasticsearch-php:

 

1.建立一個檔案夾取名為test,此為專案檔夾

2.在裡面放入一個命名為composer.json的檔案,檔案內容為:

 

[html] view plain copy  print?
  1. {  
  2.     "require":{  
  3.         "elasticsearch/elasticsearch" : "~1.2"  
  4.     }  
  5. }  
{"require":{"elasticsearch/elasticsearch" : "~1.2"}}

3.將composer.phar拷貝到test檔案夾中,cd 到test檔案夾,輸入命令:php composer.phar install --no-dev  等待安裝成功

 

這個時候test檔案夾下面應該會出現vendor檔案夾,裡面有elasticsearch、composer、guzzle等檔案夾,很多內容

4.這個時候,就可以使用elasticsearch進行建立索引和進行檢索了

 

[php] view plain copy  print?
  1. <?php   
  2. require_once(‘vendor/autoload.php‘);  
  3. function get_conn(){  
  4.     $host = ‘ip‘;  
  5.     $dbname = ‘dbname‘;  
  6.     $user = ‘user‘;  
  7.     $passwd = ‘passwd‘;  
  8.   
  9.     $conn = new PDO("pgsql:dbname=$dbname;host=$host",$user,$passwd);  
  10.     return $conn;  
  11. }  
  12.   
  13. function create_index(){  
  14.     //Elastic search php client  
  15.     $client = new Elasticsearch\Client();  
  16.     $sql = "SELECT * FROM log";  
  17.     $conn = get_conn();  
  18.     $stmt = $conn->query($sql);  
  19.     $rtn = $stmt->fetchAll();  
  20.   
  21.     //delete index which already created  
  22.     $params = array();  
  23.     $params[‘index‘] = ‘log_index‘;  
  24.     $client->indices()->delete($params);  
  25.       
  26.     //create index on log_date,src_ip,dest_ip  
  27.     $rtnCount = count($rtn);  
  28.     for($i=0;$i<$rtnCount;$i++){  
  29.         $params = array();  
  30.         $params[‘body‘] = array(  
  31.             ‘log_date‘ => $rtn[$i][‘log_date‘],  
  32.             ‘src_ip‘ => $rtn[$i][‘src_ip‘],  
  33.             ‘dest_ip‘ => $rtn[$i][‘dest_ip‘]  
  34.         );  
  35.         $params[‘index‘] = ‘log_index‘;  
  36.         $params[‘type‘] = ‘log_type‘;  
  37.           
  38.         //Document will be indexed to log_index/log_type/autogenerate_id          
  39.         $client->index($params);  
  40.     }  
  41.     echo ‘create index done!‘;  
  42. }  
  43.   
  44. function search(){  
  45.     //Elastic search php client  
  46.     $client = new Elasticsearch\Client();  
  47.     $params = array();  
  48.     $params[‘index‘] = ‘log_index‘;  
  49.     $params[‘type‘] = ‘log_type‘;  
  50.     $params[‘body‘][‘query‘][‘match‘][‘src_ip‘] = ‘1.122.33.141‘;  
  51.   
  52.     $rtn = $client->search($params);  
  53.     var_dump($rtn);  
  54. }  
  55.   
  56. set_time_limit(0);  
  57. //create_index();  
  58. search();  
  59. ?>  
<?php require_once(‘vendor/autoload.php‘);function get_conn(){$host = ‘ip‘;$dbname = ‘dbname‘;$user = ‘user‘;$passwd = ‘passwd‘;$conn = new PDO("pgsql:dbname=$dbname;host=$host",$user,$passwd);return $conn;}function create_index(){//Elastic search php client$client = new Elasticsearch\Client();$sql = "SELECT * FROM log";$conn = get_conn();$stmt = $conn->query($sql);$rtn = $stmt->fetchAll();//delete index which already created$params = array();$params[‘index‘] = ‘log_index‘;$client->indices()->delete($params);//create index on log_date,src_ip,dest_ip$rtnCount = count($rtn);for($i=0;$i<$rtnCount;$i++){$params = array();$params[‘body‘] = array(‘log_date‘ => $rtn[$i][‘log_date‘],‘src_ip‘ => $rtn[$i][‘src_ip‘],‘dest_ip‘ => $rtn[$i][‘dest_ip‘]);$params[‘index‘] = ‘log_index‘;$params[‘type‘] = ‘log_type‘;//Document will be indexed to log_index/log_type/autogenerate_id$client->index($params);}echo ‘create index done!‘;}function search(){//Elastic search php client$client = new Elasticsearch\Client();$params = array();$params[‘index‘] = ‘log_index‘;$params[‘type‘] = ‘log_type‘;$params[‘body‘][‘query‘][‘match‘][‘src_ip‘] = ‘1.122.33.141‘;$rtn = $client->search($params);var_dump($rtn);}set_time_limit(0);//create_index();search();?>

 

建立索引成功,可以看到“create index done!”

查詢成功,可以看到返回的結果數組。

PHP Elasticsearch的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.