- <?php
- Class ElasticSearch {
- public $index;
- function __construct ($server = ' http://localhost:9200 ') {
- $this->server = $server;
- }
- function Call ($path, $http = Array ()) {
- 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)));
- }
- //curl-x PUT http://localhost:9200/{index}/
- function Create () {
- $this->call (NULL, Array (' method ' = ' PUT '));
- }
- //curl-x DELETE http://localhost:9200/{index}/
- function Drop () {
- $this->call (NULL, Array (' method ' = ' DELETE '));
- }
- //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 ($type) {
- return $this->call ($type. '/_count ', Array (' method ' = ' GET ', ' content ' = ' {matchall:{}} ');
- }
- //curl-x PUT http://localhost:9200/{index}/{type}/_mapping-d ...
- 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 ($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 ($type, $q) {
- return $this->call ($type. '/_search '. Http_build_query (Array (' q ' = $q)));
- }
- }
Distributed search engine Elasticsearch PHP class encapsulation using native API