1、声明依赖关系:

        比方说,你的项目中需要一个php版的elasticsearch框架。为了将它添加到你的项目中(下载),你所需要做的就是创建一个 composer.json 文件,其中描述了项目的依赖关系。注意文件要放在你执行composer命令的目录中

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

2、cmd切换到要下载elasticsearch框架的目录,然后执行命令:composer install

如有出错误信息:

[Composer\Downloader\TransportException]

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

解决办法:切换国内镜像地址,再执行操作

1、通过命令切换如下:(最终修改的是composer.json)

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

2、直接修改 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"
        }
    }
}

PHP使用elasticsearch教程

下面我们讲一下基本使用方法,需要获取更多使用教程和方法请看官方文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_overview.html

想直接通过文件查看其他方法可以打开以下文件查看(基本使用的方法都在里面):

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

2、\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;

用Composer来生成php版的elasticsearch框架

如果你还没有安装Composer的话请看:Composer安装教程文章

1、声明依赖关系:

        比方说,你的项目中需要一个php版的elasticsearch框架。为了将它添加到你的项目中(下载),你所需要做的就是创建一个 composer.json 文件,其中描述了项目的依赖关系。注意文件要放在你执行composer命令的目录中

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

2、cmd切换到要下载elasticsearch框架的目录,然后执行命令:composer install

如有出错误信息:

[Composer\Downloader\TransportException]

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

解决办法:切换国内镜像地址,再执行操作

1、通过命令切换如下:(最终修改的是composer.json)

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

2、直接修改 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"
        }
    }
}

PHP使用elasticsearch教程

下面我们讲一下基本使用方法,需要获取更多使用教程和方法请看官方文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_overview.html

想直接通过文件查看其他方法可以打开以下文件查看(基本使用的方法都在里面):

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

2、\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);

Elasticsearch全文搜索引擎-PHP使用教程。的更多相关文章

  1. 【Elasticsearch全文搜索引擎实战】之Head插件实践

    简介 Elasticsearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Ap ...

  2. ElasticSearch全文搜索引擎

    一.ElasticSearch简介 1.1 什么是ElasticSearch ElasticSearch简称ES,其中Elastic      从名字里我们可以知道,ES的特点就在于灵活的搜索,其实E ...

  3. net core 3.1使用ElasticSearch 全文搜索引擎

    ElasticSearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene 基础之上. Lucene 可以说是当下最先进.高性能.全功能的搜索引擎库,无论是开源还是私有. ...

  4. ElasticSearch全文搜索引擎(A)

    文章:[Elasticsearch] 全文搜索 (一) - 基础概念和match查询 全文检索,是从最初的字符串匹配和简单的布尔逻辑检索技术,演进到能对超大文本.语音.图像.活动影像等非结构化数据进行 ...

  5. 【Elasticsearch全文搜索引擎实战】之Kibana搭建

    1. Kibana介绍 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据.使用Kibana,可以通过各种图表进行高 ...

  6. 【Elasticsearch全文搜索引擎实战】之集群搭建及配置

    文中Elasticsearch版本为6.0.1 1. 环境配置 把环境配置放在第一节来讲,是因为很多人按官网的Getting Started安装运行会有各种错误.其实都是因为一些配置不正确引起的. 首 ...

  7. 3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建

    高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建 如果大家看了我的上一篇<2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离>文章,如果能很好的 ...

  8. 全文搜索引擎 Elasticsearch 入门教程

    全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选. 它可以快速地储存.搜索和分析海量数据.维基百科.Stack Overflow.Gi ...

  9. 全文搜索引擎Elasticsearch入门实践

    全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...

随机推荐

  1. 「CF1037D」Valid BFS?

    传送门 Luogu 解题思路 考虑直接模拟 \(\text{BFS}\) 的过程. 对于每一个节点的儿子,先遍历在输入序列中靠前的,判断 \(\text{BFS}\) 是否匹配即可. 细节注意事项 注 ...

  2. jetson nano 安装 snowboy 遇到的问题及处理

    Snowboy 是 KITT.AI 开发的一个高度可定制的热词检测引擎,当笔者的 jetson nano 加上话筒后,就立马尝试安装,但在安装过程中却发生了错误,所以把处理方式记录了下来以作备忘. 首 ...

  3. 高手教大家如何配置JVM参数

    /usr/local/jdk/bin/java -Dresin.home=/usr/local/resin -server -Xms1800M -Xmx1800M -Xmn300M -Xss512K ...

  4. LibreOJ #6001. 「网络流 24 题」太空飞行计划

    \(\quad\) 与网络流有关的最值有三个:最大流,最小费用,最小割.这道题是最小割.想了好久,终于想明白最小割应该怎么用. \(\quad\) 先找出矛盾的事物.在这道题中,两件事是矛盾的:做实验 ...

  5. Linux CentOS7 VMware 环境变量PATH、cp命令、mv命令、文档查看cat/more/less/head/tail——笔记

    一.环境变量PATH PATH一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径. 命令在这几个目录里面就不需要敲绝对路径 echo $PATH 例子:把/tmp/ 加到 $PA ...

  6. Calendar 时间类的应用

    Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calendar 类进行时间和日期的处 ...

  7. 「CF161B」Discounts

    传送门 Luogu 解题思路 贪心地想一想,我们肯定要让凳子去给价格越高的商品打半价,那么我们就先按照价格排序,但是要优先把凳子排在前面. 然后我们发现一条凳子肯定只能给价格小于等于它本身的物品打半价 ...

  8. IdentityServer4专题之一:OAuth2.0介绍

    1.OAuth 2.0授权方式介绍: OAuth 2.0 的标准是 RFC 6749 文件.该文件先解释了 OAuth 是什么: OAuth 引入了一个授权层,用来分离两种不同的角色:客户端和资源所有 ...

  9. Linux学习计划(一)

    一.用途:网络服务器 二.优点: 1.开源免费 2.良好的可移植性 3.安全性 三.安装Linux 工具:VMware workstation .centOS7 安装步骤 图片加载中... 说明: Ⅰ ...

  10. I2S 总线学习:2-I2S驱动WM8978

    背景 为了了解I2S总线所对应的硬件设计,下文转载了<STM32:I2S驱动WM8978>. 以加深对I2S总线的了解. 正文 最近项目中使用STM32F4驱动音频IC:WM8978. 由 ...