### Starting local Elasticsearch (Nix) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md When using the Nix setup, run this command using the 'just' task runner to start a local Elasticsearch instance and process-compose for development. ```sh just up ``` -------------------------------- ### Starting Docker development environment Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Execute this command to start the development environment defined in the docker-compose.yml file in detached mode (-d). This launches services like Elasticsearch, Kibana, and Redis. ```sh docker-compose up -d ``` -------------------------------- ### Entering Nix development shell (Impure) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Run this command if direnv is not used to enter the Nix development shell. This pulls down all necessary dependencies, installs hooks, formatters, and sets up the environment. ```sh nix develop --impure ``` -------------------------------- ### Allowing direnv environment (Nix) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Use this command when inside the project directory with direnv installed to allow it to load the environment defined in the .envrc file, pulling down dependencies and installing hooks. ```sh direnv allow ``` -------------------------------- ### Install Laravel-Elasticsearch via Composer Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This command installs the latest stable version of the pdphilip/elasticsearch package using Composer. ```bash composer require pdphilip/elasticsearch ``` -------------------------------- ### Running all Pest tests (Docker/Nix) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Execute this command to run all tests using the Pest testing framework. This is the standard way to run tests in both the Docker and Nix development environments. ```sh ./vendor/bin/pest ``` -------------------------------- ### Install Specific Laravel-Elasticsearch Versions via Composer Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md These commands show how to install specific major versions of the pdphilip/elasticsearch package compatible with different Laravel versions. ```bash composer require pdphilip/elasticsearch:~5 ``` ```bash composer require pdphilip/elasticsearch:~4 ``` ```bash composer require pdphilip/elasticsearch:~3.9 ``` ```bash composer require pdphilip/elasticsearch:~3.8 ``` ```bash composer require pdphilip/elasticsearch:~2.7 ``` ```bash composer require pdphilip/elasticsearch:~2.6 ``` ```bash composer require pdphilip/elasticsearch:~1.9 ``` ```bash composer require pdphilip/elasticsearch:~1.8 ``` ```bash composer require pdphilip/elasticsearch:~1.7 ``` ```bash composer require pdphilip/elasticsearch:~1.6 ``` -------------------------------- ### Viewing Docker service logs Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Run this command to view the logs output by the services running in the Docker environment. The -f flag follows the logs in real-time. ```sh docker-compose logs -f ``` -------------------------------- ### Stopping Docker development environment Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Use this command to stop and remove the containers, networks, and volumes created by docker-compose up, effectively shutting down the development environment. ```sh docker-compose down ``` -------------------------------- ### Configuring Elastic Cloud in .env (INI) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md Example environment variables for connecting to an Elastic Cloud deployment, including authentication type, hosts, username, password, and cloud ID. ```ini ES_AUTH_TYPE=cloud ES_HOSTS="https://xxxxx-xxxxxx.es.europe-west1.gcp.cloud.es.io:9243" ES_USERNAME=elastic ES_PASSWORD=XXXXXXXXXXXXXXXXXXXX ES_CLOUD_ID=XXXXX:ZXVyb3BlLXdl.........SQwYzM1YzU5ODI5MTE0NjQ3YmEyNDZlYWUzOGNkN2Q1Yg== ES_API_ID= ES_API_KEY= ES_SSL_CA= ES_INDEX_PREFIX=my_app_ ES_ERROR_INDEX= ``` -------------------------------- ### Configuring Multiple Elasticsearch Hosts in .env (INI) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md Example of setting the `ES_HOSTS` environment variable with multiple Elasticsearch node addresses, separated by commas, for connecting to a cluster. ```ini ES_HOSTS="http://es01:9200,http://es02:9200,http://es03:9200" ``` -------------------------------- ### Searching Eloquent Models with searchPhrasePrefix and highlight in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example demonstrates using 'searchPhrasePrefix' to find documents containing a specific phrase prefix and applying highlighting to the results. ```php UserProfile::searchPhrasePrefix('loves espressos and t')->highlight()->get(); ``` -------------------------------- ### Running filtered Pest tests (Docker/Nix) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CONTRIBUTING.md Use this command to run a specific subset of tests by providing a filter string (e.g., a test name or part of a name) to the Pest test runner. ```sh ./vendor/bin/pest --filter="test_name" ``` -------------------------------- ### Example bulkInsert() Return Value (JSON) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Illustrates the structure and content of the JSON response returned by the `bulkInsert()` method, including details on total operations, success/failure counts, and specific error information for failed inserts. ```json { "hasErrors": true, "total": 3, "took": 0, "success": 2, "created": 1, "modified": 1, "failed": 1, "errors": [ { "id": "Y-dp3ZUBnJmuNNwy7vkF", "type": "document_parsing_exception", "reason": "[1:45] failed to parse field [created_at] of type [date] in document with id 'Y-dp3ZUBnJmuNNwy7vkF'. Preview of field's value: 'xxxxxx'" } ] } ``` -------------------------------- ### Querying Eloquent Models with Relationships in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example demonstrates querying 'UserLog' records with a specific status, ordering them, and eager loading the related 'user' model using the 'with' method. ```php UserLog::where('status', 1)->orderByDesc('created_at')->with('user')->get(); ``` -------------------------------- ### Searching Eloquent Models with whereGeoDistance in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example demonstrates performing a geospatial search using 'whereGeoDistance' on the 'UserLog' model to find records within a 10km radius of a specific coordinate. ```php UserLog::whereGeoDistance('location', '10km', [40.7185,-74.0025])->get(); ``` -------------------------------- ### Paginating Eloquent Query Results in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example demonstrates querying 'UserLog' records with a specific status, ordering them by creation date in descending order, and paginating the results to 50 items per page. ```php UserLog::where('status', 4)->orderByDesc('created_at')->paginate(50); ``` -------------------------------- ### Deleting Eloquent Model Records by Date and State in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example demonstrates deleting 'UserProfile' records that are in the 'unsubscribed' state and were updated more than 90 days ago. ```php UserProfile::where('state','unsubscribed') ->where('updated_at','<=',Carbon::now()->subDays(90))->delete(); ``` -------------------------------- ### Using bulkInsert() in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Demonstrates the usage of the new `bulkInsert()` method for inserting multiple documents into Elasticsearch, showing examples of updating, creating, and failing inserts. This method continues on errors and returns results for each operation. ```php People::bulkInsert([ [ 'id' => '_edo3ZUBnJmuNNwymfhJ', // Will update (if id exists) 'name' => 'Jane Doe', 'status' => 1, ], [ 'name' => 'John Doe', // Will Create 'status' => 2, ], [ 'name' => 'John Dope', 'status' => 3, 'created_at' => 'xxxxxx', // Will fail ], ]); ``` -------------------------------- ### Creating Eloquent Model Records in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This example shows how to create a new record in the 'UserLog' model using the Eloquent create method, populating various fields including a nested 'location' array. ```php UserLog::create([ 'user_id' => '2936adb0-b10d-11ed-8e03-0b234bda3e12', 'ip' => '62.182.98.146', 'location' => [40.7185,-74.0025], 'country_code' => 'US', 'status' => 1, ]); ``` -------------------------------- ### Accessing Underlying Elasticsearch Client in Laravel Elasticsearch v5.0 Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Provides an example of how to obtain the underlying Elasticsearch PHP client instance directly from the `Connection` facade to execute client-specific methods not available in the Eloquent wrapper. ```php Connection::on('elasticsearch')->elastic()->{clientMethod}(); ``` -------------------------------- ### Registering Elasticsearch Service Provider in Laravel 11+ (PHP) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md Code snippet showing how to add the `ElasticServiceProvider` to the `bootstrap/providers.php` file for Laravel versions 11 and above to enable package autoloading. ```php //bootstrap/providers.php [ ... ... PDPhilip\Elasticsearch\ElasticServiceProvider::class, ... ], ``` -------------------------------- ### Adding Elasticsearch Connection to Laravel database.php (PHP) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md Configuration array for the 'elasticsearch' connection in Laravel's `config/database.php`, reading settings from environment variables for host, authentication, SSL, and various client options. ```php 'elasticsearch' => [ 'driver' => 'elasticsearch', 'auth_type' => env('ES_AUTH_TYPE', 'http'), //http or cloud 'hosts' => explode(',', env('ES_HOSTS', 'http://localhost:9200')), 'username' => env('ES_USERNAME', ''), 'password' => env('ES_PASSWORD', ''), 'cloud_id' => env('ES_CLOUD_ID', ''), 'api_id' => env('ES_API_ID', ''), 'api_key' => env('ES_API_KEY', ''), 'ssl_cert' => env('ES_SSL_CA', ''), 'ssl' => [ 'cert' => env('ES_SSL_CERT', ''), 'cert_password' => env('ES_SSL_CERT_PASSWORD', ''), 'key' => env('ES_SSL_KEY', ''), 'key_password' => env('ES_SSL_KEY_PASSWORD', ''), ], 'index_prefix' => env('ES_INDEX_PREFIX', false), 'options' => [ 'bypass_map_validation' => env('ES_OPT_BYPASS_MAP_VALIDATION', false), 'logging' => env('ES_OPT_LOGGING', false), 'ssl_verification' => env('ES_OPT_VERIFY_SSL', true), 'retires' => env('ES_OPT_RETRIES', null), 'meta_header' => env('ES_OPT_META_HEADERS', true), 'default_limit' => env('ES_OPT_DEFAULT_LIMIT', 1000), 'allow_id_sort' => env('ES_OPT_ID_SORTABLE', false), ], ], ``` -------------------------------- ### Configure Elasticsearch Environment Variables Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet provides the necessary environment variables to configure the connection and settings for Elasticsearch in your Laravel application's .env file. ```ini ES_AUTH_TYPE=http ES_HOSTS="http://localhost:9200" ES_USERNAME= ES_PASSWORD= ES_CLOUD_ID= ES_API_ID= ES_API_KEY= ES_SSL_CA= ES_INDEX_PREFIX=my_app_ # prefix will be added to all indexes created by the package with an underscore # ex: my_app_user_logs for UserLog model ES_SSL_CERT= ES_SSL_CERT_PASSWORD= ES_SSL_KEY= ES_SSL_KEY_PASSWORD= ``` -------------------------------- ### Using Callback-Based Search Options in Laravel Elasticsearch v5.0 Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Demonstrates the new approach for applying search options like fuzzy search, boost, and minimum should match using a callback function passed to methods like `searchTerm()`. ```php Product::searchTerm('espresso time', function (SearchOptions $options) { $options->searchFuzzy(); $options->boost(2); $options->minimumShouldMatch(2); })->get(); ``` -------------------------------- ### Searching Eloquent Models with searchTerm in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet shows how to perform a search using the package's 'searchTerm' method on the 'UserProfile' model, looking for either 'Laravel' or 'Elasticsearch'. ```php UserProfile::searchTerm('Laravel')->orSearchTerm('Elasticsearch')->get(); ``` -------------------------------- ### Querying Eloquent Models with whereIn and take in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet shows how to query 'UserProfile' records where the 'country_code' is either 'US' or 'CA', order by 'last_login' descending, and limit the results to the first 10. ```php UserProfile::whereIn('country_code',['US','CA']) ->orderByDesc('last_login')->take(10)->get(); ``` -------------------------------- ### Composer Dependency for v5.0.0 (JSON) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Specifies the required version constraint for the `pdphilip/elasticsearch` package in the Composer `composer.json` file to use version 5.0.0 or higher. ```json "pdphilip/elasticsearch": "^5" ``` -------------------------------- ### Searching Eloquent Models with whereFuzzy in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet shows how to use the 'whereFuzzy' method to perform a fuzzy search on the 'description' field of the 'UserProfile' model, allowing for minor typos. ```php UserProfile::whereFuzzy('description', 'qick brwn fx')->get(); ``` -------------------------------- ### Querying Eloquent Models by Date in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet demonstrates a standard Eloquent query to retrieve records from the 'UserLog' model where the 'created_at' timestamp is within the last 30 days. ```php UserLog::where('created_at','>=',Carbon::now()->subDays(30))->get(); ``` -------------------------------- ### Setting Default Limit in Model (PHP) Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Shows how to configure the default query limit for an Elasticsearch model by setting the `$defaultLimit` protected property. This replaces the deprecated `MAX_SIZE` constant from previous versions. ```php use PDPhilip\Elasticsearch\Eloquent\Model; class Product extends Model { protected $defaultLimit = 10000; protected $connection = 'elasticsearch'; } ``` -------------------------------- ### Updating Query Behavior in Laravel Elasticsearch v5.0 Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Explains the change in the default behavior of `where()` from `match` to `term` query in v5.0 and introduces the new `whereMatch()` method for explicitly performing `match` queries. ```php // Old: Product::where('name', 'John')->get(); // match query // New: Product::whereMatch('name', 'John')->get(); // match query Product::where('name', 'John')->get(); // term query ``` -------------------------------- ### Schema Blueprint Class Changes in Laravel Elasticsearch v5.0 Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/CHANGELOG.md Shows the removal of `IndexBlueprint` and `AnalyzerBlueprint` classes and their replacement with a single `Blueprint` class for defining index schemas in migrations. ```diff - use PDPhilip\Elasticsearch\Schema\IndexBlueprint; - use PDPhilip\Elasticsearch\Schema\AnalyzerBlueprint; use PDPhilip\Elasticsearch\Schema\Blueprint; ``` -------------------------------- ### Searching Eloquent Models with whereMatch in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet shows how to use the 'whereMatch' method to find 'UserProfile' records where the 'bio' field contains the term 'PHP'. ```php UserProfile::whereMatch('bio', 'PHP')->get(); ``` -------------------------------- ### Updating Eloquent Model Records in PHP Source: https://github.com/pdphilip/laravel-elasticsearch/blob/main/README.md This snippet illustrates how to update records in the 'UserLog' model, specifically changing the 'status' field to 4 for all records where the current status is 1. ```php UserLog::where('status', 1)->update(['status' => 4]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.