elasticsearch学习记录与实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wt1286331074/article/details/88312770

参考:https://ke.qq.com/course/331194

前提条件安装jdk:yum install -y java (检查java -version)

// 安装elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.rpm
rpm -ivh elasticsearch-6.6.1.rpm
systemctl start elasticsearch.service(复制上面的)
service elasticsearch status(报错,内存不够用)
// 解决办法
vim /etc/elasticsearch/jvm.options (修改:-Xms256m -Xmx256m,添加:最底下 -XX:-AssumeMP)

service elasticsearch restart   --OK  // 启动
netstat -apn|grep 9200   --有的
curl localhost:9200或者127.0.0.1:9200
// 安装可视化管理kibana
还是那个官网,点Product,选择rpm
wget https://artifacts.elastic.co/downloads/	/kibana-6.6.1-x86_64.rpm
rpm -ivh kibana-6.6.1-x86_64.rpm

vim /etc/kibana/kibana.yml // 修改相关配置
去掉注释:server.host再改成:"0.0.0.0";	// 允许外部访问
去掉注释:elasticsearch.hosts,再回头还是那localhost改为127.0.0.1
/usr/share/kibana/bin/kibana  // 启动

然后去阿里云的安装-安全规则-自定义:tcp:5601,然后访问http://39.108.172.231:5601
这样就进入kibana管理界面了

其中我用配置低点的服务器开启kibana时候elasticsearch会自动关掉,也不知道啥原因,换了阿里好点的服务器就没问题了,森气。

概念:

每台服务器可以运行多个Elasticsearch实例,一个实例称之为一个节点,一组节点构成一个集群
方便理解:索引index=数据库database;里面单条数据称之为document文档,由n个文档组成,这个存的就是一个人的基本信息这种样子,是json

然后就是kibana的各种语法学习,特地写个文章记录下

安装中文分词ik==》http://github.com/medcl/elasticsearch-analysis-ik
find / -name elasticsearch-plugin
// 复制github上命令修改下路径和版本号,版本号curl 。。。就得到了
/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.1/elasticsearch-analysis-ik-6.6.1.zip
service elasticsearch restart   重启 
curl 127.0.0.1:9200看下

学会了在kibana里面操作和查询数据,然后就去php搞吧
官方api文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/index.html

composer require elasticsearch/elasticsearch:~6.0	// 执行composer引入语法和项目
vim /etc/elasticsearch/elasticsearch.yml搜索network.hosts后面的地址改成0.0.0.0,这样是为了让外部浏览器能访问和操作
重启elasticsearch:service elasticsearch restart
然后访问:http://39.108.172.231:9200/  显示相关参数

然后就去看官方文档
最后展示下代码

// 公用部分
        $hosts = [
            'http://39.108.172.231:9200',         // IP + Port
        ];
        $client = \Elasticsearch\ClientBuilder::create()           // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
        ->build();              // Build the client object

        // 查询部分
        $params = [
            'index' => 'product',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'match_phrase' => [
                        'title' => [
                            'query' => '索尼',
                            'slop' => 20	// 中间能隔多少个字
                        ]
                    ]
                ]
            ]
        ];
        $results = $client->search($params);
        // 打印出来的$result取里面的$results['hits']['hits'][0]['_id']这个就是真正的id,可以循环取出来
        // _source这个里面是它所有的结果
        dd($results);


        // 这边添加即修改
        $params = [
            'index' => 'product',
            'type' => '_doc',
            'id' => 5,// 刚刚添加的id
            'body' => [
                'title' => '索尼智能相机',
                'description' => '超光学防抖'
            ]
        ];
        // Document will be indexed to my_index/my_type/my_id
        $response = $client->index($params);

在这里插入图片描述
然后访问我这个方法,就会添加或者查询了,如果不行可以去kibana里面看

猜你喜欢

转载自blog.csdn.net/wt1286331074/article/details/88312770