thinkphp5 +elasticsearch

Posted 东歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp5 +elasticsearch相关的知识,希望对你有一定的参考价值。

php7使用elasticsearch

1、安装

官网下载地址:https://www.elastic.co/downloads/elasticsearch

# 解压到非root目录,运行时使用非root账号且必须安装java环境

yum install java

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz

tar zxvf elasticsearch-6.2.3.tar.gz

useradd elasticsearch

password elasticsearch

chown elasticsearch:elasticsearch elasticsearch-6.2.3

cd elasticsearch-6.2.3

nohup ./bin/elasticsearch  &   #设置成常驻进程

 

# php扩展库引入  composer.json

{  "require": {    // ...    "elasticsearch/elasticsearch": "~6.0"    // ...  } }

 

# 测试数据导入

create table articles(

  id int not null primary key auto_increment,

  title varchar(200) not null comment ‘标题‘,

  content text comment ‘内容

);

insert into articles(title, content) values (‘Laravel 测试1‘, ‘我的宝马多少马力‘),

(‘我的宝马发动机多少‘, ‘我的保时捷马力不错‘),

(‘Laravel 测试3‘, ‘我的宝马发动机多少‘);

2、api使用

 

<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <[email protected]>
// +----------------------------------------------------------------------
namespace appadmincontroller;

use cmfcontrollerAdminBaseController;
use thinkDb;
use ElasticsearchClientBuilder;

/* [email protected]
 * 功能:分词系统
 * return 
 */
class ElasticController extends AdminBaseController
{
    /* [email protected]
     * 功能:生成索引
     * return
     */
    public function index(){
        try {
            $db = new PDO(‘mysql:host=127.0.0.1;dbname=thinkcmf5‘, ‘root‘, ‘d1560a683a5e0d82‘);

            $sql = ‘select * from articles‘;
            $query = $db->prepare($sql);
            $query->execute();
            $lists = $query->fetchAll();
            print_r($lists);
        } catch (Exception $e) {
            echo $e->getMessage();
        }

        $client = ClientBuilder::create()->build();
        foreach ($lists as $row) {
            $params = [
                ‘body‘ => [
                    ‘id‘ => $row[‘id‘],
                    ‘title‘ => $row[‘title‘],
                    ‘content‘ => $row[‘content‘]
                ],
                ‘id‘ => ‘article_‘ . $row[‘id‘],
                ‘index‘ => ‘articles_index‘,
                ‘type‘ => ‘articles_type‘
            ];
            $client->index($params);
        }


    }

    /* [email protected]
     * 功能:获取索引
     * return
     */
    public function getIndex(){
        $client = ClientBuilder::create()->build();
        $params = [
            ‘index‘ => ‘articles_index‘,
            ‘type‘ => ‘articles_type‘,
            ‘id‘ => ‘article_1‘
        ];
        $res = $client->get($params);
        print_r($res);
    }
    
    
    /* [email protected]
     * 功能:从索引中删除文档
     * return 
     */
    public function delIndex(){
        $client = ClientBuilder::create()->build();
        $params = [
            ‘index‘ => ‘articles_index‘,
            ‘type‘ => ‘articles_type‘,
            ‘id‘ => ‘article_1‘
        ];
        $res = $client->delete($params);
        print_r($res);

    }


    /* [email protected]
      * 功能:设置索引
      * return
      */
    public function createIndex(){
        $client = ClientBuilder::create()->build();
        $params[‘index‘] = ‘articles_index‘;
        $params[‘body‘][‘settings‘][‘number_of_shards‘] = 2;
        $params[‘body‘][‘settings‘][‘number_of_replicas‘] = 0;
        $client->indices()->create($params);

    }


    /* [email protected]
     * 功能:查询条件
     * return 
     */
    public function search(){

        $client = ClientBuilder::create()->build();
        $params = [
            ‘index‘ => ‘articles_index‘,
            ‘type‘ => ‘articles_type‘,
        ];

        //多字段匹配
//        $params[‘body‘][‘query‘][‘multi_match‘][‘query‘] = ‘我的宝马发动机多少‘;
//        $params[‘body‘][‘query‘][‘multi_match‘][‘fields‘] = ["title","content"];
//        $params[‘body‘][‘query‘][‘multi_match‘][‘type‘] ="most_fields"; // most_fields 多字段匹配度更高   best_fields  完全匹配占比更高
//
//        //单个字段匹配
//        $params[‘body‘][‘query‘][‘match‘][‘content‘] =  ‘我的宝马多少马力‘;

        //完全匹配
//        $params[‘body‘][‘query‘][‘match_phrase‘][‘content‘] =  ‘我的宝马多少马力‘;


        //联合搜索  must,should,must_not
        $params[‘body‘][‘query‘]["bool"][‘must‘]["match"][‘content‘] = "宝马";
        $params[‘body‘][‘query‘]["bool"][‘must_not‘]["match"][‘title‘] = "宝马";

        $res = $client->search($params);
        print_r($res);

    }

}

 

以上是关于thinkphp5 +elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章

elaseticsearchelaseticsearch启动报错Caused by: org.elasticsearch.transport.BindTransportException: Fai(代

max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536](代

elasticsearch中文档操作,索引导入数据批量导入删除文档

elasticsearch中文档操作,索引导入数据批量导入删除文档

elasticsearch中文档操作,索引导入数据批量导入删除文档

ELK在广告系统监控中的应用 及 Elasticsearch简介