PHP code example of triadev / laravel-elasticsearch-dsl

1. Go to this page and download the library: Download triadev/laravel-elasticsearch-dsl 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/ */

    

triadev / laravel-elasticsearch-dsl example snippets


php artisan vendor:publish --provider="Triadev\Es\Dsl\Provider\ServiceProvider" --tag="config"

int: time needed to execute the query
$result->took();

bool
$result->timedOut();

float
$result->maxScore();

int: number of matched documents
$result->totalHits();

Illuminate\Support\Collection: collection of searchable eloquent models
$result->hits();

array|null
$result->aggregation();

ElasticDsl::search()->termLevel()
    ->must()
        ->term('FIELD', 'VALUE')
    ->mustNot()
        ->term('FIELD', 'VALUE')
    ->should()
        ->term('FIELD', 'VALUE')
    ->filter()
        ->term('FIELD', 'VALUE')
})->get()

ElasticDsl::search()
    ->termLevel()
        ->term('FIELD', 'VALUE')
        ->bool(function (Search $search) {
            $search->termLevel()
                ->term('FIELD', 'VALUE')
                ->bool(function (Search $search) {
                    $search->fulltext()
                        ->match('FIELD1', 'QUERY1')
                        ->matchPhrase('FIELD2', 'QUERY2');
                });
            })
        ->prefix('FIELD', 'VALUE')
    ->get();

--------------------------------------------------
[
    "query" => [
        "bool" => [
            "must" => [
                [
                    "term" => [
                        "FIELD" => "VALUE"
                    ]
                ],
                [
                    "bool" => [
                        "must" => [
                            [...],
                            [...]
                        ]
                    ]
                ],
                [
                    "prefix" => [
                        "FIELD" => [
                            "value" => "VALUE"
                        ]
                    ]
                ]
            ]
        ]
    ]
]

ElasticDsl::search()->termLevel()->filter()->term('FIELD', 'VALUE')->get();

ElasticDsl::search()->fulltext()->must()->match('FIELD', 'QUERY')->get();

ElasticDsl::search()->geo()->filter()->geoDistance('FIELD','10km', new Location(1, 2))->get();

ElasticDsl::search()->compound()->functionScore(
    function (Search $search) {
        $search->termLevel()->term('FIELD1', 'VALUE1');
    },
    function (FunctionScore $functionScore) {
        $functionScore->simple([]);
    }
)->get();

ElasticDsl::search()->joining()->nested('PATH', function (Search $search) {
   $search->termLevel()->filter()->term('FIELD', 'VALUE');
})->get();

ElasticDsl::search()->specialized()->moreLikeThis('LIKE')->toDsl();

ElasticDsl::search()->nestedInnerHit('NAME', 'PATH', function (Search $search) {
    $search->termLevel()->term('FIELD', 'VALUE');
})->get();

ElasticDsl::search()
    ->overwriteIndex('INDEX')
    ->overwriteType('TYPE')
    ->termLevel()
        ->matchAll()
    ->get();

ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
    $aggregation->metric(function (Aggregation\Metric $metric) {
        ...
    });
})->get();

ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
    $aggregation->bucketing(function (Aggregation\Bucketing $metric) {
        ...
    });
})->get();

ElasticDsl::search()->aggregation(function (Aggregation $aggregation) {
    $aggregation->pipeline(function (Aggregation\Pipeline $metric) {
        ...
    });
})->get();

ElasticDsl::suggest()->term('NAME', 'TEXT', 'FIELD')->get();
config/laravel-elasticsearch-dsl.php