7

我正在尝试将 ElasticSearch 与 Codeigniter 框架一起使用。

我所做的只是安装 ElasticSearch 并将在网络上找到的一个好的 PHP 库复制 (:P) 到 CI 库:

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

然后我正在尝试创建索引并简单地检索它们:

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

当我运行此代码时,它会显示错误:

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

我误会/错过了什么吗?对不起,但我是弹性搜索的新手,还有一点 php :P

因为如果我去:

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

//it prints in window
 {"_index":"comments","_type":"details","_id":"1","exists":false}
4

4 回答 4

3

我不太确定,但我猜你会打电话给add()

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

您不想在这里设置值。我会假设你会在 HTTP 错误请求之前收到一个 php 错误,但我会先试试这个:

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

你的add()方法已经知道参数代表什么,所以你只需要传递细节。

看起来您的 json 格式可能不正确。

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

// to
$data = '{author:"jhon",datetime:2001-09-09 00:02:04}';
于 2011-11-11T19:26:37.940 回答
3

一个老问题,但值得更新。使用这个插件

将您的composer.json文件放在应用程序文件夹中:

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

要安装composer和 elasticsearch 插件,请在 bash shell 中执行以下命令:

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

安装php-curl并重启 apache 服务器:

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

在库文件夹(codeigniter)中创建一个Elasticsearch.php文件,并将此代码放入:

<?php 
use Elasticsearch\ClientBuilder;

class Elasticsearch{

    public $client;

    public function __construct(){
        $this->client = ClientBuilder::create()->build();
    } 
}

您可以通过编辑配置文件夹中的 autoload.php 来自动加载 elasticsearch:

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

然后在您的模型/控制器中使用:

$this->elasticsearch->client->index($params);
于 2016-05-15T15:18:38.903 回答
2

您提供的 PHP 库未定义内容类型,这就是您收到消息的原因:“未指定内容类型”。

在此处检查 PHP 库并查看 README.txt。它有详细的注释,对初学者很有用,您可能想仔细阅读它们: https ://github.com/niranjan-uma-shankar/Elasticsearch-PHP-class

如果你使用这个库,那么你可以像这样初始化这个类:

$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);
于 2011-12-21T10:26:46.913 回答
0

调用不带引号(单引号或双引号)传递参数的函数:

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

此外,对我来说,使用数组然后将其编码为 json 对象更容易:

$data = array('author' => 'john', 'datetime' => '2001-09-09 00:02:04');
$this->elasticsearch->add(details, 1, json_encode($data));
于 2011-11-29T15:20:59.253 回答