【问题标题】:Elastic search and Codeigniter (PHP)弹性搜索和 Codeigniter (PHP)
【发布时间】:2011-12-27 04:32:02
【问题描述】:

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

【问题讨论】:

标签: php codeigniter file-get-contents elasticsearch


【解决方案1】:

我不太确定,但我猜你会打电话给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}';

【讨论】:

  • 感谢您的建议,是的,我错过了有关我的 sintax 的一些基础知识,但错误仍然存​​在,我认为我错过了一些更重要的东西,嗯 ...
  • @sbaaaang 你找到解决方案了吗?我无法插入数据
  • @dhpratik nope,我不得不承认我当时放弃了,那时我从未使用过 elasticsearch,因为 CI 有太多麻烦:P 下一个 PHP 项目我将转移到 Laravel目前对我来说似乎好多了
  • @dhpratik 你有什么错误吗?你能展示一些我猜的东西吗
  • 它没有给出任何错误。但是当尝试将数据添加到索引时,它没有被插入。后来我改变了'add'的库函数。添加功能使用http put,我将其更改为post
【解决方案2】:

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

将您的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();
    } 
}

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

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

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

$this->elasticsearch->client->index($params);

【讨论】:

    【解决方案3】:

    您提供的 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);
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-04
        • 2021-11-23
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        • 2014-03-26
        相关资源
        最近更新 更多