如果在更新

时间:2017-06-03 22:09:45

标签: php elasticsearch

我正在使用elasticsearch,我遇到的问题是有时用户没有被添加到索引中,不确定原因,有时只会发生。

1.我的设置是当您注册到我的网站时,用户会被添加到索引中 2.当用户更新他们的个人资料时,索引会使用他们的新信息

进行更新

3.当用户更新或编辑他们的个人资料时,如果他们没有被添加到索引中,则会抛出错误。

我希望能够将它们添加到索引中,如果它们在更新时尚未添加,换句话说,如果尚未添加到索引中,则在更新之前添加它们

这就是我正在更新的方式

$client = Clientbuilder::create()->build();
    $response = $client->update([
        'index' => 'users',
        'type' => 'user',
        'id' => Auth::user()->id,
        'body' => [
            'doc' => [
                $field => $data,
            ],
        ],

    ]);
}

然后

if ($request->has('name')) {
        $user->name = strip_tags($request->input('name'));
        $user->save();

        //update elasticsearch users index
        $user->elasticupdate('name',$user->name);

    }

我将它们添加到索引中就像这样

$client = Clientbuilder::create()->build();
        $response = $client->index([
            'index' => 'users',
            'type' => 'user',
            'id' => $user->id,
            'body' => [
                'username' => $user->username,
                'name' => $user->name,
                'profession' =>$user->getProfession(),
                'secondary_profession' =>$user->getSecondaryProfession(),
                'genre' => $user->getUserGenre(),
                'subgenre' => $user->getUserSubGenre(),
                'city' => $user->getCity(),
                'profilepic' => 'n026qso2xxztaphbwwey.png', //Default pics
                'backgroundpic' => 'backgrounddefaultpms_oa9mee.png', //Default pics
                // 'profilepic' => $user->getProfilePic(),
                // 'backbroundpic' => $user->getBackroundPic(),
                'country' => $user->country,
                'country_fips_code' => $user->country_fips_code,
                'state' => $user->getState(),
                //'affiliations' => $user->getAffiliations(),
                'affiliation_one' => ' ',
                'affiliation_two' => ' ',
                'affiliation_three' => ' ',
                'affiliation_four' => ' ',
                'affiliation_five' => ' ',
                'credential_one' => ' ',
                'credential_two' => ' ',
                'credential_three' => ' ',
                'credential_four' => ' ',
                'credential_five' => ' ',
                // 'countryAbr' => $countryAbr,
                // 'stateAbr' => $stateAbr,
                'cityGeonameId' => $user->geoname_id,
                'latlong' => [$origin],
            ],

        ]);

我想过只运行它们更新其配置文件的所有内容,但是如果我设置为默认值的某些字段中填充了实际数据,例如,如果用户实际上已经添加到索引并且一切正常,那么这将覆盖它,任何想法

1 个答案:

答案 0 :(得分:0)

您应该将op_type字段设置为create,如下所示:

$client = Clientbuilder::create()->build();
$response = $client->index([
    'index' => 'users',
    'type' => 'user',
    'id' => $user->id,
    'op_type' => 'create',
    'body' => [
        'username' => $user->username,
        'name' => $user->name,
        'profession' =>$user->getProfession(),
        'secondary_profession' =>$user->getSecondaryProfession(),
        'genre' => $user->getUserGenre(),
        'subgenre' => $user->getUserSubGenre(),
        'city' => $user->getCity(),
        'profilepic' => 'n026qso2xxztaphbwwey.png', //Default pics
        'backgroundpic' => 'backgrounddefaultpms_oa9mee.png', //Default pics
        // 'profilepic' => $user->getProfilePic(),
        // 'backbroundpic' => $user->getBackroundPic(),
        'country' => $user->country,
        'country_fips_code' => $user->country_fips_code,
        'state' => $user->getState(),
        //'affiliations' => $user->getAffiliations(),
        'affiliation_one' => ' ',
        'affiliation_two' => ' ',
        'affiliation_three' => ' ',
        'affiliation_four' => ' ',
        'affiliation_five' => ' ',
        'credential_one' => ' ',
        'credential_two' => ' ',
        'credential_three' => ' ',
        'credential_four' => ' ',
        'credential_five' => ' ',
        // 'countryAbr' => $countryAbr,
        // 'stateAbr' => $stateAbr,
        'cityGeonameId' => $user->geoname_id,
        'latlong' => [$origin],
    ],

]);

在这种情况下,如果已存在具有相同ID的用户,请求将返回错误,并且您不会覆盖现有用户。请在此处查看“版本控制”部分https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html