Code Monkey home page Code Monkey logo

phalcon-elasticsearch's Introduction

Baka Phalcon Elastic Search

Phalcon Elastic Search package to index / query model with relationship easily

Table of Contents

  1. Indexing
    1. Create
    2. Insert
  2. Search
  3. Testing

Installing

Packages:

  • "elasticsearch/elasticsearch": "~2.0@beta"
  • "baka/database": "dev-master"
  • "phalcon/incubator": "~3.0","

Add elastic configuration to config.php

#config.php

'namespace' => [
    'controller' => 'Project\Controllers',
    'models' => 'Project\Models',
],

'elasticSearch' => [
    'hosts' => [getenv('ELASTIC_HOST')], //change to pass array
],

add queue to DI

#service.php

$di->set('queue', function () use ($config) {
    //Connect to the queue
    $queue = new Phalcon\Queue\Beanstalk\Extended([
        'host' => $config->beanstalk->host,
        'prefix' => $config->beanstalk->prefix,
    ]);

    return $queue;
});

Indexing

To create a Index in Elastic search first you will need to configure a CLI project and extend it from IndexTasksBuilder , after doing that just run the following command

php cli/app.php IndexBuilder createIndex ModelName 3

Where 4 is the normal of levels you want the relationships to index for example

Level 1
 Class A 
 - Relation BelongsTo Class B

 Level 2
 Class A 
 - Relation BelongsTo Class B
 - - Class B
 - - - Relation HasMany Class C

Level 3
 Class A 
 - Relation BelongsTo Class B
 - - Class B
 - - - Relation HasMany Class C
 - - - - Class C
 - - - - - Relation HasMany Class D

We can ignore a relationship if we specify on the options 'elasticSearch' => false

I wont recommend going beyond 4 levels if it not neede, it will use a lot of space.

If you get a error related to nestedLimit , you can use a 4th param to specify the amount the index limit

php cli/app.php IndexBuilder createIndex ModelName 3 100

Indexing Queue

Now that you created a Index we need to index the data, for that your model will need to extend from \Baka\Elasticsearch\Model . After every update | save we will send the information to a queue where the process will insert or update the info in elastic

<?php


class Users extends \Baka\Elasticsearch\Model
{

}

Queue

php cli/app.php IndexBuilder queue ModelName

Example: php cli/app.php IndexBuilder queue Users

Search

In order to simply searching en elastic search with elastic you most install this extension https://github.com/NLPchina/elasticsearch-sql

Now your search controller must use our trait

<?php

/**
 * Search controller
 */
class SearchController extends BaseController
{
    use \Baka\Elasticsearch\SearchTrait
}

And Follow the same query structure has Baka Http

https://api.dev/v1/search/indexName?sort=id|asc&q=(is_deleted:0,relationship.type_id:1)&fields=id,first_name,last_name,relationship.name,relationship.relationshipb.name

Example

https://api.dev/v1/search/users?sort=first_name|asc&q=(is_deleted:0,users_statuses_id:,first_name:,last_name:)&fields=id,first_name,last_name,potentiality,classification,userssprograms.id,events_satisfaction,is_prospect,gifts.name,is_key_users,dob,companies.name,companies.companiesstatuses.name,companies.rnc,position

Testing

codecept run

phalcon-elasticsearch's People

Contributors

fredpeal avatar gabbanaesteban avatar kaioken avatar leninpaulino avatar sparohawk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

hbikhish

phalcon-elasticsearch's Issues

Remove the need to call this function in order to delete a document

// TODO: Remove the need to call this function in order to delete a document
// Is not necesary create a whole object just to use the id in order to DELETE document. The ID should be set manually
$object->document();
$indexName = static::$indexName ?? self::generateIndexNameFromObject($object);


This issue was generated by todo based on a TODO comment in 97c9816 when #8 was merged. cc @bakaphp.

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.