项目作者: bakaphp

项目描述 :
Elastic Search library for PhalconPHP Api's
高级语言: PHP
项目地址: git://github.com/bakaphp/phalcon-elasticsearch.git
创建时间: 2018-05-07T02:41:23Z
项目社区:https://github.com/bakaphp/phalcon-elasticsearch

开源协议:MIT License

下载


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

  1. #config.php
  2. 'namespace' => [
  3. 'controller' => 'Project\Controllers',
  4. 'models' => 'Project\Models',
  5. ],
  6. 'elasticSearch' => [
  7. 'hosts' => [getenv('ELASTIC_HOST')], //change to pass array
  8. ],

add queue to DI

  1. #service.php
  2. $di->set('queue', function () use ($config) {
  3. //Connect to the queue
  4. $queue = new Phalcon\Queue\Beanstalk\Extended([
  5. 'host' => $config->beanstalk->host,
  6. 'prefix' => $config->beanstalk->prefix,
  7. ]);
  8. return $queue;
  9. });

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

  1. Level 1
  2. Class A
  3. - Relation BelongsTo Class B
  4. Level 2
  5. Class A
  6. - Relation BelongsTo Class B
  7. - - Class B
  8. - - - Relation HasMany Class C
  9. Level 3
  10. Class A
  11. - Relation BelongsTo Class B
  12. - - Class B
  13. - - - Relation HasMany Class C
  14. - - - - Class C
  15. - - - - - 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

  1. <?php
  2. class Users extends \Baka\Elasticsearch\Model
  3. {
  4. }

Queue

php cli/app.php IndexBuilder queue ModelName

Example:
php cli/app.php IndexBuilder queue Users

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

  1. <?php
  2. /**
  3. * Search controller
  4. */
  5. class SearchController extends BaseController
  6. {
  7. use \Baka\Elasticsearch\SearchTrait
  8. }

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

  1. codecept run