PHP code example of belyys7 / elasticsearch-its-easy
1. Go to this page and download the library: Download belyys7/elasticsearch-its-easy 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/ */
belyys7 / elasticsearch-its-easy example snippets
$data = [
[
'user' => [
'id' => 100,
'email' => '[email protected] ',
'name' => 'Stepan',
'age' => 21,
'birthday' => '2001-06-15',
],
'work' => [
'position' => [
'id' => 25,
'name' => 'php developer',
],
'skills' => [
[
'id' => 36,
'name' => 'php'
],
[
'id' => 40,
'name' => 'mysql'
],
[
'id' => 56,
'name' => 'js'
],
],
'salary' => 4000
],
'location' => [
'lat' => 50.445077,
'lon' => 30.521215
],
],
[
'user' => [
'id' => 101,
'email' => '[email protected] ',
'name' => 'Luigi',
'age' => 29,
'birthday' => '2005-03-20',
],
'work' => [
'position' => [
'id' => 12,
'name' => 'js developer',
],
'skills' => [
[
'id' => 56,
'name' => 'js'
],
[
'id' => 70,
'name' => 'mongodb'
],
[
'id' => 1,
'name' => 'html'
],
[
'id' => 2,
'name' => 'css'
],
],
'salary' => 2700
],
'location' => [
'lat' => 47.454589,
'lon' => 32.915673
],
],
];
use IdapGroup\ElasticsearchItsEasy\ModelSearchBase;
class StaffModelSearch extends ModelSearchBase
{
public function setRules() : void
{
$this->rules = [
self::GROUP_MUST => [
self::RULE_EQUAL => [
'userId' => 'user.id',
],
],
self::GROUP_SHOULD => [
self::RULE_LIKE => [
'userEmail' => 'user.email',
'userName' => 'user.name',
],
self::RULE_IN => [
'workSkillsId' => 'work.skills.id',
],
],
self::GROUP_FILTER => [
self::RULE_EQUAL => [
'workPositionId' => 'work.position.id'
],
self::RULE_RANGE_NUMBER => [
'userAge' => 'user.age',
'workSalary' => 'work.salary',
],
self::RULE_RANGE_DATE => [
'birthday' => 'user.birthday',
],
],
self::GROUP_LOCATION => [
'location' => self::SORT_DESC,
],
];
}
public function setSort() : void
{
$this->sort = [
'user.id' => self::SORT_DESC,
];
}
}
$staffModelSearch = new StaffModelSearch('es01', '9200', 'staff_search');
$staffModelSearch->reCreateIndex();
foreach ($data as $item) {
$staffModelSearch->addDocument($item, 'user.id', $item['user']['id']);
}
$params = [
'userId' => 100,
'workPositionId' => 25,
'userEmail' => '[email protected] ',
'userName' => 'Stepan',
'workSkillsId' => [36, 40],
'userAge' => ['min' => 18, 'max' => 65],
'workSalary' => ['min' => 500, 'max' => 5000],
'location' => [
'point' => [
'lat' => 48.454589,
'lon' => 33.915673,
'distance' => 100000
],
'rectangle' => [
'topLeftLat' => 55.710929,
'topLeftLng' => 14.090451,
'bottomRightLat' => 41.830140,
'bottomRightLng' => 41.802791
],
],
'page' => 1,
'limit' => 20
];
//$staffModelSearch->enableFixLimitResult(50);
$result = $staffModelSearch->searchList($params);
[
'result' => [
[
'user' => [
'id' => 100,
'email' => '[email protected] ',
'name' => 'Stepan',
'age' => 21
],
'work' => [
'position' => [
'id' => 25,
'name' => 'php developer',
],
'skills' => [
[
'id' => 36,
'name' => 'php'
],
[
'id' => 40,
'name' => 'mysql'
],
[
'id' => 56,
'name' => 'js'
],
],
'salary' => 4000
],
'location' => [
'lat' => 50.445077,
'lon' => 30.521215
],
],
//... etc.
],
'pagination' => [
'totalCount' => (int),
'pageCount' => (int),
'currentPage' => (int)
]
]
$params = [
'userId' => 100,
'workPositionId' => 25,
'userEmail' => '[email protected] ',
'userName' => 'Stepan',
'workSkillsId' => [36, 40],
'userAge' => ['min' => 18, 'max' => 65],
'workSalary' => ['min' => 500, 'max' => 5000],
'location' => [
'point' => [
'lat' => 48.454589,
'lon' => 33.915673,
'distance' => 100000
],
'rectangle' => [
'topLeftLat' => 55.710929,
'topLeftLng' => 14.090451,
'bottomRightLat' => 41.830140,
'bottomRightLng' => 41.802791
],
],
];
$result = $staffModelSearch->searchMap($params);
[
[
'user' => [
'id' => 100,
'email' => '[email protected] ',
'name' => 'Stepan',
'age' => 21
],
'work' => [
'position' => [
'id' => 25,
'name' => 'php developer',
],
'skills' => [
[
'id' => 36,
'name' => 'php'
],
[
'id' => 40,
'name' => 'mysql'
],
[
'id' => 56,
'name' => 'js'
],
],
'salary' => 4000
],
'location' => [
'lat' => 50.445077,
'lon' => 30.521215
],
],
//... etc.
]
$params = [
//...
'location' => [
'point' => [
'lat' => 48.454589,
'lon' => 33.915673,
'distance' => 100000
],
'rectangle' => [
'topLeftLat' => 55.710929,
'topLeftLng' => 14.090451,
'bottomRightLat' => 41.830140,
'bottomRightLng' => 41.802791
],
'clustering' => true,
'zoom' => 1,
],
//...
];
[
// Cluster 1
[
[
// data
],
[
// data
],
//...
],
// Cluster 2
[
[
// data
],
[
// data
],
//...
],
//... etc.
]
// Describe the rules in the associative array as 'term' => [
'user.name.keyword' => 'Stepan',
],
],
[
'term' => [
'user.age' => 21,
],
],
//...
],
//...
];
// Use a Method to Set Your Rules
$staffModelSearch->setOverWriteRules($overWriteRules);