Centos安装elasticsearch,php连接使用
一.下载安装JAVA
下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
# 如果用`wget`方式下载解压时会出现错误`gzip: stdin: not in gzip format`
# 这里建议在浏览器里下载后传到服务器上,去Oracle官网下载过jdk的童鞋应该都知道,
# 下载之前需要同意Oracle的安装协议,不然不能下载,但是用wget的方式,默认是不同意,
# 虽然能下载下来,但是下载下来的文件会有问题,所以在Linux上解压一直失败。
tar -xzvf jdk-13.0.1_linux-x64_bin.tar.gz
vim /etc/profile
export JAVA_HOME=/www/soft/java/jdk-13.0.1
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
# ln -s /www/soft/java/jdk-13.0.1/bin/java /usr/bin/java
java -version 
二.下载安装ElasticSearch
下载地址:https://www.elastic.co/cn/downloads/elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz
tar -xzvf elasticsearch-7.4.2-linux-x86_64.tar.gz
# elasticsearch不能用root用户运行,要先创建用户
groupadd www
useradd -g www www
chown -R www:www *
su www
cd /www/soft/elasticsearch/elasticsearch-7.4.2
sh bin/elasticsearch -d
ps -ef | grep elastic
curl http://127.0.0.1:9200
# 出现如下数据,说明elasticsearch已经搭建好了
{
  "name" : "hckj998",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "vDHMfXLhR_aha7EOeClebg",
  "version" : {
    "number" : "7.4.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    "build_date" : "2019-10-28T20:40:44.881551Z",
    "build_snapshot" : false,
    "lucene_version" : "8.2.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
三.设置开机启动
cd /www/soft/elasticsearch/elasticsearch-7.4.2
#在bin目录下新建start.sh
vim start.sh
#!/bin/bash
cd /www/soft/elasticsearch/elasticsearch-7.4.2
su www -c "sh bin/elasticsearch -d"
#在bin目录下新建stop.sh
vim stop.sh
#!/bin/bash
ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}' | xargs kill -9
vim /etc/rc.d/rc.local
#在/etc/rc.d/rc.local尾部添加一行
/www/soft/elasticsearch/elasticsearch-7.4.2/start.sh
#修改权限
chmod +x /etc/rc.d/rc.local
#查看rc.local服务的状态
systemctl list-units --type=service
#如果改服务显示failed,则需要开启该服务
systemctl status rc-local.service
#如果上面的命令这里出现Active: failed (Result: exit-code)
#则说明启动脚本有问题,根据错误提示,修改start.sh脚本就行
#最后开启服务器
systemctl enable rc-local.service
四.安装ik中文分词
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.2/elasticsearch-analysis-ik-7.4.2.zip
cd /www/soft/elasticsearch
elasticsearch-7.4.2/bin/elasticsearch-plugin install -b file:///www/soft/elasticsearch/elasticsearch-analysis-ik-7.4.2.zip
五.验证
#创建索引
curl -X PUT  http://127.0.0.1:9200/test_index?pretty -v -H "Content-Type: application/json" -d '
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings": {
        "_source": {
            "enabled": true
        },
        "properties": {
            "id": {
                "type": "integer"
            },
            "title": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "image": {
                "type": "text"
            },
            "author": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "words": {
                "type": "integer"
            },
            "description": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "type_name": {
                "type": "text"
            }
        }
    }
}
'
#查看所有索引
curl -XGET http://localhost:9200/_aliases?pretty
curl -XGET '127.0.0.1:9200/_cat/indices?pretty'
#查看索引定义
curl -XGET "http://127.0.0.1:9200/test_index?pretty"
#查看索引mapping
curl -XGET "http://127.0.0.1:9200/test_index/_mapping?pretty" 
#删除索引
curl -XDELETE "http://127.0.0.1:9200/test_index?pretty"
#获取文档数量
curl -XPOST "http://127.0.0.1:9200/poetry/_doc/count" -H "Content-Type: application/json" -d '
{
    "query": {
        "match_all": {}
    }
}' 
#查询所有文档
curl -XGET "http://127.0.0.1:9200/poetry/_search" -v -H "Content-Type: application/json" -d '
{
    "query": {
        "match_all": {}
    }
}'
#删除所有文档
curl -XPOST "http://127.0.0.1:9200/student/_delete_by_query?pretty" -v -H "Content-Type: application/json" -d '
{
    "query": {
        "match_all": {}
    }
}'
#删除索引
curl -XDELETE "http://127.0.0.1:9200/student?pretty"
#查询id为10的文档
curl -XGET "http://127.0.0.1:9200/poetry/_doc/10"
.....剩余的操作请参考官网文档:
https://www.elastic.co/guide/en/elasticsearch/reference/6.0/indices.html
六.php使用
安装elasticsearch库, composer地址:  https://packagist.org/packages/elasticsearch/elasticsearch
composer require elasticsearch/elasticsearch
php使用官方文档: https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html
简单的封装
<?php
namespace app\util\elsearch;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\ClientBuilder;
/**
 * Class ELSearch
 * 封装ELSearch增删改查操作
 * @package app\util\elsearch
 */
class ELSearch
{
    //获取ELSearch客户端对象
    public static function client() {
        return $client = ClientBuilder::create()
            ->setHosts(config('util.elastic_search_host'))
            ->build();
    }
    //删除某一个文档
    public static function delDoc($index, $id) {
        $params = [
            'index' => $index,
            'type' => '_doc',
            'id' => $id
        ];
        $response = self::client()->delete($params);
        return $response;
    }
    //删除索引下的所有文档
    public static function delAllDoc($index) {
        $params = [
            'index' => $index,
            'type' => '_doc',
            'body' => [
                'query' => [
                    'bool' => [
                        'must_not' => ['term' => [ 'id' => -1000 ]],
                    ]
                ]
            ]
        ];
        $response = self::client()->deleteByQuery($params);
        return $response;
    }
    //获取文档
    public static function getDoc($index, $id) {
        $params = [
            'index' => $index,
            'type' => '_doc',
            'id' => $id
        ];
        try {
            $response = self::client()->get($params);
            return $response['_source'];
        } catch (Missing404Exception $e) {
            return null;
        }
    }
    //更新文档
    public static function updateDoc($index, $id, $data) {
        $params = [
            'index' => $index,
            'type' => '_doc',
            'id' => $id,
            'body' => [
                'doc' => $data
            ]
        ];
        return self::client()->update($params);
    }
    //添加文档
    public static function addDoc($index, $id, $data) {
        $params = [
            'index' => $index,
            'type' => '_doc',
            'id' => $id,
            'body' => $data
        ];
        $response = self::client()->index($params);
        return $response;
    }
    //删除索引
    public static function delIndex($index) {
        //先删除索引下所有的文档
        self::delAllDoc($index);
        //再删除索引
        $params = ['index' => $index];
        $response = self::client()->indices()->delete($params);
        return $response;
    }
    //创建索引
    public static function newIndex($index, $fields) {
        $params = [
            'index' => $index,
            'body' => [
                'settings' => [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => $fields
                ]
            ]
        ];
        $response = self::client()->indices()->create($params);
        return $response;
    }
}
Centos安装elasticsearch,php连接使用的更多相关文章
- CENTOS安装ElasticSearch(转)
		From: https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ CENTOS安装ElasticSearch ElasticSear ... 
- CENTOS安装ElasticSearch
		原文链接:https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ CENTOS安装ElasticSearch ElasticSearc ... 
- Centos安装elasticsearch教程
		elasticsearch安装是ytkah在做laravel电商站内搜索要实现的,通过自己的搜索和学习能力不算很费力解决了.下面就整理一下安装elasticsearch教程,服务器是Centos 7, ... 
- centos安装Elasticsearch步骤
		1.安装JDK:centos删除openJDK,安装JDK,vim /etc/profile配置JAVA_HOME 2.官网下载elasticsearch:https://www.elastic.co ... 
- centos安装及Xshell连接配置
		一.百度下载并安装VMware 二.下载centos 打开https://www.centos.org,点击“get centos now”,点击“DVD ISO”下载(也可以下滑点击“more do ... 
- CentOS 安装 Ansible 以及连接Windows server的办法
		1. CentOS机器上面按住那ansible yum install ansible 2. 安装 pywinrm 如果不安装 这个的话 ansible 会提示 没有 winrm 模块 注意需要先 ... 
- centos 安装xrdp远程连接桌面
		1. 安装epel库,否则无法安装xrdp yum install epel-release 2.安装 xrdp yum install xrdp 3. 安装tigervnc-server yum i ... 
- centos安装redis +RedisDesktopManager连接redis
		1.先到Redis官网(redis.io)下载redis安装包 wget http://download.redis.io/releases/redis-5.0.4.tar.gztar xzf red ... 
- CentOS 安装 elasticsearch 注意点
		注意点: 1. 从官网下载以 rpm 结尾的软件包 7.3.1版本 下载地址: https://artifacts.elastic.co/downloads/elasticsearch/elastic ... 
随机推荐
- 最简单的babel+webpack配置
			首先先介绍一下2个重要的库:core-js 和 regenerator core-js core-js 是用于 JavaScript 的组合式标准化库,它包含 es5 (e.g: object.fre ... 
- Beyond Compare4激活码(版本 4.2.8)
			w4G-in5u3SH75RoB3VZIX8htiZgw4ELilwvPcHAIQWfwfXv5n0IHDp5hv 1BM3+H1XygMtiE0-JBgacjE9tz33sIh542EmsGs1yg ... 
- JavaWeb_(SpringMVC框架)SpringMVC入门
			Spring MVC又叫SpringWebMVC是一个轻量级的基于请求响应的表现层框架.它是Spring框架的一部分.SpringMVC与Struts2都可以替代原始Servlet技术. Spring ... 
- ROS机器人开发实践学习笔记3
			摘要: 刚刚开始学习ROS,打算入机器人的坑了,参考教材是<ROS及其人开发实践>胡春旭编著 机械工业出版社 华章科技出品.本来以为可以按照书上的步骤一步步来,但是,too young t ... 
- redis数据结构有哪些
			1.String 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 2.list 一个链表, ... 
- mybatis+mysql批量插入和批量更新、存在及更新
			mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ... 
- $this->request->post 和input 区别
			$this->request->post 和input 区别 Request是对象,而input是内置函数 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论 
- Airbnb新用户的民宿预定结果预测
			1. 背景 关于这个数据集,在这个挑战中,您将获得一个用户列表以及他们的人口统计数据.web会话记录和一些汇总统计信息.您被要求预测新用户的第一个预订目的地将是哪个国家.这个数据集中的所有用户都来自美 ... 
- MySQL ALTER命令-修改数据表名或者修改数据表字段
			需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 删除,添加或修改表字段 如下命令使用了 ALTER 命令及 DROP 子句来删除表的 i 字段: ALTER TABLE ... 
- [dart学习]第七篇:类(构造函数)
			前言:楼主平时基本没有使用过异常处理,所以对异常的认知可能不够准确,这里就不翻译异常的相关内容了,大家可以去官网自行阅读介绍,地址 https://dart.dev/guides/language/l ... 
