1. Go to this page and download the library: Download aviationcode/elasticsearch library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aviationcode / elasticsearch example snippets
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use ElasticSearchable;
}
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use ElasticSearchable;
public $mapping = [
'category' => ['type' => 'keyword'],
'properties' => ['type' => 'object', 'dynamic' => true],
'ip' => ['type' => 'ip'],
];
}
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use ElasticSearchable;
protected $keyType = 'string';
}
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use ElasticSearchable;
public $indexName = 'my_custom_index_name';
}
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use ElasticSearchable;
public $indexVersion = 2;
}
namespace App\Http\Controllers;
use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Filter;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Must;
use Illuminate\Http\Request;
class ArticleController
{
public function index(Request $request)
{
return Elasticsearch::forModel(Article::class)
->query()
->filter(function (Filter $filter) use ($request) {
if ($user = $request->query('user')) {
$filter->term('user', $user);
}
})
->must(function (Must $must) use ($request) {
if ($query = $request->query('q')) {
$must->queryString($query);
}
})
->get();
}
}
namespace App\Http\Controllers;
use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Filter;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Must;
use Illuminate\Http\Request;
class ArticleController
{
public function index(Request $request)
{
return Elasticsearch::query('article')
->filter(function (Filter $filter) use ($request) {
if ($user = $request->query('user')) {
$filter->term('user', $user);
}
})
->must(function (Must $must) use ($request) {
if ($query = $request->query('q')) {
$must->queryString($query);
}
})
->get();
}
}
namespace App\Http\Controllers;
use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
class ArticlesPerUserPerDayController
{
public function index()
{
$qb = Elasticsearch::forModel(Article::class)->query();
$qb->aggregations()
->dateHistogram('date', 'created_at', '1d')
->terms('date.users', 'user');
return $qb->get()->aggregations;
}
}
namespace App\Http\Controllers;
use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
class ArticlesPerUserPerDayController
{
public function index()
{
$qb = Elasticsearch::query('article');
$qb->aggregations()
->dateHistogram('date', 'created_at', '1d')
->terms('date.users', 'user');
return $qb->get()->aggregations;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.