【发布时间】:2021-03-23 22:04:18
【问题描述】:
我正在使用 laravel 试验 ElasticSearch。到目前为止,这是我的配置:-
config/elasticquent.php
return array(
'config' => [
'hosts' => ['localhost:9200'],
'retries' => 1,
],
'default_index' => 'contents',
);
app/Content.php
class Content extends Model
{
use ElasticquentTrait;
protected $fillable = ['title', 'text', 'image'];
protected $mappingProperties = array(
'title' => [
'type' => 'string',
"analyzer" => "standard",
],
'text' => [
'type' => 'text',
"analyzer" => "standard",
],
'image' => [
'type' => 'string',
"analyzer" => "standard",
],
);
}
routes/web.php
Route::get('/', function () {
App\Content::createIndex($shards = null, $replicas = null);
App\Content::putMapping($ignoreConflicts = true);
App\Content::addAllToIndex();
return view('contents.index', [
'contents' => App\Content::all(),
]);
});
Route::get('/search', function() {
return App\Content::searchByQuery(['match' => ['title' => 'Adipisci']]);
});
composer.json
"require": {
"elasticquent/elasticquent": "dev-master",
}
// database/migrations/2020_12_12_105155_create_contents_table.php
public function up()
{
Schema::create('contents', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('text');
$table->string('image');
$table->timestamps();
});
}
我的应用在 docker 容器上运行:http://localhost:3024 我的 elasticsearch 也在 docker 容器上运行:
docker run --rm -d -e "discovery.type=single-node" -e "bootstrap.memory_lock=true" -p 9200:9200 elasticsearch:6.8.1
我可以使用 cURL(以及在浏览器中)访问它:-
curl -X GET 'http://localhost:9200'
{
"name" : "irt6ohl",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "jtqcAMyJTRunrxobr6zE_g",
"version" : {
"number" : "6.8.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "1fad4e1",
"build_date" : "2019-06-18T13:16:52.517138Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
我的数据库已启动、迁移和播种正常。 routes/web.php 中的这三行似乎破坏了代码:-
App\Content::createIndex($shards = null, $replicas = null);
App\Content::putMapping($ignoreConflicts = true);
App\Content::addAllToIndex();
这些设置是ElasticSearch 设置的基础?
我已经用谷歌搜索了它,其他人也遇到了类似的问题,但我无法找到明确的解决方案。你能照亮它吗?
【问题讨论】:
标签: laravel elasticsearch