LaravelPackages.net
Acme Inc.
Toggle sidebar
jeffleyd/esquery

Use syntax similar to eloquent, using the elastic search service.

22
1
v1.1.6
About jeffleyd/esquery

jeffleyd/esquery is a Laravel package for use syntax similar to eloquent, using the elastic search service.. It currently has 1 GitHub stars and 22 downloads on Packagist (latest version v1.1.6). Install it with composer require jeffleyd/esquery. Discover more Laravel packages by jeffleyd or browse all Laravel packages to compare alternatives.

Last updated

EsQuery

This project is to be similar to the eloquent query, like this facilitating searches in ElasticSearch's Lucene.

composer require jeffleyd/esquery

PUBLISH THE FILE CONFIG

php artisan vendor:publish --tag="esquery-provider"

Access the config folder and change the settings of the esquery.php file.

USAGE EXAMPLES

First create a mapping for your index
For more information about mapping types: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
$build = EsQuery::index('my_index');
$response = $build->createIndex([
        'mappings' => [
            'properties' => [
                'parent_id' => [
                    'type' => 'long',
                ],
                'created_at' => [
                    'type' => 'date',
                    'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'
                ]
            ]
        ]
    ]);
If you want to add a new index, you can do it like this
Create a migration file and extend the EsScheme class
    public function up()
    {
        EsScheme::create('bosses', function (EsMapping $table) {
            $table->integer('id');
            $table->string('name');
            $table->string('email');
            $table->nested('json');
            $table->timestamp('created_at');
            $table->timestamp('updated_at');
            
            // If you want to use a simple analyzer, you can add the following line.
            $table->setDefaultAnalyzer();

            // If you want to use a custom analyzer, you can use the following:
            $table->mapping['settings'] = [
                'analysis' => [
                    'default_analyze' => [
                        'type' => 'custom',
                        'tokenizer' => 'whitespace',
                        'char_filter' => [
                            'html_strip'
                        ],
                        'filter' => [
                            'lowercase'
                        ]
                    ]
                ]
            ];
        });
    }
    
    public function down()
    {
        EsScheme::dropIfExists('boss');
    }
Now you can create your first document
$build = EsQuery::index('my_index');
$response = $build->create([
        'parent_id' => 1,
        'created_at' => '2022-02-26 23:44:00',
    ]);
Find your document
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->first(); // Example 1
$response = $build->where('parent_id', '=', 1)->first(); // Example 2
$response = $build->where('parent_id', 1)->get(); // Example 3
$response = $build->where('created_at', '>=' '2022-02-26'))->get(); // Example 4
Performing an aggregation
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->sum('price', 'total_price')->get(); // Use get() for aggregations
Delete your document
$build = EsQuery::index('my_index');
$response = $build->where('parent_id', 1)->delete(); // Example 1 delete with conditions
$response = $build->delete(5); // Example 2 delete by ID
Delete your index
$build = EsQuery::index('my_index');
$response = $build->deleteIndex(); 
How you can attach relation
$build = EsQuery::index('my_index');
$response = $build->with('category', 'id', 'group_id')->get();

OR

$build = EsQuery::index('my_index');
$response = $build->with('category', 'id', 'group_id', function (QueryBuilder $query) {
    return $query->where('is_active', 1);
})->with('boss', 'id', 'boss_id', function (QueryBuilder $query) {
    return $query->where('is_active', 1);
})->get();



INDEX

[x] Create
[x] Delete
[x] Update mapping
[x] Exists
[x] Skip

DOCUMENT

[x] Create
[x] Create Many
[x] Update
[x] Delete by ID
[x] Delete by Query

[x] FIRST (with/without conditions)
[x] GET (with/without conditions)
[x] PAGINATION (with/without conditions)
[x] AGGREGATION MAX / MIN / SUM / AVG / COUNT
[x] LIMIT
[x] GROUP BY
[x] GROUP BY DATE

CONDITIONS

[x] where
[x] whereIn
[x] whereExists
[x] whereNotExists
[x] whereMissing
[x] between
[x] orderBy

ADDITIONAL

[x] with
[x] reset query after get/first/paginate

Site: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
Version: 8.1

Comments