新增
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'id' => '1', // 如果不提供id,es会自动生成
'body' => ['first_name' => 'abc']
];
$response = $client->index($params);
多字段
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'body' => ['first_name' => 'abc', 'age' => '100']
];
查询
id的方式
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'id' => '1'
];
$response = $client->get($params);// /my_index2/my_type/1
var_dump($response);
字段的方式
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'first_name' => 'abc'
]
]
]
];
$results = $client->search($params);
var_dump($results);
使用json
$json = '{
"query" : {
"match" : {
"first_name" : "abc"
}
}
}';
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'body' => $json
];
$results = $client->search($params);
var_dump($results);
bool查询
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'first_name' => 'abc' ] ],
[ 'match' => [ 'age' => '100' ] ],
]
]
]
]
];
$results = $client->search($params);
var_dump($results);
这里类似mysql的and
。
更新
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'id' => '1',
'body' => [
'doc' => [
'first_name' => 'abc2',
'age' => '123'
]
]
];
$response = $client->update($params);
删除
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index2',
'type' => 'my_type',
'id' => '1'
];
$response = $client->delete($params);