我正在尝试使用ElasticSearch与Codeigniter框架。
我所做的只是安装ElasticSearch和复制(:p)一个好的PHP库在网上找到的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
我搞错了/错过了什么吗?不好意思,我是elasticsearch的新手,也对php:P
有一点了解因为如果我去:
http://localhost:9200/comments/details/1
//it prints in window
{"_index":"comments","_type":"details","_id":"1","exists":false}
我不太确定,但我猜你会打电话给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}';
一个老问题,但值得更新。使用这个插件。
把你的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 ElasticsearchClientBuilder;
class Elasticsearch{
public $client;
public function __construct(){
$this->client = ClientBuilder::create()->build();
}
}
你可以通过编辑配置目录下的autolload .php自动加载elasticsearch:
$autoload['libraries'] = array(/*[some other library,]*/'elasticsearch');
然后在你的模型/控制器中使用:
$this->elasticsearch->client->index($params);
您给出的PHP库没有定义内容类型,这就是为什么您得到消息:" content-type not specified"。
查看这里的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));