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

TechTalk: ElasticSearch 2014

TechTalk: ElasticSearch 2014

Very basic introduction and a small introduction into elastichsearch-php SDK.

Avatar for Jens Kohl

Jens Kohl

June 10, 2014
Tweet

Other Decks in Technology

Transcript

  1. Facts Plattform — Default ports: — 9200: HTTP interface (REST

    interface) — 9300: TCP interface (instance interconection) — Needs 1GB memory by default (per instance!)
  2. Facts (2) Cluster — Default configuration expects a three (or

    more) node cluster — For local development you adapt to only expect one node — Or just run 3 independent instances of elasticsearch Note, that for development on a local machine, with small indices, it usually makes sense to "disable"
  3. Facts (3) Fail over — Nodes find themselves automatically via

    multicast and elect one master node — If the master nodes became unavailable the remaining nodes elect a new master node — If the old master comes back online again, it will not promoted to master again it remains a regular node in the cluster
  4. Configuration Node types Every node can be configured to allow

    or deny being eligible as the master, and to allow or deny to store the data. — Standard node — Coordinator node — Workhorse node — Search load balancer node
  5. Configuration (2) Node types Standard node The default mode. Allow

    this node to become master and store data. — node.master: true — node.data: true
  6. Configuration (3) Node types Coordinator node You want this node

    to only serve as a master: to not store any data and to have free resources. This will be the "coordinator" of your cluster. — node.master: true — node.data: false
  7. Configuration (4) Node types Workhorse node (Data node) You want

    this node to never become a master node, only to hold data. This will be the "workhorse" of your cluster. — node.master: false — node.data: true
  8. Configuration (5) Node types Search load balancer node You want

    this node to be neither master nor data node, but to act as a "search load balancer" (fetching data from nodes, aggregating results, etc.) — node.master: false — node.data: false
  9. Configuration (6) Discovery Just magic or handwritten configs: Unicast discovery

    allows to explicitly control which nodes will be used to discover the cluster. It can be used when multicast is not present, or to restrict the cluster communication-wise. discovery.zen.ping.multicast.enabled: false discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300", "127.0.0.1:9301", "127.0.0.1:9302", "127.0.0.1:9304"]
  10. cURL interface The following is equivalent — Browser: http://localhost:9200/_cluster/health? pretty

    — command line: curl -X GET localhost:9200/ _cluster/health?pretty But: Browser can only emit GET requests. Needs plugins (like Postman) to emit POST, PUT, DELETE and HEAD requests.
  11. cURL interface (2) Import new document curl -X PUT localhost:9200/my_index/my_type/my_id_

    -d '{ "key1": "value1", "key2": "value2" }' The my_id ID is optional upon creation/importing . If no ID is given elasticsearch generates one for you. Obviously one ID is need to edit or delete a document.
  12. cURL interface (3) Deletion Document curl -X DELETE localhost:9200/my_index/my_type/my_id Index

    curl -X DELETE localhost:9200/my_index It's also possible to use an * as a wildcard: curl -X DELETE localhost:9200/my_*
  13. cURL cluster health curl -X GET localhost:9200/_cluster/health?pretty { "cluster_name" :

    "elasticsearch_jkohl", "status" : "green", "timed_out" : false, "number_of_nodes" : 4, "number_of_data_nodes" : 4, "active_primary_shards" : 26, "active_shards" : 51, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0 }
  14. cURL documents $ curl -X GET localhost:9200/_cat/indices?v health index pri

    rep docs.count docs.deleted store.size pri.store.size green muz 5 1 111 1 107.2kb 53.7kb green js_phpunit 1 0 0 0 99b 99b green addresses 5 1 1 0 7kb 3.5kb green techtalk 5 1 1319147 4 1gb 521.8mb green js 5 1 0 0 810b 415b $ curl -X GET localhost:9200/_cat/indices/techtalk2?v health index pri rep docs.count docs.deleted store.size pri.store.size green techtalk 5 1 1291161 2 1gb 512.4mb
  15. cURL mapping $ curl -X GET localhost:9200/muz/_mapping?pretty { "muz" :

    { "mappings" : { "people" : { "properties" : { "mail" : { "type" : "string" }, "name" : { "type" : "string" }, "phone" : { "type" : "string" }, "query" : { "properties" : { "match_all" : { "type" : "object" } } }, "userid" : { "type" : "string" } } } } } } echo http://localhost:9200/muz/people
  16. PHP: elasticsearch/elasticsearch via Composer $ mkdir -p /tmp/estest01 $ cd

    /tmp/estest01 $ composer require --prefer-dist elasticsearch/elasticsearch:~1 $ tree -L 2 . !"" composer.json !"" composer.lock #"" vendor !"" autoload.php !"" composer !"" elasticsearch !"" guzzle !"" monolog !"" pimple !"" psr #"" symfony
  17. PHP (2) Connection to cluster $ vim index.php require_once ('vendor/autoload.php');

    $param['host'] = ['127.0.0.1:9200', 'otherHost:9200']; $client = Elasticsearch\Client($param); assert($client); // client not null $client->create($createParams); // optional $client->index($indexParams); $client->search($searchParams); $client->suggest($suggestParams);
  18. PHP (3) Index a document // … $indexParams = [

    'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', // optional 'body' => [ 'key1' => 'value1', 'key2' => 'value2' ] ]; $client->index($indexParams); $response = $client->index($indexParams); // alternative
  19. Documentation — ElasticSearch guides (entry point) — http://www.elasticsearch.org/guide/http:// www.elasticsearch.org/guide/en/elasticsearch/ client/php-api/current/index.html

    — PHP-API documentation — http://www.elasticsearch.org/guide/en/ elasticsearch/client/php-api/current/ index.html