弹性搜索和Codeigniter(PHP) [英] Elastic search and Codeigniter (PHP)

查看:118
本文介绍了弹性搜索和Codeigniter(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  class Elasticsearch {

public $ config_file ='elasticsearch';
public $ index;

函数__construct($ index = false){
$ CI =& get_instance();
$ CI-> config-> load($ this-> config_file);
$ this-> server = $ CI-> config-> item('es_server');

}

函数调用($ path,$ http = array()){
if(!$ this-> index)throw new Exception $ this-> index需要一个值');
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-> ;调用( '_状态');
}

// curl -X GET http:// localhost:9200 / {INDEX} / {TYPE} / _count -d {matchAll:{}}
函数计数($ 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'=> $数据));
}

// curl -X GET http:// localhost:9200 / {INDEX} / {TYPE} / _search?q = ...
function query类型,$ 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);

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

 遇到PHP错误

严重性:警告

消息:file_get_contents(http:// localhost:9200 / comments /)[function.file-get-contents]:无法打开流:HTTP请求失败! HTTP / 1.0 400错误请求

文件名:libraries / Elasticsearch.php

行号:19
遇到PHP错误

严重性:通知

消息:file_get_contents()[function.file-get-contents]:内容类型未指定假设应用程序/ x-www-form-urlencoded

文件名:library / Elasticsearch.php

行号:19
遇到PHP错误

严重性:警告

消息:file_get_contents( http:// localhost:9200 / comments / details / 1)[function.file-get-contents]:无法打开流:HTTP请求失败! HTTP / 1.0 400错误请求

文件名:libraries / Elasticsearch.php

行号:19

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



如果我去:

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

//它在窗口中打印
{_index:评论,_ type:details,_ id:1,exists:false}


解决方案

我不太确定,但我的猜测将是您致电 add()

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

您不想在此设置值。我会假设你会在HTTP不正确的请求之前收到一个php错误,但是我会尝试这个:

  $ this- > elasticsearch->添加( '详情', '1',$数据); 

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



另外



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

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


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}

解决方案

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

这篇关于弹性搜索和Codeigniter(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆