7

Codeigniter フレームワークで ElasticSearch を使用しようとしています。

私がしたことは、ElasticSearch をインストールし、Web で見つかった適切な PHP ライブラリを CI ライブラリにコピー (:P) しただけです。

    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}
4

4 に答える 4

3

よくわかりませんが、私の推測では、あなたの呼び出しは次の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}';
于 2011-11-11T19:26:37.940 に答える
3

古い質問ですが、更新する価値があります。このプラグインを使用します。

composer.jsonファイルをアプリケーション フォルダーに配置します。

{
     "require": {
     "elasticsearch/elasticsearch": "~2.0"
     }
}

composerと elasticsearch プラグインをインストールするには、bash シェルで次のコマンドを実行します。

curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev

php-curlApache サーバーをインストールして再起動します。

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);
于 2016-05-15T15:18:38.903 に答える
2

指定した 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);
于 2011-12-21T10:26:46.913 に答える
0

引用符なし(シングルまたはダブル)でパラメーターを渡す関数を呼び出します。

$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));
于 2011-11-29T15:20:59.253 に答える