Elastic Search and Codeigniter (PHP)

I am trying to use ElasticSearch with a Codeigniter card.

I just installed ElasticSearch and copied (: P) the good PHP library found on the Internet to the CI libraries:

class Elasticsearch { public $config_file = 'elasticsearch'; public $index; function __construct($index = false){ $CI =& get_instance(); $CI->config->load($this->config_file); $this->server = $CI->config->item('es_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){ echo $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))); } } 

then I'm trying to create indexes and just get them:

 $this->load->library('elasticsearch'); $this->elasticsearch->index = 'comments'; $this->elasticsearch->create(); $data = '{author:jhon,datetime:2001-09-09 00:02:04}'; $this->elasticsearch->add($type ='details',$id = '1',$data); 

when I run this code, it shows me errors:

 A PHP Error was encountered Severity: Warning Message: file_get_contents(http://localhost:9200/comments/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request Filename: libraries/Elasticsearch.php Line Number: 19 A PHP Error was encountered Severity: Notice Message: file_get_contents() [function.file-get-contents]: Content-type not specified assuming application/x-www-form-urlencoded Filename: libraries/Elasticsearch.php Line Number: 19 A PHP Error was encountered Severity: Warning Message: file_get_contents(http://localhost:9200/comments/details/1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request Filename: libraries/Elasticsearch.php Line Number: 19 

Am I mistaken / missing? sorry, but I'm new to elasticsearch, and also a bit with php: P

call if I go to:

 http://localhost:9200/comments/details/1 //it prints in window {"_index":"comments","_type":"details","_id":"1","exists":false} 
+6
source share
4 answers

I'm not quite sure, but I would prefer your add() call:

 $this->elasticsearch->add($type ='details',$id = '1',$data); 

You do not want to set values ​​here. I would suggest that you get a php error before an invalid HTTP request, but I would try this first:

 $this->elasticsearch->add('details','1',$data); 

Your add() method already knows what the arguments are, so you just need to pass the data.

AND

It looks like your json might be garbled.

 // change $data = '{author:jhon,datetime:2001-09-09 00:02:04}'; // to $data = '{author:"jhon",datetime:2001-09-09 00:02:04}'; 
+2
source

The PHP library you specified does not determine the type of content, so you get a message: "Content type not specified."

Check out the PHP library here, and also check out the README.txt file. It has detailed notes that will be useful for beginners, and you can go through them: https://github.com/niranjan-uma-shankar/Elasticsearch-PHP-class

If you use this library, you can initialize the class as follows:

 $this->load->library('elasticsearch'); $elasticSearch = new $this->elasticsearch; $elasticsearch->index = 'comments'; $elasticsearch->type = 'details'; $elasticsearch->create(); $data = '{"author" : "jhon", "datetime" : "2001-09-09 00:02:04"}'; $elasticsearch->add(1, $data); 
+1
source

Call functions that pass parameters without quotes (one or two):

 $this->elasticsearch->add(details, 1, $data); 

Also, it’s easier for me to work with arrays and then encode it into json objects:

 $data = array('author' => 'john', 'datetime' => '2001-09-09 00:02:04'); $this->elasticsearch->add(details, 1, json_encode($data)); 
0
source

Old question, but worth updating. Use this plugin .

Place the composer.json file in the application folder.

 { "require": { "elasticsearch/elasticsearch": "~2.0" } } 

To install composer and the elasticsearch plugin, run the following commands in the bash shell:

 curl -s http://getcomposer.org/installer | php php composer.phar install --no-dev 

Install php-curl and restart the Apache server:

 sudo apt-get install php5-curl sudo service apache2 restart 

Create the Elasticsearch.php file in the library folder (codeigniter) and put in it:

 <?php use Elasticsearch\ClientBuilder; class Elasticsearch{ public $client; public function __construct(){ $this->client = ClientBuilder::create()->build(); } } 

You can autoload elasticsearch by editing autoload.php in the config folder:

 $autoload['libraries'] = array(/*[some other library,]*/'elasticsearch'); 

Then in your model / controller use:

 $this->elasticsearch->client->index($params); 
0
source

Source: https://habr.com/ru/post/901309/


All Articles