安装

使用composer安装elasticsearch

# composer.json文件加入elasticsearch是哪个版本,就装哪个版本,比如我的elasticsearch5.3,我装的包就是"~5.3"
{ "require": { "elasticsearch/elasticsearch":"~5.3" } }

# composer 安装 执行命令:
 > composer update

测试

require("/vendor/elasticsearch/autoload.php");


$hosts = ["192.168.1.10'];


$client = Elasticsearch\ClientBuilder::create()->setHosts($host)->build();


$client = $this->getElasticClient();


$params = [
  "index"=>"website",
  "type" => "blog",
  "body" = >[
    "query"=
      >["match"=>["_id"=
      >1]
  ]]
];


$rtn = $client->search($params);


var_dump($rtn);

数据操作 1

索引内的一个文档的建立,类似mysql中添加记录

# SQL插入数据方式
INSERT INTO blog('title','content','created_at') VALUES('ES PHP', 'balabala...','2017-07-20')

ES实例化

require('/vendor/elasticsearch/autoload.php');


$hosts = ["192.168.1.10"];


$client = ElasticSearch\ClientBuilder::create()->setHosts($hosts)->build();

Index添加数据

// 添加数据
$params = [
  'index'=>'website',
  'type'=>'blog',
  'id'=>10,
  'body'=>[
    'title'=>'elasticsearch & php',
    'content'=>'balabala...',
    'created_at'= >'2017-08-01
  12:02:20']
];
$resp = $client->index($params);
var_dump($resp);

Get查询数据

// 查询数据 GET
$params = [
  'index' => 'website',
  'type' => 'blog',
  'id' = >1
];
try{
  $resp = $client->get($params);
}catch(Exception $e){
  $resp = $e->getMessage();
}

Search查询数据

//查询数据 Search
$params = [
  "index" => "website",
  "type" => "blog",
  "body" = >[
     "query" = >[
        "match" => ["title"= >"elasticsearch
    php"]
   ],]
];
try{
 $resp = $client->search($params);
}catch(Exception $e){
  $resp = $e->getMessage();
}

数据操作2

创建索引

$client = new Elasticsearch\Client();
// 设置索引名称
$index = ['index'=>'log', 'type'=>'syslog'];
// 设置分片数量
$data['body']['settings'] = ['number_of_shards'=>5, 'number_of_replicas'=>0];
// 创建索引
$client->indices()->create($index);

插入索引数据

// 设置索引名称
$index['index'] = 'log';//索引名称
$index['type'] = 'syslog';//类型名称
$index['id'] = 1;//系统自动生成唯一编号
$index['body'] = [
  'mac'=>'ajlfadlkajldf',
  'customer_id' => 3,
  'product_id' => 5,
  'version' = >2
];
$client->index($index);

查询数据1:match

$index['index'] = 'log';//设置索引名称
$index['type'] = 'syslog';//设置类型名
$index['body']['query']['match']['mac'] = 'test';
$index['size'] = 10;
$index['from'] = 200;
$client->search($index);


//相当于SQL语句
SELECT * FROM syslog WHERE mac='test' LIMIT 200,10;

查询数据2: must = and

$index['index'] = 'log';//设置索引
$index['type'] = 'syslog';//设置类型
$index['body']['query']['bool']['must'] = 
  [['match'=>['mac'=
  >'rest']],['match'=>['product_id'=>20]]
];
$index['size'] = 10;
$index['from'] = 200;
$client->search($index);
//相当于SQL语句
SELECT * FROM syslog WHERE mac='test' AND product_id=20 LIMIT 200,10

查询数据3: should = or

$index['index'] = 'log';//设置索引
$index['type'] = 'syslog';//设置类型
$index['body']['query']['bool']['should'] = 
  [['match'=>['mac'=
  >'test']],['match'=>['product_id'=>20]]
];
$index['size'] = 10;
$index['from'] = 200;
$client->search($index);
// 相当于sql语句
SELECT * FROM syslog WHERE mac='test' OR product_id=20 LIMIT 200,10

查询数据4:must_not

$index['index'] = 'log';
$index['type'] = 'syslog';
$index['body']['query']['bool']['must_not'] = 
  [['match'=>['mac'=
  >'test']],['match'=>['product_id'=>20]]
];
$index['size'] = 10;
$index['from'] = 100;
$client->search($index);


//相当于SQL语句
SELECT * FROM syslog WHERE mac='test' AND product_id!=20 LIMIT 200,10

查询数据5:range

$index['index'] = 'log';
$index['type'] = 'syslog';
$index['body']['query']['range'] = [
  'id' => ['gte'=>20, 'lt'=>30]
];
$index['size'] = 10;
$index['from'] = 200;
$client->search($index);
// 相当于sql语句
SELECT * FROM syslog WHERE id>=20 AND id<30 LIMIT 200,10

删除文档

$index['index'] = 'dbname';//设置索引
$index['type'] = 'tblname';//设置类型
$index['id'] = 2;
$client->delete($index);

将MySQL数据同步到ES

1. 下载安装 Elasticsearch-jdbc

ES-JDBC下载地址
https://github.com/jprante/elasticsearch-jdbc

# 查看 ES 版本
$ cd /usr/share/elasticsearch
$ ./elasticsearch -V
Version: 5.4.3, Build: eed30a8/2017-06-22T00:34:03.743Z, JVM: 1.8.0_131

由于本机ES5不能使用数据源ES-JDBC,故选择ES5+logstash的logstash-input-jdbc实现mysql数据同步。

1. 安装 logstash

logstash 官网
https://www.elastic.co/cn/products/logstash



作者:JunChow520
链接:http://www.jianshu.com/p/44441eab6852
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。