elasticsearch在ubuntu16下的安装及php中的使用
来源:互联网 发布:战龙三国毒龙进阶数据 编辑:程序博客网 时间:2024/05/05 21:18
安装系统环境:ubuntu 16
因为ElasticSearch
底层其实就是lucene
,所以,需要java
环境支持,但各机体安装差异较大,在此就不累述了
1.包安装
在Ubuntu/Debian上使用apt-get安装
$ sudo apt-get install elasticsearch
或者使用 wget
安装
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-2.1.0.deb$ sudo dpkg -i elasticsearch-2.1.0.deb
目前ElasticSearch的最新稳定版为2.1.0,如果之后版本有升级,请将相应的版本号替换掉上面的2.1.0 。
设置开机自启动
$ sudo update-rc.d elasticsearch defaults 95 10$ sudo /etc/init.d/elasticsearch start
或者
$ sudo service elasticsearch start
测试
curl http://localhost:9200
你将看到类似以下的信息:
{
“status” : 200,
“name” : “Blue Streak”,
“cluster_name” : “elasticsearch”,
“version” : {
“number” : “2.1.0”,
“build_hash” : “00f95f4ffca6de89d68b7ccaf80d148f1f70e4d4”,
“build_timestamp” : “2016-02-02T09:55:30Z”,
“build_snapshot” : false,
“lucene_version” : “4.10.4”
},
“tagline” : “You Know, for Search”
}
或者
$ sudo service elasticsearch status
出现runing
就表示你安装成功了
高版本可能出现浏览器访问不了 http://localhost:9200 的情况
需要手动添加ip
$ cd /etc/elasticsearch$ sudo vim elasticsearch.yml添加 network.host: 192.168.1.168 (换成你自己的ip)
这样就安装好了,生成环境记得要把network.host
注释掉
安装好后的文件的位置一般如图所示:
2、插件安装
head插件:
$ cd /usr/share/elasticsearch$ ./bin/plugin -install mobz/elasticsearch-head$ sudo service elasticsearch restart
测试:http://{your ip}:9200/_plugin/head/
ik分词插件:
https://github.com/medcl/elasticsearch-analysis-ik
3、php操作
安装 elasticsearch-php
第三方库
引入到你的 composer.json
文件:
{ "require": { "elasticsearch/elasticsearch": "~1.0" } }
执行命令
$ composer update
项目文件中引入es-php库:
require 'vendor/autoload.php'; $client = new Elasticsearch\Client();
索引一个文档
$params = array(); $params['body'] = array('testField' => 'abc'); $params['index'] = 'my_index'; //索引名字自己定义$params['type'] = 'my_type'; $params['id'] = 'my_id'; //文档id$ret = $client->index($params);
获取一个文档
$getParams = array(); $getParams['index'] = 'my_index'; $getParams['type'] = 'my_type'; $getParams['id'] = 'my_id'; $retDoc = $client->get($getParams);
搜索一个文档
$searchParams['index'] = 'my_index'; $searchParams['type'] = 'my_type'; $searchParams['body']['query']['match']['testField'] = 'abc'; $retDoc = $client->search($searchParams);
删除一个文档
$deleteParams = array(); $deleteParams['index'] = 'my_index'; $deleteParams['type'] = 'my_type'; $deleteParams['id'] = 'my_id'; $retDelete = $client->delete($deleteParams);
删除一个索引
$deleteParams = array(); $deleteParams['index'] = 'my_index'; $client->indices()->delete($deleteParams);
demo.php
<?php require 'vendor/autoload.php'; //引入es-ph库$client = new Elasticsearch\Client(); //创建索引文档function index($client) { $params = array (); $params ['body'] = array ( 'testField' => 'abc' ); $params ['index'] = 'my_index'; $params ['type'] = 'my_type'; $params ['id'] = 'my_id'; try { $ret = $client->index($params); println("create index success"); } catch(Exception $e) { echo $e->getMessage(); } } //获取一个文档function get($client) { $getParams = array (); $getParams ['index'] = 'my_index'; $getParams ['type'] = 'my_type'; $getParams ['id'] = 'my_id'; $retDoc = $client->get($getParams); println($retDoc); }//搜索function search($client) { $searchParams ['index'] = 'my_index'; $searchParams ['type'] = 'my_type'; $searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc'; $retDoc = $client->search($searchParams); println($retDoc); } //删除一个文档function deleteDoc($client) { $deleteParams = array (); $deleteParams ['index'] = 'my_index'; $deleteParams ['type'] = 'my_type'; $deleteParams ['id'] = 'my_id'; $retDelete = $client->delete($deleteParams); println($retDelete); } //删除索引function deleteIndex($client) { $deleteParams = array (); $deleteParams ['index'] = 'my_index'; $retDelete = $client->indices()->delete($deleteParams); println($retDelete); } //创建一个索引function createIndex($client) { $indexParams ['index'] = 'my_index'; $indexParams ['body'] ['settings'] ['number_of_shards'] = 2; $indexParams ['body'] ['settings'] ['number_of_replicas'] = 0; $retCreate = $client->indices()->create($indexParams); println($retCreate); }
查看每个方法的运行结果:
index():
create index success
get():
{ "_index": "my_index", "_type": "my_type", "_id": "my_id", "_version": 1, "found": true, "_source": { "testField": "abc" } }
search():
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "my_id", "_score": 0.30685282, "_source": { "testField": "abc" } } ] } }
deleteDoc():
{ "found": true, "_index": "my_index", "_type": "my_type", "_id": "my_id", "_version": 2 }
deleteIndex():
{ "acknowledged": true }
createIndex():
{ "acknowledged": true }
- elasticsearch在ubuntu16下的安装及php中的使用
- windows下elasticSearch以及elasticSearch-php安装及使用
- IPython Notebook介绍及在Ubuntu16.04下的安装使用
- Matlab2014a在Ubuntu16.04中的安装及遇到的问题
- PCL1.8.0在Ubuntu16.04下的安装及测试
- Windows下Ubuntu16.04双系统的安装及使用
- caffe在ubuntu16.04下的安装
- matlab2016b在ubuntu16.04下的安装
- ElasticSearch概述及Linux下的单机ElasticSearch安装
- elasticsearch在windows下的安装
- Elasticsearch 在Windows下的安装
- elasticsearch在linux下的安装
- Elasticsearch 在Windows下的安装
- elasticsearch在windows下的安装
- Ubuntu16.04 上 Redis 4.0 及php-redis扩展的安装和使用
- memcache在ThinkPHP中的使用1---PHP下安装memcache
- g2o在ubuntu16.04安装及初步使用
- 使用aptitude在ubuntu16.04下安装有道词典
- rpc(Remote Procedure Call Protocol)简易代码实现
- 欢迎使用CSDN-markdown编辑器
- linux设备树的建立过程
- PWM
- Fragment和Acitiity布局交互使用
- elasticsearch在ubuntu16下的安装及php中的使用
- PreferenceActivity(首选项设置页)
- Redis常用内存优化手段与参数
- 46. 避免 CSS 表达式(7)
- svn:{path}containing working copy admin area is missing错误解决
- Linux 如何生成静态库和动态库
- sql 学习笔记
- 计时器、Times(倒计时) 主要应用于注册和等待加载
- Redux 入门教程(二):中间件与异步操作