使用Algolia - Laravel搜索自定义查询结果集合

时间:2017-09-22 19:36:51

标签: php mysql laravel search algolia

我有使用复杂的laravel查询创建的集合,这个查询的结果太大了。所以我想我必须使用algolia。据我所知,algolia将模型表数据作为json自行获取并从那里开始服务。

$result = User::search("UserName")->get();

它需要某些模型配置,如searchAs等。所有这些都与现有模型相关,您可以使用search方法从模型中进行搜索(上例)。我想问的是,我有复杂的查询和结果有太多来自另一个表(加入)的属性。我想在我的自定义查询结果上进行搜索。有可能吗?

我的示例查询:

    $friendShips = Friend::
    join("vp_users as users","users.id","=","friendships.friendID")
        ->leftJoin("vp_friendships as friendshipsForFriend",function($join) use ($request)
        {
            $join->on("friendships.friendID","=","friendshipsForFriend.userID");
            $join->on("friendshipsForFriend.friendID","=",DB::raw($request->userID));

        })
        ->leftJoin("vp_videos_friends as videosFromFriendMedias",function($join)
        {
            $join->on("videosFromFriendMedias.userID","=","friendships.friendID");
            $join->on("videosFromFriendMedias.friendID", "=" ,"friendships.userID");
            $join->on("videosFromFriendMedias.isCalled", "=" , DB::raw(self::CALLED));

        })
        ->leftJoin("vp_videos_friends as videosToFriendMedias",function($join)
        {
            $join->on("videosToFriendMedias.userID", '=', "friendships.userID");
            $join->on("videosToFriendMedias.friendID", '=', "friendships.friendID");
            $join->on(function($join){
                $join->on("videosToFriendMedias.isCalled", '=', DB::raw(self::CALLED));
                $join->orOn("videosToFriendMedias.isActive", '=', DB::raw(self::ACTIVE));
            });
        })
        ->leftJoin("vp_videos_friends as 
        //some join rules too
        })...

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是使用此请求并链接searchable()方法。它会将查询返回的集合索引到Algolia。

$friendShips = Friend::
    join("vp_users as users","users.id","=","friendships.friendID")
    ->leftJoin("vp_friendships as friendshipsForFriend",function($join) use ($request) {
        $join->on("friendships.friendID","=","friendshipsForFriend.userID");
        $join->on("friendshipsForFriend.friendID","=",DB::raw($request->userID));

    })
    ->searchable();