docker安裝elasticsearch和使用es
阿新 • • 發佈:2021-12-02
安裝elasticsearch
- docker pull elasticsearch:7.4.2
- docker run -d --name es3 -p 9202:9202 -p 9302:9302 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -v d:/es/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml c9c606d302fb
安裝Kibana
- docker pull elasticsearch:7.2.0
- docker run -d --name kibana -p 5601:5601 -e ELASTICSEARCH_HOSTS=http://192.168.56.1:9200 kibana:7.4.2
操作ES
- 建立索引和對映
$hosts = [ '127.0.0.1:9200', //IP+埠 ]; $client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build(); $params = [ 'index' => 'users', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2 ], 'mappings' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'name' => [ 'type' => 'keyword' ], 'age' => [ 'type' => 'integer' ], 'mobile' => [ 'type' => 'text' ], 'email' => [ 'type' => 'text' ], 'birthday' => [ 'type' => 'date' ], 'address' => [ 'type' => 'text' ] ] ] ] ]; $response = $client->indices()->create($params); print_r($response);
- 單一索引
$params = [
'index' => 'users',
'id' => 1,
'body' => [
'name' => '張三',
'age' => 10,
'email' => '[email protected]',
'birthday' => '1990-12-12',
'address' => '北京'
]
];
$client->index($params);
- 獲取文件
$params = [
'index' => 'users',
'id' => 1
];
$response = $client->get($params);
- 更新文件
$params = [
'index' => 'users',
'id' => 1,
'body' => [
'doc' => [
'address' => '測試'
]
]
];
$response = $client->update($params);
print_r($response);
- 刪除文件
$params = [
'index' => 'users',
'id' => 1,
];
$response = $client->delete($params);
print_r($response);