PHP code example of alirzaj / laravel-elasticsearch-builder
1. Go to this page and download the library: Download alirzaj/laravel-elasticsearch-builder 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/ */
alirzaj / laravel-elasticsearch-builder example snippets
IndexDocument::dispatch(
'name_of_index',
'id',
['name' => 'alirzaj'] //an array that you want indexed in elasticsearch
);
UpdateDocument::dispatch(
'name_of_index',
'id',
['name' => 'alirzaj'] //an array that you want to add in your existing elasticsearch document
);
UpdateNestedItemByCondition::dispatchSync(
'blogs',
10,
'tags',
['id' => 20], // in document, we have a [nested] tags field. now we are looking for the ones with id of 20
/**
* we want all of those items having above condition to be updated to this item
* note that if you have id key in conditions, and id key in document parameter, the values must be the same
* in other words condition's value must not change in update.
* in this example we find the tag via id and update its name. we couldn't find it via old name and set a new name
*/
['id' => 20, 'name' => 'new-php']
);
UpdateNestedItemByQuery::dispatchSync(
'blogs',
'tags',
['id' => 20], // in documents, we have a [nested] tags field. now we are looking for all documents with this criteria
/**
* we want all of those items having above condition to be updated to this item
* note that if you have id key in conditions, and id key in document parameter, the values must be the same
* in other words condition's value must not change in update.
* in this example we find the tag via id and update its name. we couldn't find it via old name and set a new name
*/
['id' => 20, 'name' => 'new-php']
);
/**
* In tags field, remove all sub-fields with the key of id and value of 20
*/
RemoveItemFromNestedField::dispatch('blogs', 10, 'tags', 'id', 20);
/**
* find documents that have id:20 in their tags field and delete id:20 from them
*/
DeleteNestedFieldByCondition::dispatch(
'blogs',
'tags',
['id' => 20]
);