V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
fangwenxue
V2EX  ›  问与答

PHP es 批量作问题

  •  
  •   fangwenxue · 2022-10-25 18:45:45 +08:00 · 708 次点击
    这是一个创建于 521 天前的主题,其中的信息可能已经有所发展或是发生改变。
        public function batchCreateDoc(array $items, $index_name) {
            $params = ['body' => []];
            foreach ($items as $item) {
                $id = getValue($item, 'id');
                if (!$id) {
                    throw new \RuntimeException('批量更新必须包含 id 属性');
                }
                $params['body'][] = [
                    'index' => [
                        '_index' => $index_name,
                        '_type' => $index_name,
                        '_id' => $id
                    ]
                ];
                $params['body'][] = $item;
            }
            return $this->client->bulk($params);
        }
    
    $data = $es->batchCreateDoc([
    ['id' => 'b2fd0dc6e0b0857422d6b5ad3dddd61411', 'status' => 1]
    ], 'xxxx');
    print_r($data);
    
    • 更新完后 id=b2fd0dc6e0b0857422d6b5ad3dddd61411 的文档只有 status 字段 这 Id 是存在的,非创建
    • 更新前有 N 个字段都没有了,这是哪问题
    2 条回复    2022-10-25 22:15:20 +08:00
    zhuangjia
        1
    zhuangjia  
       2022-10-25 19:55:16 +08:00
    看了下文档: https://www.elastic.co/guide/en/elasticsearch/reference/6.7/docs-bulk.html
    提示如下:
    i.e. create will fail if a document with the same index and type exists already, whereas index will add or replace a document as necessary
    存在的时候是 [替换] 而不是 [更新]
    zzl22100048
        2
    zzl22100048  
       2022-10-25 22:15:20 +08:00 via iPhone
    你需要 update ,index 会覆盖
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2889 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:22 · PVG 22:22 · LAX 07:22 · JFK 10:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.