【问题标题】:How to add user to Index Elasticsearch only if one dosen't exist before updating仅当更新前不存在用户时如何将用户添加到索引 Elasticsearch
【发布时间】:2017-06-03 22:09:44
【问题描述】:

我正在使用 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],
            ],

        ]);

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

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    您应该将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

    【讨论】:

    • 这几乎完成了工作,但是它引发的错误是什么搞砸了,我怎样才能做到这一点,如果 id 已经存在,它只是继续下一行代码而不是抛出一个错误
    • 您应该只处理错误或跳过它。应该不是问题
    猜你喜欢
    • 2021-01-12
    • 2016-08-14
    • 1970-01-01
    • 2015-12-02
    • 2015-04-07
    • 2022-10-04
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多