Laravel + Mongodb 全文搜索
Laravel + Mongodb full text search
如何在 Laravel 中使用 mongodb 文本搜索?请提出建议。
我可以在终端上做到这一点 -
db.coupons.find( { $text: { $search: "Search me here" } } )
但不确定如何在 laravel 中完成。
在您的 composer.json 中添加 jenssegers/laravel-mongodb 软件包并通过 composer update
安装它。访问添加的 link,在那里你将获得处理 mongodb-laravel 连接的良好源代码。
你有没有尝试过类似的东西:
$users = User::with(array('posts' => function($query)
{
$query->where('title', 'like', '%first%');
}))->get();
看看是否有帮助。
由于您使用的是 https://github.com/jenssegers/laravel-mongodb,根据您的代码,您可以说
$coupons= Coupons::where('$text' =>['$search' =>'Search me here' ]);
}))->get();
要使用 laravel mongodb(使用 jenssegers/laravel-mongodb)进行搜索,您需要先创建一个索引。例如:db.articles.createIndex( { "subject" : "text" } )
,其中 subject
是进行搜索的文本属性。
然后您可以尝试在 mongodb 中进行查询:db.articles.find( { $text: { $search: "coffee" } } )
或在 Laravel 中进行查询:
$word = "coffee";
$response = Article::whereRaw(array('$text'=>array('$search'=> $word)))->get();
如果您想按短语搜索,您需要使用引号。在 mongodb 中:db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
和 Laravel:
$word = "coffee shop";
$response = Article::whereRaw(array('$text'=>array('$search'=> "\"" . $word . "\"")))->get();
如何在 Laravel 中使用 mongodb 文本搜索?请提出建议。
我可以在终端上做到这一点 -
db.coupons.find( { $text: { $search: "Search me here" } } )
但不确定如何在 laravel 中完成。
在您的 composer.json 中添加 jenssegers/laravel-mongodb 软件包并通过 composer update
安装它。访问添加的 link,在那里你将获得处理 mongodb-laravel 连接的良好源代码。
你有没有尝试过类似的东西:
$users = User::with(array('posts' => function($query)
{
$query->where('title', 'like', '%first%');
}))->get();
看看是否有帮助。
由于您使用的是 https://github.com/jenssegers/laravel-mongodb,根据您的代码,您可以说
$coupons= Coupons::where('$text' =>['$search' =>'Search me here' ]);
}))->get();
要使用 laravel mongodb(使用 jenssegers/laravel-mongodb)进行搜索,您需要先创建一个索引。例如:db.articles.createIndex( { "subject" : "text" } )
,其中 subject
是进行搜索的文本属性。
然后您可以尝试在 mongodb 中进行查询:db.articles.find( { $text: { $search: "coffee" } } )
或在 Laravel 中进行查询:
$word = "coffee";
$response = Article::whereRaw(array('$text'=>array('$search'=> $word)))->get();
如果您想按短语搜索,您需要使用引号。在 mongodb 中:db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
和 Laravel:
$word = "coffee shop";
$response = Article::whereRaw(array('$text'=>array('$search'=> "\"" . $word . "\"")))->get();