弹性搜索和Codeigniter(PHP)

its*_*sme 7 php codeigniter file-get-contents elasticsearch

我正在尝试使用带有Codeigniter框架的ElasticSearch.

我所做的只是安装ElasticSearch并将在Web上找到的一个好的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)));
  }
}
Run Code Online (Sandbox Code Playgroud)

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

$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);
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我错误/错过了somenthing吗?抱歉,我是关于elasticsearch的新手,还有一点关于php:P

因为如果我去:

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

//it prints in window
 {"_index":"comments","_type":"details","_id":"1","exists":false}
Run Code Online (Sandbox Code Playgroud)

swa*_*ins 3

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

$this->elasticsearch->add($type ='details',$id = '1',$data);
Run Code Online (Sandbox Code Playgroud)

您不想在这里设置值。我假设您会在 HTTP 错误请求之前收到 php 错误,但我会先尝试以下操作:

$this->elasticsearch->add('details','1',$data);
Run Code Online (Sandbox Code Playgroud)

您的add()方法已经知道参数代表什么,因此您只需传递详细信息。

您的 json 格式可能不正确。

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

// to
$data = '{author:"jhon",datetime:2001-09-09 00:02:04}';
Run Code Online (Sandbox Code Playgroud)