ElasticSearch第4天 在項目中使用Elasticsearch-PHP的API

今日目標

在項目中使用Elasticsearch-PHP的API

項目:用戶管理系統

現在有一個項目是做一個用戶管理系統,要求可以對用戶的信息實現增刪改查,用戶的信息包括
<姓名(name),性別(sex),年齡(age),身份證號(idcard)>

元數據數據字典

_index:info_manager
_type : user
_id:自動生成

插入用戶

$data = [
            'name' => $request->name,
            'sex' => $request->sex,
            'age' => $request->age,
            'idcard' => $request->idcard,
            'created_at' => date('Y-m-d H:i:s', time()),
            'updated_at' => date('Y-m-d H:i:s', time()),
        ];

        $params = [
            'index' => $this->index,
            'type' => $this->type,
            'body' => $data,
        ];

        $exists = app('es')->search([
            'index' => $this->index,
            'type' => $this->type,
            'body' => [
                'query' => [
                    'match' => [
                        'idcard' => $request->idcard,
                    ],
                ],
            ],
        ]);
        if ($exists['hits']['hits']) {
            return '已經存在該用戶';
        }

        $response = app('es')->index($params);

        if ($response['result'] == 'created') {
            return '創(chuàng)建成功';
        }

        return '創(chuàng)建失敗';
}

查找姓名為“張三”的用戶

    public function get(Request $request)
    {
        $exists = app('es')->search([
            'index' => $this->index,
            'type' => $this->type,
            'body' => [
                'query' => [
                    'match' => [
                        'name' => $request->name,
                        //'idcard' => $request->idcard,
                    ],
                ],
            ],
        ]);
        if ($exists['hits']['hits']) {
            return $exists['hits']['hits'];
        }

        return  '不存在該用戶';
    }
  • 響應
[
    {
        "_index": "info_manager",
        "_type": "user",
        "_id": "SBQ5qHwBQ6qKqx4bjNy_",
        "_score": 4.60517,
        "_source": {
            "name": "張三",
            "sex": "男",
            "age": "28",
            "idcard": "61062164545212444121221",
            "created_at": "2021-10-22 13:38:36",
            "updated_at": "2021-10-22 13:38:36"
        }
    }
]

修改id為xx的用戶年齡

    public function update(Request $request)
    {
        $exists = app('es')->exists([
            'index' => $this->index,
            'type' => $this->type,
            'id' => $request->id,
        ]);

        if ($exists) {
            $response = app('es')->update([
                'index' => $this->index,
                'type' => $this->type,
                'id' => $request->id,
                'body' => [
                    'doc' => [
                        'age' => $request->age,
                    ],
                ],
            ]);

            return  $response;
        }

        return '資源不存在';
    }

列出所有用戶

public function list(Request $request)
    {
        $params = [
            'index' => $this->index,
            'type' => $this->type,
        ];

        $response = app('es')->search();

        return $response['hits']['hits'];
    }

刪除id為xx的用戶

    public function del(Request $request)
    {
        $params = [
            'index' => $this->index,
            'type' => $this->type,
            'id' => $request->id,
        ];

        $response = app('es')->delete($params);

        return $response;
    }

明日計劃

進一步學習在項目中使用es實現一些較復雜的功能,分頁查詢,統計等

總結

項目中是可以用ES實現數據庫的各種操作,但感覺對比數據庫的使用,好像稍微有些麻煩,比如每次需要去指定index和type,這個可以通過封裝去實現,或者去使用其他封裝好的,更貼近數據庫使用習慣的插件去使用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容