Giter Club home page Giter Club logo

elasticsearch-its-easy's Introduction

Elasticsearch it`s easy SDK

Elasticsearch it`s easy SDK for working with Elasticsearch like with constructor

Documentation

The documentation for the Elasticsearch REST API can be found here.

Installation

The preferred way to install this extension is through composer.

Either run

composer require idapgroup/elasticsearch-its-easy

or add

{
  "require": {
    "idapgroup/elasticsearch-its-easy": "^1.1.1"
  }
}

to the requirement section of your composer.json file.

Quickstart

Prepare the data you want to store in elasticsearch

$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
        ],
    ],
];

Create your class to be expanded by basic search

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,
        ];
    }

}

Create model based on elasticsearch configuration

$staffModelSearch = new StaffModelSearch('es01', '9200', 'staff_search');

Indexing Documents in Elasticsearch

$staffModelSearch->reCreateIndex();

foreach ($data as $item) {
    $staffModelSearch->addDocument($item, 'user.id', $item['user']['id']);
}

Example #1: search as list with pagination

Specify search keys and their values

$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
];

Set data output limits if required and search

//$staffModelSearch->enableFixLimitResult(50);
$result = $staffModelSearch->searchList($params);

The result of the response will be in the format

[
    '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)
    ]
]

Example #2: search for map

Specify search keys and their values

$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
        ],       
    ],
];

Execute search

$result = $staffModelSearch->searchMap($params);

The result of the response will be in the format

[
    [
        '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.
]

Additional settings

Custom clustering (useful when markers on the map overlap each other and the maximum zoom does not solve the problem)

$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,
    ],
    //...
];

Response structure example

[
    // Cluster 1
    [
        [
            // data
        ],
        [
            // data
        ],
        //...
    ],
    // Cluster 2
    [
        [
            // data
        ],
        [
            // data
        ],
        //...
    ],
    //... etc.
]

Overwrite rules

// Describe the rules in the associative array as required by the ES documentation
$overWriteRules = [
    'must' => [
        [
            'term' => [
                'user.name.keyword' => 'Stepan',
            ],
        ],
        [
            'term' => [
                'user.age' => 21,
            ],
        ],
        //...
    ],
    //...
];

// Use a Method to Set Your Rules
$staffModelSearch->setOverWriteRules($overWriteRules);

elasticsearch-its-easy's People

Contributors

belyys7 avatar matveu avatar

Stargazers

 avatar Bohdan Kikacheishvili avatar Omelchenko Dmitro avatar Alexander Ermakov avatar  avatar  avatar  avatar

Watchers

Oleksa Korin avatar Kovalyov Anton avatar  avatar Eugene Kotsogub avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.