创建一个索引
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index'
];
$response = $client->indices()->create($params);
指定参数创建索引
$params = [
'index' => 'my_index2', // 索引名
'body' => [
'settings' => [ // 分片和副本数
'number_of_shards' => 3, // 分片
'number_of_replicas' => 0 // 副本,如果只有一台机器,设置为0
],
'mappings' => [ // 映射
'my_type' => [ // 类型
'_source' => [
'enabled' => true // 开启即可,否则某些功能不可用
],
'properties' => [ // 指定字段的类型或字段属性
'first_name' => [ // 字段
'type' => 'text', // 数据类型
'analyzer' => 'standard' // 分析器
],
'age' => [ // 同上
'type' => 'integer'
]
]
]
]
]
];
索引名
:类似mysql的库
分片
:类似mysql的表分区
副本
:类似mysql的从库(备库)
映射
:类似mysql的字段数据类型,类型说明
类型
:类似mysql的表
_source
:说明
字段
:类似mysql的字段
数据类型
:类似mysql的数据类型
分析器
:分词器
查看映射信息
<?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'
];
$response = $client->indices()->getMapping($params);
var_dump($response);
查看多个索引映射信息
$params = [
'index' => ['my_index', 'my_index2']
];
查看索引信息
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => ['my_index', 'my_index2']
];
$response = $client->indices()->getSettings($params);
var_dump($response);
修改索引
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_replicas' => 0
]
]
];
$response = $client->indices()->putSettings($params);
var_dump($response);
修改前
修改后
删除索引
<?php
require 'vendor/autoload.php';
$hosts = [
'192.168.247.140:9200' // ip和端口
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = ['index' => 'my_index'];
$response = $client->indices()->delete($params);