您的位置:首页 > 编程语言 > PHP开发

composer控制版本,php使用Elasticsearch

2016-10-18 13:12 375 查看
首先win安装composer:

Composer 是 PHP的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。Composer不是一个包管理器。是的,它涉及"packages"和"libraries",但它在每个项目的基础上进行管理,在你项目的某个目录中(例如vendor)进行安装。默认情况下它不会在全局安装任何东西。因此,这仅仅是一个依赖管理。

下载资源:http://download.csdn.net/detail/xujingzhong0077/9656804(百度云上有)

切记打开PHP扩展的openssl扩展!

验证是否成功。打开win+R
》 cmd 输入composer,显示如下界面 表示安装成功!



1、建项目文件test

2、直接在项目文件中composer install
安装生成composer.phar文件

3、在里面放入一个命名为composer.json的文件,文件内容为:

{

"require":{
"elasticsearch/elasticsearch" : "~1.2"
}
}
4、然后php composer.phar install就安装好了

下面是php简单使用Elasticsearch

<?php
require_once('./vendor/autoload.php');
class ES
{
public $params;
public $client;
public function __construct()
{
$this->params['hosts'] = array(
'192.168.145.128:9200'
);
//            $this->client = new \Elasticsearch\Client($this->params);
}

public function search(){
$client = new \Elasticsearch\Client($this->params);
echo "<pre>";
//            //插入数据(没索引建索引,没给id默认生成id)
//            $params = [
//                'index' => 'my_index',
//                'type' => 'my_type',
//                'body' => ['testField' => 'abc']
//            ];
//            $response = $client->index($params);
//            print_r($response);die;

//search搜索获取数据
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'testField' => 'abc', //搜索testField字段=abc
]
],
'sort' => [
'testField' => [
'order' => 'asc',  //搜索testField字段排序
],
],
'from' => 2,
'size' => 1,
]
];
$response = $client->search($params);
print_r($response);die;

//indices为建立索引(库)
$params = [
'index' => 'my_index',
//                'body' => [
//                    'settings' => [
//                        'number_of_shards' => 2,//分片参数
//                        'number_of_replicas' => 0
//                    ]
//                ]
];
$response = $client->indices()->create($params);
print_r($response);die;

//indices为删除索引(库)
$Params = [
'index' => 'my_index'
];
$response = $client->indices()->delete($Params);
print_r($response);die;

//deleteById 删除指定id数据
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',  //必填
];
$response = $client->delete($params);
print_r($response);die;

//search搜索获取数据
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'testField' => 'abc', //搜索testField字段=abc
]
],
'sort' => [
'testField' => [
'order' => 'asc',  //搜索testField字段排序
],
],
'from' => 2,    //limit
'size' => 1,
]
];
$response = $client->search($params);
print_r($response);die;

//getById 获取指定id的数据
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',    //必填
];
$response = $client->get($params);
print_r($response);die;

//插入数据(没索引建索引,没给id默认生成id)
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);die;

}

public function _encoding($data, $from='gbk', $to='utf-8')
{
if(is_array($data))
{
foreach($data as $key=>$value)
{
$data[$key] =  $this->_encoding($value, $from, $to);
}
return $data;
}else{
return iconv($from,$to,$data);
}
}
}

$ES = new ES();
$ES->search();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: