A Laravel package provides fluent API to work with Elasticsearch
carterzhou/elasticsearch is a Laravel package for a laravel package provides fluent api to work with elasticsearch.
It currently has 0 GitHub stars and 7 downloads on Packagist (latest version 1.0.5).
Install it with composer require carterzhou/elasticsearch.
Discover more Laravel packages by carterzhou
or browse all Laravel packages to compare alternatives.
Last updated
Via Composer
$ composer require carterzhou/elasticsearch
Firstly, create an instance of this class. Here we use dependency injection to let Laravel create and inject an instance for us.
use CarterZhou\Elasticsearch\Client;
class TestController extends Controller
{
protected $client;
/**
* TestController constructor.
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
}
Then we can use search method to grab data from Elasticsearch. Notice that we can chain match method to add filtering conditions (similar to Eloquent where method).
$url = $this->client->getHost() . '/indices-2019.01.28';
$this->client
->match('request', 'one-of-urls')
->match('request', 'field1 field2')
->setSize(500);
$this->client->search($url);
if ($this->client->hasDocuments()) {
foreach ($this->client->getDocuments() as $document) {
// Process your document here...
}
}
$url = $this->client->getHost() . '/indices-2019.01.28';
$this->client
->match('request', 'one-of-urls')
->match('request', 'field1 field2')
->setSize(500);
do {
$this->client->search($url);
if ($this->client->hasDocuments()) {
foreach ($this->client->getDocuments() as $redirect) {
// Process your document here...
}
}
} while ($this->client->hasMoreDocuments());
Notice that we use a do while loop here because a search will be performed at least once. You don't have to manually set "from" because the search method will calculate and maintain properties including "from" under the hood.
Warning: you should not use search method if total of matching documents is over 10000, because by default the result window is 10000 by using "from" to do query. In such case, please use scroll method instead.
As stated above, do not use search method to loop through large result sets because normally you are not allowed to do so. To address such need, you can use scroll method like so
$url = $this->client->getHost() . '/logstash*';
$this->client->matchAll()->setSize(500);
$this->client->scroll($url);
do {
foreach ($this->client->getDocuments() as $document) {
// Process your document here...
}
$this->client->scroll($url);
} while ($this->client->hasDocuments());
Please see the changelog for more information on what has changed recently.
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
license. Please see the license file for more information.