Elastic search and Codeigniter (PHP)

核能气质少年 提交于 2019-12-03 13:23:20

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}';
Stevan

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);

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);

Call the functions passing the parameters without quotes (single or double):

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

In addition, for me it's easier 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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!