2017 © Pedro Peláez
 

library elasticsearch

Baka Elasticsearch component

image

baka/elasticsearch

Baka Elasticsearch component

  • Sunday, June 17, 2018
  • by mctekk
  • Repository
  • 3 Watchers
  • 0 Stars
  • 38 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 533 % Grown

The README.md

Baka Phalcon Elastic Search

Phalcon Elastic Search package to index / query model with relationship easily, (*1)

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",", (*2)

Add elastic configuration to config.php, (*3)


#config.php 'namespace' => [ 'controller' => 'Project\Controllers', 'models' => 'Project\Models', ], 'elasticSearch' => [ 'hosts' => [getenv('ELASTIC_HOST')], //change to pass array ],

add queue to DI, (*4)


#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, (*5)

php cli/app.php IndexBuilder createIndex ModelName 3, (*6)

Where 4 is the normal of levels you want the relationships to index for example, (*7)

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, (*8)

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

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

php cli/app.php IndexBuilder createIndex ModelName 3 100, (*11)

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, (*12)


<?php class Users extends \Baka\Elasticsearch\Model { }

Queue, (*13)

php cli/app.php IndexBuilder queue ModelName, (*14)

Example: php cli/app.php IndexBuilder queue Users, (*15)

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

Now your search controller must use our trait, (*17)


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

And Follow the same query structure has Baka Http, (*18)

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, (*19)

Example, (*20)

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, (*21)

Testing

codecept run

The Versions