elasticsearch在ubuntu16下的安装及php中的使用

安装系统环境:ubuntu 16
因为ElasticSearch底层其实就是lucene,所以,需要java环境支持,但各机体安装差异较大,在此就不累述了

# install java
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install  -y oracle-java8-installer
sudo apt-get install -y oracle-java8-set-default

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注释掉

安装好后的文件的位置一般如图所示:
file direcotory

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  
}  
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值