Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search and Codeigniter (PHP)

I'm trying using ElasticSearch with Codeigniter framework.

What i did is just install ElasticSearch and copyed ( :P ) a good PHP library found on the web to 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 creating indexes and simply retrieve 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 show 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

does i'm mistaking/missed somenthing? sorry but i'm newbie about elasticsearch and also a little bit with php :P

cause if i go to:

http://localhost:9200/comments/details/1

//it prints in window
 {"_index":"comments","_type":"details","_id":"1","exists":false}
like image 427
itsme Avatar asked Nov 11 '11 16:11

itsme


People also ask

What is Elasticsearch in CodeIgniter?

Elasticsearch is an open-source search server based on Apache Lucene. ElasticSearch in Codeigniter enables you to perform super quick full-text and other complex searches. It also incorporates a REST API which allows you to effortlessly issue requests for creating, deleting, overhauling, and recovering information.

How to connect to Elasticsearch using PHP?

This is the official PHP client for Elasticsearch. Using this client assumes that you have an Elasticsearch server installed and running. You can install the client in your PHP project using composer: After the installation you can connect to Elasticsearch using the ClientBuilder class.

Why does CodeIgniter use cookies?

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance. Elasticsearch is an open-source search server based on Apache Lucene. ElasticSearch in Codeigniter enables you to perform super quick full-text and other complex searches.

How to install Elasticsearch on cloudways?

Elasticsearch is built on Apache Lucene and was first released in 2010 by Elasticsearch N.V. After you have created an account with Cloudways, choose your desired cloud provider from Vultr, DigitalOcean, Amazon, or Google. You need to select the PHP Stack. Now click on Launch Server. Wait till your server is fully launched.


3 Answers

I'm not quite sure, but my guess would be your call to add():

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

You don't want to be setting values here. I would assume you'd get a php error before an HTTP bad request, but I would try this first:

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

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

Also

It looks like your json might be malformed.

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

// to
$data = '{author:"jhon",datetime:2001-09-09 00:02:04}';
like image 65
swatkins Avatar answered Oct 22 '22 07:10

swatkins


The PHP library that you have given is not defining content-type, that's why you are getting the message: "Content-type not specified".

Check the PHP library here and also go through the README.txt. It has detailed notes which will be useful to beginners and you may want to go through them: https://github.com/niranjan-uma-shankar/Elasticsearch-PHP-class

If you use this library, then you can initialize the class like this:

$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);
like image 2
Ninja Avatar answered Oct 22 '22 09:10

Ninja


An old question, but deserves an update. Use this plugin.

Put your composer.json file in the application folder:

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

To install composer and elasticsearch plugin execute these commands in bash shell:

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

Install php-curl and restart apache server:

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

Create a Elasticsearch.php file in the libraries folder (codeigniter), and put this code inside:

<?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 config folder:

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

Then in your model/controller use:

$this->elasticsearch->client->index($params);
like image 3
Stevan Avatar answered Oct 22 '22 09:10

Stevan