我是靠谱客的博主 优美蜻蜓,这篇文章主要介绍【elasticsearch】批量更新数据,现在分享给大家,希望可以做个参考。

软件版本:

  • php 5.6.31
  • elasticsearch 6.1.1
  • Yii 2 框架

elasticsearch 扩展:

复制代码
1
2
3
4
5
6
{ "require": { "elasticsearch/elasticsearch": "~5.0" } }

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function actionArticle() { $open_sites = Yii::$app->getDb()->createCommand( "select indicator from {{%site}} where status = 1 order by id asc" )->queryAll(); if (empty($open_sites)) { return 0; } foreach ($open_sites as $open_site) { $indicator = $open_site['indicator']; $articles = Yii::$app->getDb()->createCommand( "select id, heat from {{%article_{$indicator}}} order by id asc" )->queryAll(); if (empty($articles)) { continue; } $es_params = ['index' => $indicator, 'type' => 'doc']; $body = []; foreach ($articles as $article) { $article_id = $article['id']; $body[] = ['update' => ['_id' => "article_{$article_id}"]]; $body[] = ['doc' => ['heat' => $article['heat']]]; if (count($body) >= 2000) { $es_params['body'] = $body; // file_put_contents('es_bulk.log', print_r($es_params, true));die; Yii::$app->elasticsearch->client->bulk($es_params); $body = []; } } if (!empty($body)) { $es_params['body'] = $body; Yii::$app->elasticsearch->client->bulk($es_params); } } return 0; }

对应 elasticsearch 批量更新 api 的参数请求结构:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Array ( [index] => cn [type] => doc [body] => Array ( [0] => Array ( [update] => Array ( [_id] => article_1 ) ) [1] => Array ( [doc] => Array ( [heat] => 0.00 ) ) [2] => Array ( [update] => Array ( [_id] => article_2 ) ) [3] => Array ( [doc] => Array ( [heat] => 0.00 ) ) ... ) )

最后

以上就是优美蜻蜓最近收集整理的关于【elasticsearch】批量更新数据的全部内容,更多相关【elasticsearch】批量更新数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部