[英]Elastic search and Codeigniter (PHP)
我正在嘗試使用帶有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)));
}
}
然后我正在嘗試創建索引並簡單地檢索它們:
$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
我錯誤/錯過了somenthing嗎? 抱歉,我是關於elasticsearch的新手,還有一點關於php:P
因為如果我去:
http://localhost:9200/comments/details/1
//it prints in window
{"_index":"comments","_type":"details","_id":"1","exists":false}
一個老問題,但值得更新。 使用此插件 。
將您的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
在libraries文件夾(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);
我不太確定,但我的猜測是你對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}';
您提供的PHP庫未定義內容類型,這就是您收到消息的原因:“未指定Content-type”。
在這里檢查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);
調用傳遞參數的函數,不帶引號(單引號或雙引號):
$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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.