php 操作elasticsearch

 官方文档:https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html

php使用es 搜索关键词

public function es_search(Request $request){
		$keywords = $request->param('keywords','','trim');
		
		$page = $request->param('page');
		$size = 20;
		if($page){
			$from = ($page-1)*20;
			$pre = $page-1;
			$next = $page+1;
		
		}else{
			$pre = 1;
			$next = 2;
			$from = ($pre-1)*20;
		}	
		
		$search = [
			'index'=>'goods',
			'body'=>[
				'query'=>[
					'match'=>[
						'goods_title'=>$keywords,
					]
				],
				'from'=>$from,
				'size'=>$size,
				'highlight'=>[
					'fields'=>[
						'goods_title'=>new \stdClass() //new \stdClass()设置关键词高亮显示
					],
					'pre_tags'=>["<font style='color:red'>"],
					'post_tags'=>['</font>']
				]
			]
		];
		$res = $this->client->search($search);
		return view('list',['goods'=>$res['hits']['hits'],'keywords'=>$keywords,'pre'=>$pre,'next'=>$next]);
	}

php es 索引操作

<?php
declare (strict_types = 1);

namespace app\admin\controller;
use think\facade\Config;
use think\facade\Db;
use Elasticsearch\ClientBuilder;

class Index
{
    public $client;
    public function __construct()
    {
        $this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    }

    /**
     * 创建索引
     */
    public function index()
    {
        $index = [
            'index' => 'tp6_demo', //索引名称
            'body' => [
                 'mappings' => [
                     'properties'=>[
                         'id' => [
                             'type' => 'integer'
                         ],

                         'title' => [
                             'type' => 'text',
                             'analyzer' => 'ik_max_word'  //指定IK分词器
                         ]
                     ]
                 ]
            ]
        ];
        $res = $this->client->indices()->create($index);
        p($res);
    }

    /**
     * 判断索引是否存在
     */
    public function index_exists(){
        $res = $this->client->indices()->exists(['index' => 'index_tp6']);
        p($res, 1);
    }

    /**
     * 删除索引
     */
    public function del_index(){
        $res = $this->client->indices()->delete(['index' => 'index_test']);
        p($res, 1);
    }

    /**
     * 获取索引结构
     */
    public function getMapping(){
        $res = $this->client->indices()->getMapping(['index' => 'ik_index']);
        p($res);
    }

    /**
     * 往索引中添加数据
     */
    public function add_doc(){
        $doc = [
            'index' => 'index_tp6',
            'id' => 1,
            'body' => [
                'name' => '张学友' . mt_rand(0,1000),
                'age'  => mt_rand(15,96),
                'addr' => '湖北省武汉市洪山区'
            ]
        ];
        $res = $this->client->index($doc);
        p($res);
    }

    /**
     * 批量往索引中添加数据
     */
    public function all_doc(){
        $doc['body'] = [
            [
                'index' => [
                    '_index' => 'index_tp6',
                    '_id' => 2
                ]
            ],
            [
                'name' => '刘德华' . mt_rand(1,1000),
                'age'  => mt_rand(1,100),
                'addr' => '湖北省武汉市'
            ],
            [
                'index' => [
                    '_index' => 'index_tp6',
                    '_id' => 3
                ]
            ],
            [
                'name' => '刘德华' . mt_rand(1,1000),
                'age'  => mt_rand(1,100),
                'addr' => '湖北省武汉市'
            ],
        ];
        $res = $this->client->bulk($doc);
        p($res);

    }

    /**
     * 根据ID查询
     */
    public function selectById(){
        $id = [
            'index' => 'index_tp6',
            'id' => 3,
            '_source'  => ['name', 'age','addr'] // 只显示这两个字段
        ];
        $res = $this->client->get($id);
        p($res);
    }

    /**
     * 根据ID删除数据
     */
    public function deleteById(){
        $id = [
            'index' => 'index_tp6',
            'id'    => 1
        ];
        $response = $this->client->delete($id);
        p($response);
    }

    /**
     * 根据Id 修改数据
     */
    public function updateById(){
        $doc = [
            'index' => 'index_tp6',
            'id' => 3,
            'body'=>[
                'doc' => [
                    'age' => 100
                ]
            ]
        ];
        $response = $this->client->update($doc);
        p($response);
    }

    /**
     * 根据条件查询
     */
    public function selectWhere(){
        $param = [
            'index' => 'tp6_demo',
            'body' => [
                'query' => [
                    'match' => [

                    ]
                ]
            ]
        ];
        $res = $this->client->search($param);
        p($res);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值