Elasticsearch全文搜索引擎-PHP使用教程。

一、聲明依賴關係:

        比方說,你的項目中須要一個php版的elasticsearch框架。爲了將它添加到你的項目中(下載),你所須要作的就是建立一個 composer.json 文件,其中描述了項目的依賴關係。注意文件要放在你執行composer命令的目錄中php

1
2
3
4
5
{
     "require" :{
         "elasticsearch/elasticsearch" : "~2.0"
     }
}

二、cmd切換到要下載elasticsearch框架的目錄,而後執行命令:composer installhtml

     若有出錯誤信息:mysql

      [Composer\Downloader\TransportException]sql

      Content-Length mismatch, received 583439 bytes out of the expected 1215108編程

     解決辦法:切換國內鏡像地址,再執行操做json

      一、經過命令切換以下:(最終修改的是composer.json)api

      composer config -g repo.packagist composer https://packagist.phpcomposer.comapp

      二、直接修改 composer.json (其實跟方法1是同樣的。)composer

1
2
3
4
5
6
7
8
9
10
11
{
     "require" :{
         "elasticsearch/elasticsearch" : "~2.0"
     },
     "repositories" : {
         "packagist" : {
             "type" "composer" ,
             "url" "https://packagist.phpcomposer.com"
         }
     }
}

001.png

 

 

PHP使用elasticsearch教程

 

下面咱們講一下基本使用方法,須要獲取更多使用教程和方法請看官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_overview.html框架

 

想直接經過文件查看其餘方法能夠打開如下文件查看(基本使用的方法都在裏面):

一、\elasticsearch\src\Elasticsearch\Client.php中的方法

二、\elasticsearch\Namespaces\IndicesNamespace.php中的方法

 

 

ThinkPHP中的模型(已測試過):

1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
  * Elasticsearch檢索引擎模型
  */
namespace  app\index\model;
use  Elasticsearch\ClientBuilder;  
   
class  Elasticsearch
{
     //配置
     private  $config  = [
         'hosts'  => [ 'http://127.0.0.1:9200' ]
     ];
     private  $api ;
     public  function  __construct()
     {
         # include (APP_PATH . '/vendor/autoload.php' );
         # require_once  EXTEND_PATH .  'org/elasticsearch/autoload.php' ;
         import( 'org.elasticsearch.autoload' , EXTEND_PATH);
         $this ->api = ClientBuilder::create()->setHosts( $this ->config[ 'hosts' ])->build(); 
     }
 
     /*************************************************************
     /**
      * 索引一個文檔
      * 說明:索引沒有被建立時會自動建立索引
      */
     public  function  addOne()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'id' ]  =  '20180407001' ;  # 不指定就是es自動分配
         $params [ 'body' ]  =  array ( 'name'  =>  '小川編程' );  
         return  $this ->api->index( $params );
     }
 
     /**
      * 索引多個文檔
      * 說明:索引沒有被建立時會自動建立索引
      */
     public  function  addAll()
     {
         $params  = [];
         for ( $i  = 1;  $i  < 21;  $i ++) {  
             $params [ 'body' ][] = [
                 'index'  => [
                     '_index'  =>  'test_index' . $i ,
                     '_type'   =>  'cat_test' ,
                     '_id'     =>  $i ,
                 ]
             ];  
             $params [ 'body' ][] = [  
                 'name'  =>  '小川編程' . $i ,  
                 'content'  =>  '內容' . $i  
             ];
         }  
         return  $this ->api->bulk( $params );  
     }
 
     /**
      * 獲取一個文檔
      */
     public  function  getOne()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'id' ]    =  '20180407001' ;  
         return  $this ->api->get( $params ); 
     }
 
     /**
      * 搜索文檔
      */
     public  function  search()
     {
         $params  = [];
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'body' ][ 'query' ][ 'match' ][ 'name' ] =  '小川編程' ;  
         return  $this ->api->search( $params ); 
     }
 
     /**
      * 刪除文檔
      * 說明:文檔刪除後,不會刪除對應索引。
      */
     public  function  delete ()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ] =  'cat' ;  
         $params [ 'id' ] =  '20180407001' ;  
         return  $this ->api-> delete ( $params );  
     }
 
     /*************************************************************
     /**
      * 建立索引
      */
     public  function  createIndex()
     {
         $params  = [];
         $params [ 'index' ]  =  'xiaochuan'
         return  $this ->api->indices()->create( $params );  
     }
       
       /**
      * 刪除索引:匹配單個 | 匹配多個
      * 說明: 索引刪除後,索引下的全部文檔也會被刪除
      */
       public  function  deleteIndex()
       {  
           $params  = [];
           $params [ 'index' ] =  'test_index' ;  # 刪除test_index單個索引
           # $params [ 'index' ] =  'test_index*' ; # 刪除以test_index開始的全部索引
         return  $this ->api->indices()-> delete ( $params );  
       }
 
       /*************************************************************
       /**
      * 設置索引配置
      */
       public  function  setIndexConfig()
       {  
           $params  = [];
           $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'body' ][ 'index' ][ 'number_of_replicas' ] = 0;  
         $params [ 'body' ][ 'index' ][ 'refresh_interval' ] = -1;  
         return  $this ->api->indices()->putSettings( $params );  
       }
 
       /**
      * 獲取索引配置
      */
       public  function  getIndexConfig()
       {
           # 單個獲取條件寫法
         $params [ 'index' ] =  'xiaochuan' ;  
         # 多個獲取條件寫法
         //$params['index'] = ['xiaochuan', 'test_index'];  
         return  $this ->api->indices()->getSettings( $params );  
       }
 
     /**
      * 設置索引映射
      */
       public  function  setIndexMapping()
       {
           #  設置索引和類型 
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
           
         #  向現有索引添加新類型
         $myTypeMapping  array (  
             '_source'  =>  array (  
                 'enabled'  => true  
             ),  
             'properties'  =>  array (  
                 'first_name'  =>  array (  
                     'type'  =>  'string' ,  
                     'analyzer'  =>  'standard'  
                 ),  
                 'age'  =>  array (  
                     'type'  =>  'integer'  
                 )  
             )  
         );  
         $params [ 'body' ][ 'cat' ] =  $myTypeMapping ;  
           
         #  更新索引映射 
         $this ->api->indices()->putMapping( $params );  
       }
 
       /**
      * 獲取索引映射
      */
       public  function  getIndexMapping()
       {  
           #  獲取全部索引和類型的映射  
         $ret  $this ->api->indices()->getMapping();  
         
         /*  
         #  獲取索引爲:xiaochuan的映射
         $params['index'] = 'xiaochuan';  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取類型爲:cat的映射
         $params['type'] = 'cat';  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取(索引爲:xiaochuan和 類型爲:cat)的映射
         $params['index'] = 'xiaochuan';  
         $params['type']  = 'cat'  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取索引爲:xiaochuan和test_index的映射
         $params['index'] = ['xiaochuan', 'test_index'];  
         $ret = $this->api->indices()->getMapping($params); 
         */
 
         return  $ret ;
       }
 
 
}

 

其餘形式用法測試:

test.php

1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php   
require_once ( 'vendor/autoload.php' );  
use  Elasticsearch\ClientBuilder;  
   
   
function  get_conn(){  
     $host  'localhost' ;  
     $dbname  'mraz' ;  
     $user  'root' ;  
     $passwd  '111111' ;  
   
   
     $conn  new  PDO( "mysql:dbname=$dbname;host=$host" , $user , $passwd );  
     return  $conn ;  
}  
   
   
function  create_index(){  
     //Elastic search php client  
   
   
   
   
     $client  = Elasticsearch\ClientBuilder::create()->build();  
     $sql     "SELECT * FROM emp" ;  
     $conn    = get_conn();  
     $stmt    $conn ->query( $sql );  
     $rtn     $stmt ->fetchAll();  
   
   
     //delete index which already created  
     $params  array ();  
     $params [ 'index' ] =  'emp_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 (  
             'id'        =>  $rtn [ $i ][ 'id' ],  
             'fdName'    =>  $rtn [ $i ][ 'fdName' ],  
             'fdAge'     =>  $rtn [ $i ][ 'fdAge' ],  
             'fdStatus'  =>  $rtn [ $i ][ 'fdStatus' ]  
         );  
         $params [ 'index' ] =  'emp_index' ;  
         $params [ 'type' ]  =  'emp_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  = Elasticsearch\ClientBuilder::create()->build();  
     $params  array ();  
     $params [ 'index' ] =  'emp_index' ;  
     $params [ 'type' ] =  'emp_type' ;  
     $params [ 'body' ][ 'query' ][ 'match' ][ 'fdStatus' ] =  '1' ;  
     $params [ 'body' ][ 'sort' ] =  array ( 'fdAge' => array ( 'order' => 'desc' ));  
     $params [ 'size' ] = 3;    
     $params [ 'from' ] = 1;    
     $rtn  $client ->search( $params );  
     var_dump( $rtn );  
}  
   
   
set_time_limit(0);  
// create_index();  
search();  
?>

 

1)建立:

1
2
3
4
5
6
7
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' ;   //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$data [ 'body' ][ 'settings' ][ 'number_of_shards' ] = 5;   //主分片數量  
$data [ 'body' ][ 'settings' ][ 'number_of_replicas' ] = 0;  //從分片數量  
$elastic ->indices()->create( $index );

2)插入索引數據:

1
2
3
4
5
6
7
8
9
10
11
12
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'id' ] = 1    //不指定id,系統會自動生成惟一id  
$index [ 'body' ] =  array (  
   'mac'  =>  'fcd5d900beca' ,  
   'customer_id'  => 3,  
   'product_id'  => 5,  
   'version'  => 2  
);  
$elastic ->index( $index );

3)查詢:

1
2
3
4
5
6
7
8
9
10
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'match' ][ 'mac' ] =  'fcd5d900beca' ;  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'must' ] =  array (  
     array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
     array ( 'match'  =>  array ( 'product_id'  => 20))  
    );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  and  product_id=20 limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'should' ] =  array (  
       array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
       array ( 'match'  =>  array ( 'product_id'  => 20))  
      );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#當於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  or  product_id=20 limit 200,10;

Elasticsearch全文搜索引擎-PHP使用教程。

 小川 2018-07-07  PHP

用Composer來生成php版的elasticsearch框架

 

若是你尚未安裝Composer的話請看:Composer安裝教程文章

 

一、聲明依賴關係:

        比方說,你的項目中須要一個php版的elasticsearch框架。爲了將它添加到你的項目中(下載),你所須要作的就是建立一個 composer.json 文件,其中描述了項目的依賴關係。注意文件要放在你執行composer命令的目錄中

1
2
3
4
5
{
     "require" :{
         "elasticsearch/elasticsearch" : "~2.0"
     }
}

二、cmd切換到要下載elasticsearch框架的目錄,而後執行命令:composer install

     若有出錯誤信息:

      [Composer\Downloader\TransportException]

      Content-Length mismatch, received 583439 bytes out of the expected 1215108

     解決辦法:切換國內鏡像地址,再執行操做

      一、經過命令切換以下:(最終修改的是composer.json)

      composer config -g repo.packagist composer https://packagist.phpcomposer.com

      二、直接修改 composer.json (其實跟方法1是同樣的。)

1
2
3
4
5
6
7
8
9
10
11
{
     "require" :{
         "elasticsearch/elasticsearch" : "~2.0"
     },
     "repositories" : {
         "packagist" : {
             "type" "composer" ,
             "url" "https://packagist.phpcomposer.com"
         }
     }
}

001.png

 

 

PHP使用elasticsearch教程

 

下面咱們講一下基本使用方法,須要獲取更多使用教程和方法請看官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_overview.html

 

想直接經過文件查看其餘方法能夠打開如下文件查看(基本使用的方法都在裏面):

一、\elasticsearch\src\Elasticsearch\Client.php中的方法

二、\elasticsearch\Namespaces\IndicesNamespace.php中的方法

 

 

ThinkPHP中的模型(已測試過):

1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
  * Elasticsearch檢索引擎模型
  */
namespace  app\index\model;
use  Elasticsearch\ClientBuilder;  
   
class  Elasticsearch
{
     //配置
     private  $config  = [
         'hosts'  => [ 'http://127.0.0.1:9200' ]
     ];
     private  $api ;
     public  function  __construct()
     {
         # include (APP_PATH . '/vendor/autoload.php' );
         # require_once  EXTEND_PATH .  'org/elasticsearch/autoload.php' ;
         import( 'org.elasticsearch.autoload' , EXTEND_PATH);
         $this ->api = ClientBuilder::create()->setHosts( $this ->config[ 'hosts' ])->build(); 
     }
 
     /*************************************************************
     /**
      * 索引一個文檔
      * 說明:索引沒有被建立時會自動建立索引
      */
     public  function  addOne()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'id' ]  =  '20180407001' ;  # 不指定就是es自動分配
         $params [ 'body' ]  =  array ( 'name'  =>  '小川編程' );  
         return  $this ->api->index( $params );
     }
 
     /**
      * 索引多個文檔
      * 說明:索引沒有被建立時會自動建立索引
      */
     public  function  addAll()
     {
         $params  = [];
         for ( $i  = 1;  $i  < 21;  $i ++) {  
             $params [ 'body' ][] = [
                 'index'  => [
                     '_index'  =>  'test_index' . $i ,
                     '_type'   =>  'cat_test' ,
                     '_id'     =>  $i ,
                 ]
             ];  
             $params [ 'body' ][] = [  
                 'name'  =>  '小川編程' . $i ,  
                 'content'  =>  '內容' . $i  
             ];
         }  
         return  $this ->api->bulk( $params );  
     }
 
     /**
      * 獲取一個文檔
      */
     public  function  getOne()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'id' ]    =  '20180407001' ;  
         return  $this ->api->get( $params ); 
     }
 
     /**
      * 搜索文檔
      */
     public  function  search()
     {
         $params  = [];
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
         $params [ 'body' ][ 'query' ][ 'match' ][ 'name' ] =  '小川編程' ;  
         return  $this ->api->search( $params ); 
     }
 
     /**
      * 刪除文檔
      * 說明:文檔刪除後,不會刪除對應索引。
      */
     public  function  delete ()
     {
         $params  = [];  
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ] =  'cat' ;  
         $params [ 'id' ] =  '20180407001' ;  
         return  $this ->api-> delete ( $params );  
     }
 
     /*************************************************************
     /**
      * 建立索引
      */
     public  function  createIndex()
     {
         $params  = [];
         $params [ 'index' ]  =  'xiaochuan'
         return  $this ->api->indices()->create( $params );  
     }
       
       /**
      * 刪除索引:匹配單個 | 匹配多個
      * 說明: 索引刪除後,索引下的全部文檔也會被刪除
      */
       public  function  deleteIndex()
       {  
           $params  = [];
           $params [ 'index' ] =  'test_index' ;  # 刪除test_index單個索引
           # $params [ 'index' ] =  'test_index*' ; # 刪除以test_index開始的全部索引
         return  $this ->api->indices()-> delete ( $params );  
       }
 
       /*************************************************************
       /**
      * 設置索引配置
      */
       public  function  setIndexConfig()
       {  
           $params  = [];
           $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'body' ][ 'index' ][ 'number_of_replicas' ] = 0;  
         $params [ 'body' ][ 'index' ][ 'refresh_interval' ] = -1;  
         return  $this ->api->indices()->putSettings( $params );  
       }
 
       /**
      * 獲取索引配置
      */
       public  function  getIndexConfig()
       {
           # 單個獲取條件寫法
         $params [ 'index' ] =  'xiaochuan' ;  
         # 多個獲取條件寫法
         //$params['index'] = ['xiaochuan', 'test_index'];  
         return  $this ->api->indices()->getSettings( $params );  
       }
 
     /**
      * 設置索引映射
      */
       public  function  setIndexMapping()
       {
           #  設置索引和類型 
         $params [ 'index' ] =  'xiaochuan' ;  
         $params [ 'type' ]  =  'cat' ;  
           
         #  向現有索引添加新類型
         $myTypeMapping  array (  
             '_source'  =>  array (  
                 'enabled'  => true  
             ),  
             'properties'  =>  array (  
                 'first_name'  =>  array (  
                     'type'  =>  'string' ,  
                     'analyzer'  =>  'standard'  
                 ),  
                 'age'  =>  array (  
                     'type'  =>  'integer'  
                 )  
             )  
         );  
         $params [ 'body' ][ 'cat' ] =  $myTypeMapping ;  
           
         #  更新索引映射 
         $this ->api->indices()->putMapping( $params );  
       }
 
       /**
      * 獲取索引映射
      */
       public  function  getIndexMapping()
       {  
           #  獲取全部索引和類型的映射  
         $ret  $this ->api->indices()->getMapping();  
         
         /*  
         #  獲取索引爲:xiaochuan的映射
         $params['index'] = 'xiaochuan';  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取類型爲:cat的映射
         $params['type'] = 'cat';  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取(索引爲:xiaochuan和 類型爲:cat)的映射
         $params['index'] = 'xiaochuan';  
         $params['type']  = 'cat'  
         $ret = $this->api->indices()->getMapping($params);  
           
         #  獲取索引爲:xiaochuan和test_index的映射
         $params['index'] = ['xiaochuan', 'test_index'];  
         $ret = $this->api->indices()->getMapping($params); 
         */
 
         return  $ret ;
       }
 
 
}

 

其餘形式用法測試:

test.php

1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php   
require_once ( 'vendor/autoload.php' );  
use  Elasticsearch\ClientBuilder;  
   
   
function  get_conn(){  
     $host  'localhost' ;  
     $dbname  'mraz' ;  
     $user  'root' ;  
     $passwd  '111111' ;  
   
   
     $conn  new  PDO( "mysql:dbname=$dbname;host=$host" , $user , $passwd );  
     return  $conn ;  
}  
   
   
function  create_index(){  
     //Elastic search php client  
   
   
   
   
     $client  = Elasticsearch\ClientBuilder::create()->build();  
     $sql     "SELECT * FROM emp" ;  
     $conn    = get_conn();  
     $stmt    $conn ->query( $sql );  
     $rtn     $stmt ->fetchAll();  
   
   
     //delete index which already created  
     $params  array ();  
     $params [ 'index' ] =  'emp_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 (  
             'id'        =>  $rtn [ $i ][ 'id' ],  
             'fdName'    =>  $rtn [ $i ][ 'fdName' ],  
             'fdAge'     =>  $rtn [ $i ][ 'fdAge' ],  
             'fdStatus'  =>  $rtn [ $i ][ 'fdStatus' ]  
         );  
         $params [ 'index' ] =  'emp_index' ;  
         $params [ 'type' ]  =  'emp_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  = Elasticsearch\ClientBuilder::create()->build();  
     $params  array ();  
     $params [ 'index' ] =  'emp_index' ;  
     $params [ 'type' ] =  'emp_type' ;  
     $params [ 'body' ][ 'query' ][ 'match' ][ 'fdStatus' ] =  '1' ;  
     $params [ 'body' ][ 'sort' ] =  array ( 'fdAge' => array ( 'order' => 'desc' ));  
     $params [ 'size' ] = 3;    
     $params [ 'from' ] = 1;    
     $rtn  $client ->search( $params );  
     var_dump( $rtn );  
}  
   
   
set_time_limit(0);  
// create_index();  
search();  
?>

 

1)建立:

1
2
3
4
5
6
7
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' ;   //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$data [ 'body' ][ 'settings' ][ 'number_of_shards' ] = 5;   //主分片數量  
$data [ 'body' ][ 'settings' ][ 'number_of_replicas' ] = 0;  //從分片數量  
$elastic ->indices()->create( $index );

2)插入索引數據:

1
2
3
4
5
6
7
8
9
10
11
12
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'id' ] = 1    //不指定id,系統會自動生成惟一id  
$index [ 'body' ] =  array (  
   'mac'  =>  'fcd5d900beca' ,  
   'customer_id'  => 3,  
   'product_id'  => 5,  
   'version'  => 2  
);  
$elastic ->index( $index );

3)查詢:

1
2
3
4
5
6
7
8
9
10
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'match' ][ 'mac' ] =  'fcd5d900beca' ;  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'must' ] =  array (  
     array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
     array ( 'match'  =>  array ( 'product_id'  => 20))  
    );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  and  product_id=20 limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'should' ] =  array (  
       array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
       array ( 'match'  =>  array ( 'product_id'  => 20))  
      );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#當於sql語句:select*from ems_run_log where mac= 'fcd5d900beca'  or  product_id=20 limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'must_not' ] =  array (  
    array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
    array ( 'match'  =>  array ( 'product_id'  => 20))  
   );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac!= 'fcd5d900beca'  and  product_id!=20 limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'range' ] =  array (  
    'id'  =>  array ( 'gte'  => 20, 'lt'  => 30);  
  );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where id>=20  and  id<30  limit 200,10;

4)刪除文檔:

1
2
3
4
5
6
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'test' ;   //索引名稱  
$index [ 'type' ] =  'ems_test' //類型名稱  
$index [ 'id' ] = 2;   
$elastic -> delete ( $index );
1
2
3
4
5
6
7
8
9
10
11
12
13
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'bool' ][ 'must_not' ] =  array (  
    array ( 'match'  =>  array ( 'mac'  =>  'fcd5d900beca' )),  
    array ( 'match'  =>  array ( 'product_id'  => 20))  
   );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where mac!= 'fcd5d900beca'  and  product_id!=20 limit 200,10;
1
2
3
4
5
6
7
8
9
10
11
12
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'log' //索引名稱  
$index [ 'type' ] =  'ems_run_log' //類型名稱  
$index [ 'body' ][ 'query' ][ 'range' ] =  array (  
    'id'  =>  array ( 'gte'  => 20, 'lt'  => 30);  
  );  
$index [ 'size' ] = 10;  
$index [ 'from' ] = 200;  
$elastic ->search( $index );  
 
#至關於sql語句:select*from ems_run_log where id>=20  and  id<30  limit 200,10;

4)刪除文檔:

1
2
3
4
5
6
include ( './vendor/autoload.php' );  
$elastic  new  Elasticsearch\Client();  
$index [ 'index' ] =  'test' ;   //索引名稱  
$index [ 'type' ] =  'ems_test' //類型名稱  
$index [ 'id' ] = 2;   
$elastic -> delete ( $index );
相關文章
相關標籤/搜索