Comments (3)
Ok...I am going on with my life. I was accidentally inspired to take a closer look.
Goodluck
from plastic.
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
There you can see this code:
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:
- $client->setHosts($config['hosts']);
- $client->setRetries($config['retries']);
- $client->setLogger($logger);
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:
- $client->setConnectionParams($config['params]);
When you look at the code of the ClientBuilder's build method:
https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/ClientBuilder.php#L475
You can see that it set's the headers at some point.
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.
So that the Bearer token gets set.
see: https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-tokens.html
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.
from plastic.
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.
This might not be possible for existing config. Like this person experienced. https://laracasts.com/discuss/channels/laravel/change-environment-variables-on-the-fly-in-a-command
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, artisan make:command CustomRun
And add auth and config stuff:
<?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);
}
}
}
from plastic.
Related Issues (20)
- Nested query fails. Issue with Mapping?
- How to sync a property of a many-to-many relation (using a pivot table)?
- "Nothing to map" message but mappings exist HOT 1
- Can't populate the index with correct data HOT 1
- Update set id to 0 in elasticsearch HOT 1
- Populate and run mappings from json
- There is no good way to set custom model fillers
- Search on Translatable model HOT 1
- Feature request: Set search parameters for a query HOT 1
- return api response
- phrase_match with Plastic HOT 1
- plastic:populate does not include deleted records
- Consider this package unmaintained HOT 5
- Default $current for paginator in v0.3.1
- IAM - Signed Requests using Plastic
- Make plastic compatible with Laravel 5.7 HOT 5
- Is this project abandoned? HOT 1
- [ErrorException] filter_var(): explicit use of FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED is deprecated HOT 3
- How do I know actual search lucene query while using this library?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plastic.