Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created February 16, 2010 17:20
Show Gist options
  • Select an option

  • Save hubgit/305696 to your computer and use it in GitHub Desktop.

Select an option

Save hubgit/305696 to your computer and use it in GitHub Desktop.

Revisions

  1. hubgit revised this gist Aug 7, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ElasticSearch.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ function __construct($server = 'http://localhost:9200'){
    }

    function call($path, $http = array()){
    if (!$this->index) throw new Exception('$this->index is not defined');
    if (!$this->index) throw new Exception('$this->index needs a value');
    return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
    }

  2. hubgit revised this gist Aug 7, 2010. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions ElasticSearch.php
    Original file line number Diff line number Diff line change
    @@ -10,18 +10,17 @@ function __construct($server = 'http://localhost:9200'){
    }

    function call($path, $http = array()){
    if (!$this->index) throw new Exception('$this->index is not defined');
    return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/
    function create($index){
    $this->index = $index;
    function create(){
    $this->call(NULL, array('method' => 'PUT'));
    }

    //curl -X DELETE http://localhost:9200/{INDEX}/
    function drop($index){
    $this->index = $index;
    function drop(){
    $this->call(NULL, array('method' => 'DELETE'));
    }

  3. hubgit revised this gist Aug 7, 2010. 1 changed file with 26 additions and 15 deletions.
    41 changes: 26 additions & 15 deletions ElasticSearch.php
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,28 @@
    <?php

    // http://www.elasticsearch.com/docs/elasticsearch/rest_api/

    class ElasticSearch {
    public $index;

    function __construct($server = 'http://localhost:9200'){
    $this->server = $server;
    }

    function call($path, $http = array()){
    return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
    return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/
    function create(){
    $this->call(NULL, array('method' => 'PUT'));
    //curl -X PUT http://localhost:9200/{INDEX}/
    function create($index){
    $this->index = $index;
    $this->call(NULL, array('method' => 'PUT'));
    }

    //curl -X DELETE http://localhost:9200/{INDEX}/
    function drop($index){
    $this->index = $index;
    $this->call(NULL, array('method' => 'DELETE'));
    }

    //curl -X GET http://localhost:9200/{INDEX}/_status
    @@ -20,22 +31,22 @@ function status(){
    }

    //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_count -d {matchAll:{}}
    function count(){
    return $this->call($this->type . '/_count', array('method' => 'GET', 'content' => '{ matchAll:{} }'));
    function count($type){
    return $this->call($type . '/_count', array('method' => 'GET', 'content' => '{ matchAll:{} }'));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/_mapping -d ...
    function map($data){
    return $this->call($this->type . '/_mapping', array('method' => 'PUT', 'content' => $data));
    function map($type, $data){
    return $this->call($type . '/_mapping', array('method' => 'PUT', 'content' => $data));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/{ID} -d ...
    function add($id, $data){
    return $this->call($this->type . '/' . $id, array('method' => 'PUT', 'content' => $data));
    function add($type, $id, $data){
    return $this->call($type . '/' . $id, array('method' => 'PUT', 'content' => $data));
    }

    //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_search?q= ...
    function query($q){
    return $this->call($this->type . '/_search?' . http_build_query(array('q' => $q)));
    function query($type, $q){
    return $this->call($type . '/_search?' . http_build_query(array('q' => $q)));
    }
    }
    }
  4. hubgit created this gist Feb 16, 2010.
    41 changes: 41 additions & 0 deletions ElasticSearch.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?php

    class ElasticSearch {
    function __construct($server = 'http://localhost:9200'){
    $this->server = $server;
    }

    function call($path, $http = array()){
    return json_decode(file_get_contents($this->server . '/' . $this->index . '/' . $path, NULL, stream_context_create(array('http' => $http))));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/
    function create(){
    $this->call(NULL, array('method' => 'PUT'));
    }

    //curl -X GET http://localhost:9200/{INDEX}/_status
    function status(){
    return $this->call('_status');
    }

    //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_count -d {matchAll:{}}
    function count(){
    return $this->call($this->type . '/_count', array('method' => 'GET', 'content' => '{ matchAll:{} }'));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/_mapping -d ...
    function map($data){
    return $this->call($this->type . '/_mapping', array('method' => 'PUT', 'content' => $data));
    }

    //curl -X PUT http://localhost:9200/{INDEX}/{TYPE}/{ID} -d ...
    function add($id, $data){
    return $this->call($this->type . '/' . $id, array('method' => 'PUT', 'content' => $data));
    }

    //curl -X GET http://localhost:9200/{INDEX}/{TYPE}/_search?q= ...
    function query($q){
    return $this->call($this->type . '/_search?' . http_build_query(array('q' => $q)));
    }
    }