php - Elasticsearch 和 Codeigniter (PHP)

标签 php codeigniter file-get-contents elasticsearch

我正在尝试将 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

我是不是误会/遗漏了什么?抱歉,我是 elasticsearch 的新手,也有点 php :P

因为如果我去:

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

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

最佳答案

我不太确定,但我猜你会调用 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}';

关于php - Elasticsearch 和 Codeigniter (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8097238/

相关文章:

php - 在私有(private)类中执行sql语句的问题

php - Drupal 8 数据库插入中的 fatal error : Call to undefined method,

CodeIgniter:在同一 Controller 中加载多个模型

php - 如何在与其他表有关系的字段中存储多个值

php - 以 url 作为变量的 file_get_contents

php - 检测字符串输入是否包含 HTML 的正确方法是什么?

PHP:匹配子网列表 (CIDR) 中的 IP

php codeigniter 计算行数

php - file_get_contents() 移除 XML 标签

php - 获取两个字符串之间的内容 PHP