我面临着奇怪的情况.我在生产环境中遇到错误,而在开发环境中工作正常.
开发:Laravel 5.4.28 PHP 7.0.13 MYSQL 5.7.17
制作:Laravel 5.4.28 PHP 7.2.1 MYSQL 5.7.20
在实现代码中.我用了:
namespace App;
use Illuminate\Support\Facades\Storage;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Artwork extends Model
{
use Searchable;
Run Code Online (Sandbox Code Playgroud)
在开发中它工作正常.但是在生产中它给了我这个错误:count():参数必须是一个数组或在Builder.php中实现Countable的对象(第936行)
正如你在这张照片中看到的: 在这里输入图像描述 任何想法背后的原因是什么?以及如何解决?
首先,我使用Laravel Scout的scout-elasticsearch-driver:https : //github.com/babenkoivan/scout-elasticsearch-driver
我逐步遵循了自述文件,创建索引,迁移索引,配置映射并User::search()->get()返回空数组的步骤。
显然,我的数据库已迁移并填充。
我想通过以下方式搜索用户:
所以我创建了一个IndexConfigurator:
class UserIndexConfigurator extends IndexConfigurator
{
use Migratable;
/**
* @var array
*/
protected $settings = [
//
];
}
Run Code Online (Sandbox Code Playgroud)
创建了一个SearchRule:
class UserSearchRule extends SearchRule
{
public function buildQueryPayload()
{
$query = $this->builder->query;
return [
'should' => [
[
'match' => [
'first_name' => [
'query' => $query
]
]
],
[
'match' => [
'last_name' => [
'query' …Run Code Online (Sandbox Code Playgroud) 我应该如何管理多语言索引(例如:page/page_translations模型应该成为page_en/page_fr索引).我正在使用" Dimsav\Translatable "包.
页面模型:id,status_id,created_at,updated_at
PageTranslation模型:id,page_id,locale,title,slug,body
Algolia为此提供了支持(https://www.algolia.com/doc/guides/search/multilingual-search/),但我不确定如何使用Laravel Scout实现这一目标.
我想到的唯一解决方案是在存储语言环境的相同索引中索引两个语言行(来自翻译模型)并在搜索上应用条件.
Algolia
objectID = 1,title ='英文标题',locale_id ='1'
objectID = 2,title ='Franch title',locale_id ='2'
$pages = App\PageTranslation::search('Star Trek')->where('locale_id', 1)->get();
或者更好的方法?也许分别索引page/page_translations并在两者中搜索?
我希望实现以下目标:
pages_en index:objectID = 1,title ='英文标题'等.
pages_fr index:objectID = 2,title ='Franch title'等.
$pages = App\Page::search('Star Trek')->where('locale', 'en')->get();
有没有办法检查 Elasticsearch 是否已完成处理我的请求?
我想为我的应用程序执行集成测试,检查插入后是否可以找到记录。例如,如果我提出以下请求:
POST /_all/_bulk
{
"update":{
"_id":419,
"_index":"popc",
"_type":"offers"
}
}
{
"doc":{
"id":"419",
"author":"foo bar",
"number":"642-00419"
},
"doc_as_upsert":true
}
Run Code Online (Sandbox Code Playgroud)
而且我马上检查,测试失败,因为Elasticsearch需要一些时间才能完成我的请求。
如果我在断言前睡 1 秒钟,它大部分时间都可以工作,但并非总是如此。
我可以将睡眠时间延长到例如。3 秒,但它使测试非常缓慢,因此我的问题。
我曾尝试使用cat 挂起任务和挂起集群任务端点,但响应始终为空。
如果其中任何一个是相关的,我正在使用Elasticsearch 5.4,Laravel Scout 3.0.5和tamayo/laravel-scout-elastic 3.0.3
我一直在使用 laravel scout 进行一些测试,并根据文档(https://laravel.com/docs/8.x/scout#configuring-searchable-data),我已将我的用户模型映射为这样:
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$data = $this->toArray();
return array_merge($data, [
'entity' => 'An entity'
]);
}
Run Code Online (Sandbox Code Playgroud)
只是为了测试,这就是我在调试时得出的结论。
使用此映射导入用户模型后,我可以在 meilisearch 仪表板上看到它确实显示了用户数据 + 实体 = '一个实体'。
但是,应用此方法时:
User::search('something')->where('entity', 'An entity')->get()
它会产生以下错误:
"message": " --> 1:1\n |\n1 | entity=\"An entity\"\n | ^----^\n |\n = attribute `entity` is not filterable, available filterable attributes are: ",
"exception": "MeiliSearch\\Exceptions\\ApiException",
"file": "/var/www/api/vendor/meilisearch/meilisearch-php/src/Http/Client.php",
Run Code Online (Sandbox Code Playgroud)
回溯查看“可过滤属性”,我得出的结论是:
$client = app(\MeiliSearch\Client::class); …Run Code Online (Sandbox Code Playgroud) 首先,存储在索引中的日期格式是2021-09-16T14:06:02.000000Z
当我使用用户选项登录remember然后注销时,出现以下错误。
ElasticSearch 的响应是
\narray:3 [\xe2\x96\xbc\n "took" => 1\n "errors" => true\n "items" => array:1 [\xe2\x96\xbc\n 0 => array:1 [\xe2\x96\xbc\n "index" => array:5 [\xe2\x96\xbc\n "_index" => "users"\n "_type" => "_doc"\n "_id" => "313"\n "status" => 400\n "error" => array:3 [\xe2\x96\xbc\n "type" => "mapper_parsing_exception"\n "reason" => "failed to parse field [created_at] of type [date] in document with id '313'. Preview of field's value: '2021-09-16 11:37:49'"\n "caused_by" => array:3 [\xe2\x96\xbc\n "type" => "illegal_argument_exception"\n "reason" => "failed …Run Code Online (Sandbox Code Playgroud) 我试过
将弹性搜索与 Laravel scout 与包结合使用
"laravel/scout": "^1.0",
"tamayo/laravel-scout-elastic": "^1.0"
Run Code Online (Sandbox Code Playgroud)
在 localhost:9200 中运行 Elasticsearch 服务器并创建索引并提供必要的配置,
向模型添加了可搜索的特征,
并将数据导入到索引中
php artisan scout:import "App\story"
Imported [App\story] models up to ID: 4
All [App\story] records have been imported.
Run Code Online (Sandbox Code Playgroud)
但是当我进行搜索时,它返回一个空数组
story::search("the")->get()
=> Illuminate\Database\Eloquent\Collection {#754
all: [],
}
Run Code Online (Sandbox Code Playgroud)
当我卷曲时,它也显示为,
// http://localhost:9200/author/_search?pretty=true&q=*:*
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": [
]
}
}
Run Code Online (Sandbox Code Playgroud)
当我在 ES 中添加没有索引的记录时,模型会抛出类似index not found. 但是在添加数据和所有之后,它似乎是空的。我错过了什么吗?
与 algolia 完全相同。
我正在为 Laravel Scout使用laravel-scout-tntsearch-driver包。我已经实施了,一切正常。但现在我想做一个关系搜索。我的城市有很多公司。
城市.php
public function companies()
{
return $this->hasMany(Company::class);
}
Run Code Online (Sandbox Code Playgroud)
公司.php
public function city()
{
return $this->belongsTo(City::class);
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title
];
}
Run Code Online (Sandbox Code Playgroud)
现在搜索只适用于所有公司。
Company::search('bugs bunny')->get();
Run Code Online (Sandbox Code Playgroud)
还有 where 子句在这里不起作用。我想要这样的东西:
Route::get('/search/{city}', function (\App\City $city) {
$companies = $city->companies()->search('bugs bunny');
});
Run Code Online (Sandbox Code Playgroud)
我想你明白了。谢谢!
Laravel Scout:有没有一种方法可以只搜索特定的字段?
目前这条线工作正常:
$es = Element::search($q)->get();
Run Code Online (Sandbox Code Playgroud)
但它搜索标题,短描述和描述字段.我需要它只搜索标题字段.
我似乎无法弄清楚如何在 Laravel scout 中的 1 个查询中的 2 个表中进行搜索。出于目前不重要的特定原因,我不想只运行原始查询。现在我有这个要搜索:
我的关系:
namespace App;
use Laravel\Scout\Searchable;
class Users extends Model
{
use Searchable;
public function searchableAs(){
return 'users_index';
}
public function toSearchableArray(){
return $this->toArray();
}
public function originalUser(){
return $this->belongsTo(OriginalUser::class);
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
use App\OriginalUser;
use App\Users;
use App\Groups;
class SomeController extends Controller{
public function index(){
$group = Group::where('group_id',1)->first();
return Users::search('something')->where('group_id',$group->id)->get();
}
}
Run Code Online (Sandbox Code Playgroud)
现在这将返回在其中一列中具有“某物”的用户,如下所示:
[
{
"id": "1",
"original_user_id": "1",
"username": "something"
"original_user": {
"id": "1",
"username":"test"
}
}
]
Run Code Online (Sandbox Code Playgroud)
但我需要我的代码也返回这条记录:
[
{ …Run Code Online (Sandbox Code Playgroud) laravel-scout ×10
laravel ×4
laravel-5 ×3
mysql ×3
php ×3
algolia ×1
laravel-5.3 ×1
laravel-8 ×1
testing ×1