首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastic search和Codeigniter (PHP)

Elastic search和Codeigniter (PHP)
EN

Stack Overflow用户
提问于 2011-11-12 00:48:28
回答 4查看 13.4K关注 0票数 7

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

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

代码语言:javascript
复制
    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)));
  }
}

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

代码语言:javascript
复制
$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);

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

代码语言:javascript
复制
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也有一点了解。

因为如果我去:

代码语言:javascript
复制
http://localhost:9200/comments/details/1

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

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-12 03:26:38

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

代码语言:javascript
复制
$this->elasticsearch->add($type ='details',$id = '1',$data);

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

代码语言:javascript
复制
$this->elasticsearch->add('details','1',$data);

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

也叫

看起来你的json可能是畸形的。

代码语言:javascript
复制
// change
$data = '{author:jhon,datetime:2001-09-09 00:02:04}';

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

Stack Overflow用户

发布于 2016-05-15 23:18:39

这是一个老问题,但需要更新一下。使用此plugin

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

代码语言:javascript
复制
{
     "require": {
     "elasticsearch/elasticsearch": "~2.0"
     }
}

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

代码语言:javascript
复制
curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev

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

代码语言:javascript
复制
sudo apt-get install php5-curl
sudo service apache2 restart

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

代码语言:javascript
复制
<?php 
use Elasticsearch\ClientBuilder;

class Elasticsearch{

    public $client;

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

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

代码语言:javascript
复制
$autoload['libraries'] = array(/*[some other library,]*/'elasticsearch');

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

代码语言:javascript
复制
$this->elasticsearch->client->index($params);
票数 3
EN

Stack Overflow用户

发布于 2011-12-21 18:26:47

您给出的PHP库没有定义content-type,这就是为什么您会收到这样的消息:"Content-type not specified“。

在这里查看PHP库,也可以查看README.txt。它有详细的注释,对初学者很有用,你可能想通读一遍:https://github.com/niranjan-uma-shankar/Elasticsearch-PHP-class

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

代码语言:javascript
复制
$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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8097238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档