-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compatibility and status of project #171
Comments
Hi there, I do not use this package, but was looking around the web for elasticsearch Laravel packages. When I looked at you question, I could see that you are trying to connect to an instance that is using token based security(https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-tokens.html). After looking at the code of Plastic, I can tell you that it is not possible to set authentication headers at this moment, due to missing config options. Also even if you had these config options, there is no functionality to set these connection parameters. Let me explain where to find it and how to fix it: Take a look at: https://github.com/sleimanx2/plastic/blob/master/src/Connection.php#L247 private function buildClient(array $config)
{
$client = ClientBuilder::create()
->setHosts($config['hosts']);
if (isset($config['retries'])) {
$client->setRetries($config['retries']);
}
if (isset($config['logging']) and $config['logging']['enabled'] == true) {
$logger = ClientBuilder::defaultLogger($config['logging']['path'], $config['logging']['level']);
$client->setLogger($logger);
}
return $client->build();
} It configures the Elasticsearch/Client by setting the folllowing:
This is your problem; Because it needs run another method on $client....which it does not at the moment. It should call the following as well:
When you look at the code of the ClientBuilder's build method: if (is_null($this->connectionFactory)) {
if (is_null($this->connectionParams)) {
$this->connectionParams = [];
}
// Make sure we are setting Content-Type and Accept (unless the user has explicitly
// overridden it
if (isset($this->connectionParams['client']['headers']) === false) {
$this->connectionParams['client']['headers'] = [
'Content-Type' => ['application/json'],
'Accept' => ['application/json']
];
} else {
if (isset($this->connectionParams['client']['headers']['Content-Type']) === false) {
$this->connectionParams['client']['headers']['Content-Type'] = ['application/json'];
}
if (isset($this->connectionParams['client']['headers']['Accept']) === false) {
$this->connectionParams['client']['headers']['Accept'] = ['application/json'];
}
}
$this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer);
} So I suggest that you do the following: First Update your config /*
|--------------------------------------------------------------------------
| Hosts
|--------------------------------------------------------------------------
|
| The most common configuration is telling the client about your cluster: how many nodes, their addresses and ports.
| If no hosts are specified, the client will attempt to connect to localhost:9200.
|
*/
'hosts' => [
env('PLASTIC_HOST', '127.0.0.1:9200'),
],
'params' => [
'client' => [
'headers' => [
'Authentication' => [ 'Bearer <your_token>' ]
]
]
] You need to log in and obtain an acces_token, then use laravel to update the config at runtime. Second change the code private function buildClient(array $config)
{
$client = ClientBuilder::create()
->setHosts($config['hosts']);
if (isset($config['retries'])) {
$client->setRetries($config['retries']);
}
// ADD THIS
if (isset($config['params'])) {
$client->setConnectionParams($config['params']);
}
if (isset($config['logging']) and $config['logging']['enabled'] == true) {
$logger = ClientBuilder::defaultLogger($config['logging']['path'], $config['logging']['level']);
$client->setLogger($logger);
}
return $client->build();
} Then you can run the mapping:run with headers set. |
I have made a pull request, which explains it: #172 The only thing you need to do is to set the config headers on runtime. That person says that it was only possible to set something totally new. If that is the case, you should not update your config, but set it at runtime. You should than create a new command, <?php namespace App\Console\Mapping;
use Illuminate\Console\ConfirmableTrait;
use Sleimanx2\Plastic\Mappings\Mapper;
use Sleimanx2\Plastic\Console\Mapping\BaseCommand;
class CustomRun extends BaseCommand
{
use ConfirmableTrait;
/**
* The console command name.
*
* @var string
*/
protected $signature = 'mapping:customrun {--index=} {--database=} {--step} {--force}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'run the remaining mappings';
/**
* @var Mapper
*/
private $mapper;
private $authService;
/**
* Reset constructor.
*
* @param Mapper $mapper
*/
public function __construct(Mapper $mapper, $authService)
{
parent::__construct();
$this->mapper = $mapper;
$this->authService = $authService;
}
/**
* Execute the console command.
*/
public function handle()
{
if (!$this->confirmToProceed()) {
return;
}
$token = $this->authService->accesstoken();
Config::set('plastic.connection.params', [
'client' => [
'headers' => [
'Authentication' => [ 'Bearer '.$token ]
]
]
]);
$this->prepareDatabase();
$path = $this->getMappingPath();
$this->mapper->run($path, [
'step' => $this->option('step'),
'index' => $this->option('index'),
]);
// Once the mapper has run we will grab the note output and send it out to
// the console screen, since the mapper itself functions without having
// any instances of the OutputInterface contract passed into the class.
foreach ($this->mapper->getNotes() as $note) {
$this->output->writeln($note);
}
}
protected function prepareDatabase()
{
$this->mapper->setConnection($this->option('database'));
if (!$this->mapper->repositoryExists()) {
$options = ['--database' => $this->option('database')];
$this->call('mapping:install', $options);
}
}
} |
Ok...I am going on with my life. I was accidentally inspired to take a closer look. Goodluck 😅 |
Hi everyone,
we are using Laravel 5.4.* together with plastic:latest and Elasticsearch 5.6.10 but we receive errors for every single command that we execute.
What we did so far is:
Now if we try to execute
php artisan mapping:run
we get the error:If we try to execute
php artisan plastic:populate --index=product
we get the following error:The status of the Elasticsearch cluster is the following:
What are the current compatibilities of this package?
Thanks in advance.
The text was updated successfully, but these errors were encountered: