Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elasticsearch - Turbinando sua aplicação PHP

Elasticsearch - Turbinando sua aplicação PHP

Utilizando Elasticsearch em suas aplicações PHP

Breno Oliveira

March 21, 2016
Tweet

More Decks by Breno Oliveira

Other Decks in Programming

Transcript

  1. Breno Oliveira @brenoholiveira [email protected] B R E N O O

    L I V E I R A Tech Leader at Moip ElasticSearch Turbinando sua aplicação PHP
  2. +

  3. +

  4. Single Term SELECT * FROM pedidos p INNER JOIN cliente

    ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%"
  5. multi Term SELECT * FROM pedidos p INNER JOIN cliente

    ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%" OR p.descricao LIKE "%batman%"
  6. ORDER ARRGH!!!! SELECT * FROM pedidos p INNER JOIN cliente

    ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%" OR p.descricao LIKE "%batman%" ORDER BY ??
  7. MELHORIAS • Micro serviço • Concorrência no banco de dados

    • No more JOINS • Full-Text Search • Melhorias de desempenho
  8. .nem nosql I T ' ’ N O T K

    E Y- VA L U E S T O R A G E
  9. curl -XPUT http://localhost:9200/tips/tip/1 -d '{"tip": "Using Elasticsearch in Production", "tags":

    ["protip","success"]} ' Endpoint Index Type Document ID Document { "_index": "tips", "_type": "tip", "_id": "1", "_version": 1, "created": true } Response
  10. $ curl http://localhost:9200/tips/tip/1?pretty { "_index": "tips", "_type": "tip", "_id": "1",

    "_version": 1, "found": true, "_source": { "tip": "Using Elasticsearch in Production", "tags": [ "protip", "success" ] } }
  11. vagrant@default:$ curl http://localhost:9200 { "status" : 200, "name" : "Corruptor",

    "cluster_name" : "elasticsearch", "version" : { "number" : "1.5.2", "build_hash" : "62ff9868b4c8a0c45860bebb259e21980778ab1c", "build_timestamp" : "2015-04-27T09:21:06Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline" : "You Know, for Search" }
  12. { "require": { "elasticsearch/elasticsearch": "~2.0@beta" } } composer.json $ curl

    -s http://getcomposer.org/installer | php $ php composer.phar install --no-dev
  13. <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create() ->build(); $params = array();

    $params['index'] = 'tips'; $params['type'] = 'tip'; $params['id'] = '1'; $result = $client->get($params) ?> <pre> <?= print_r($result); ?> </pre> exemplo1.php
  14. Array ( [_index] => tips [_type] => tip [_id] =>

    1 [_version] => 1 [found] => 1 [_source] => Array ( [tip] => Using Elasticsearch in Production [tags] => Array ( [0] => protip [1] => success ) ) )
  15. exemplo2.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create() ->build(); $params =

    [ 'index' => 'tips', 'type' => 'tip', 'id' => '2', 'body' => [ 'tip' => 'Using Marvel to get metrics from Elasticsearch', 'tags' => ['protip', 'metrics'] ] ]; // Documento indexado em tips/tip/2 $response = $client->index($params); ?> <pre> <?= print_r($response) ?> </pre>
  16. exemplo3.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create() ->build(); for($i =

    0; $i < 100; $i++) { $params['body'][] = [ 'index' => [ '_index' => 'jobs', '_type' => 'job', ] ]; $params['body'][] = [ 'title' => 'Job position '. $i, 'email' => 'email-'. $i . '@gmail.com' ]; } $responses = $client->bulk($params); ?>
  17. exemplo4.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $json = '{

    "query" : { "match" : { "tags" : "protip" } } }'; $params = [ 'index'=> 'tips', 'type' => 'tip', 'body' => $json ]; $responses = $client->search($params); ?> <pre> <?= print_r($responses) ?> </pre>
  18. exemplo5.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $params = [

    'index' => 'tips', 'type' => 'tip', 'id' => '3', 'body' => [ 'tip' => 'Wrong insert', 'tags' => ['missing', 'error', 'no_logs'] ] ]; $response_insert = $client->index($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '3' ]; $responde_delete = $client->delete($params); ?>
  19. exemplo6.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $params = [

    'index' => 'tips', 'type' => 'tip', 'id' => '4', 'body' => [ 'tip' => 'Loren Ipsum', 'tags' => ['lorem'] ] ]; $response_insert = $client->index($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '4', 'body' => [ 'doc' => [ 'text' => 'lipsum generator' ] ] ]; $response_update = $client->update($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '4' ]; $response_search = $client->get($params); ?>
  20. exemplo7.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $json = '{

    "my-suggestion" : { "text" : "Elastiscearch", "term" : { "field" : "tip" } } }'; $params = [ 'index' => 'tips', 'body' => $json ]; $response = $client->suggest($params); ?>
  21. exemplo8.php <?php require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $json = '{

    "query" : { "more_like_this" : { "like_text" : "In production", "min_term_freq" : 1, "min_doc_freq" : 1 } } }'; $params = [ 'index' => 'tips', 'type' => 'tip', 'body' => $json ]; $response = $client->search($params); ?>
  22. E-commerce app Base de Dados Elasticsearch rabbitmq Publish Message php

    worker Consume Message & Sync Cadastrar novo produto