Elasticsearch PHP - Create or Update a document if it doesn’t exist

Posted 2 weeks ago

        $client = ClientBuilder::create()->build();

        $id = 123;

        $params = [];
        $params['index'] = self::INDEX;
        $params['type'] = self::TYPE;
        $params['id'] = $id;
        $params['body']["doc"]['data']['body-test'] = 'abc';
        $params['body']["doc_as_upsert"] = true;

        return $client->update($params);

The important part I was missing is the “doc_as_upsert” on the body.