創建一個例子索引的mapping如下:
$mapping = [
'index' => 'my_index',
'body' => [
'mappings' => [
'my_type' => [
'_all' => [
'enabled' => 'false'
],
'_routing' => [ #設置routing(分片路由)
'required' => 'true'
],
'properties' => [
'name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->indices()->create($mapping);
批量執行創建文檔例子
$params['body'] = [];
$nameArr = ['趙','錢','孫','李','周','吳','鄭','王'];
for ($i = 1; $i <= 100; $i++) {
$params['body'][] = [
'create' => [ #創建
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => $i,
'_routing' => mt_rand(1,100),
]
];
$params['body'][] = [
'name' => $nameArr[mt_rand(0,7)],
'age' => mt_rand(18,60)
];
}
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->bulk($params);
批量執行更新文檔操作例子
//查找指定文檔
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
'range' => [
'age' => [
'gt' => '25',
'lt' => '40'
]
]
]
]
],
'from' => 0,
'size' => 100,
'sort' => [
'age' => 'desc'
]
]
];
$info = $client->search($params);
$data = $info['hits']['hits'];
// 批量執行更新文檔
$params['body'] = [];
$nameArr = ['趙','錢','孫','李','周','吳','鄭','王'];
foreach ($data as $key => $value) {
$params['body'][] = [
'update' => [ #更新文檔
'_index' => $value['_index'],
'_type' => $value['_type'],
'_id' => $value['_id'],
'_routing' => $value['_routing'],
]
];
$params['body'][] = [
'doc' => [ #更新指定的字段值
'name' => $nameArr[mt_rand(0,7)].$nameArr[mt_rand(0,7)]
]
];
}
$res = $client->bulk($params);
增刪改批量執行操作例子
$params['body'] = [];
//創建或替換文檔操作
$params['body'][] = [
'index' => [ #創建或替換
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 1,
'_routing' => 1,
]
];
$params['body'][] = [
'name' => '楊',
'age' => 23
];
//創建文檔操作
$params['body'][] = [
'create' => [ #創建
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 2,
'_routing' => 2,
]
];
$params['body'][] = [
'name' => '郭',
'age' => 19
];
//局部更新文檔操作
$params['body'][] = [
'update' => [ #局部更新
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 3,
'_routing' => 3,
]
];
$params['body'][] = [
'doc' => [
'age' => 19
]
];
//刪除文檔操作
$params['body'][] = [
'delete' => [ #刪除
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 4,
'_routing' => 4,
]
];
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->bulk($params);