我正在使用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],
],
]);
我想过只运行这个,他们会更新他们的个人资料,但如果我设置为默认值的一些字段填充了实际数据,例如,如果用户实际上已经添加到索引中,并且一切正常,那么这将覆盖它,任何想法
发布于 2017-06-04 11:25:07
您应该将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的用户,则request将返回一个错误,并且您不会覆盖现有的用户。请看这里的“版本控制”部分https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
https://stackoverflow.com/questions/44348976
复制