Reputation: 2728
I'm experimenting with ElasticSearch using laravel. Here's my config thus far:-
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();
});
}
My app is running on a docker container at: http://localhost:3024
My elasticsearch is also running on a docker container at:
docker run --rm -d -e "discovery.type=single-node" -e "bootstrap.memory_lock=true" -p 9200:9200 elasticsearch:6.8.1
with me able to access it using cURL (and in a browser):-
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"
}
My database is up, migrated and seeded fine. It seems these three lines in routes/web.php
break the code:-
App\Content::createIndex($shards = null, $replicas = null);
App\Content::putMapping($ignoreConflicts = true);
App\Content::addAllToIndex();
These settings are fundamental to the ElasticSearch
setup?
I've googled around for it and other folks are having similar issues but I'm unable to find a definitive solution. Are you able to shine some light on it?
Upvotes: 3
Views: 2787
Reputation: 312
The problem is that each docker is almost like an isolated environment.
So your app (which is running on http://localhost:3024), is trying to connect to localhost:9200 on that same container. and thus it results in the error you're getting.
To fix this, you need to connect both container through a network.
Read this: Docker Network Connect
Upvotes: 4