elasticsearch-php的使用

需要安装elasticsearch请看另外一篇博客:https://blog.csdn.net/dabao87/article/details/106781861 

elasticsearch-php封装好的控制器

<?php

namespace app\api\controller;

require '../vendor/autoload.php';
use Elasticsearch\ClientBuilder;
	
class ElasticSearch
{
    private $client;
    // 构造函数
    public function __construct()
    {
        $params = array(
            '47.101.54.26:9200'
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }

    // 创建索引
    public function create_index($index_name = 'test_ik') { // 只能创建一次
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0
                ],
                'mappings' => [
		            'my_type' => [
		                '_source' => [
		                    'enabled' => true
		                ],
		                'properties' => [
		                    'first_name' => [
		                        'type' => 'string',
		                        'analyzer' => 'standard'
		                    ],
		                    'age' => [
		                        'type' => 'integer'
		                    ]
		                ]
		            ]
		        ]
            ]
        ];

        try {
            return $this->client->indices()->create($params);
        } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }

    // 删除索引
    public function delete_index($index_name = 'test_ik') {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }

    // 创建文档模板
    public function create_mappings($type_name = 'goods',$index_name = 'test_ik') {

        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => [
                $type_name => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'id' => [
                            'type' => 'integer', // 整型
                            'index' => 'not_analyzed',
                        ],
                        'title' => [
                            'type' => 'string', // 字符串型
                            'index' => 'analyzed', // 全文搜索
                            'analyzer' => 'ik_max_word'
                        ],
                        'content' => [
                            'type' => 'string',
                            'index' => 'analyzed',
                            'analyzer' => 'ik_max_word'
                        ],
                        'price' => [
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ];

        $response = $this->client->indices()->putMapping($params);
        return $response;
    }

    // 查看映射
    public function get_mapping($type_name = 'goods',$index_name = 'test_ik') {
        $params = [
            'index' => $index_name,
            'type' => $type_name
        ];
        $response = $this->client->indices()->getMapping($params);
        return $response;
    }

    // 添加文档
    public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $doc
        ];

        $response = $this->client->index($params);
        return $response;
    }

    // 判断文档存在
    public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->exists($params);
        return $response;
    }


    // 获取文档
    public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->get($params);
        return $response;
    }

    // 更新文档
    public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => [
                'doc' => [
                    'title' => '苹果手机iPhoneX'
                ]
            ]
        ];

        $response = $this->client->update($params);
        return $response;
    }

    // 删除文档
    public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->delete($params);
        return $response;
    }

    // 查询文档 (分页,排序,权重,过滤)
    public function search_doc($keywords = "电脑",$index_name = "test_ik",$type_name = "goods",$from = 0,$size = 2) {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => //match是精确查询,可以用match_phrase
                            	[ 'title' => [
	                                'query' => $keywords,
	                                'boost' => 3, 	// 权重大
	                                'slop'	=> 20,	// 20表示中文分词器将搜索内容分成了两个词或多个词,这个两个词之间可以相隔多少个字查询
                            ]]],
                            [ 'match' => 
                            	[ 'content' => [
	                                'query' => $keywords,
	                                'boost' => 2,
	                                'slop'	=> 100,	// 100表示中文分词器将搜索内容分成了两个词或多个词,这个两个词之间可以相隔多少个字查询
                            ]]],
                        ],
                    ],
                ],
                'sort' => ['price'=>['order'=>'desc']],
                'from' => $from,
                'size' => $size,
            ]
        ];

        $results = $this->client->search($params);
//        $maxScore  = $results['hits']['max_score'];
//        $score = $results['hits']['hits'][0]['_score'];
//        $doc   = $results['hits']['hits'][0]['_source'];
        return $results;
    }





	
}

使用方法:

<?php

namespace app\api\controller;

use think\Controller;

class Testelasticsearch extends Controller
{
	private $elasticController;
	
	public function __construct(){
		$this->elasticController = new Elasticsearch();
	}
	
	//创建索引
    public function create_index(){
    	$this->elasticController->create_index();
    }
    
    //添加文档
    public function add_doc(){
//  	$id,$doc,$index_name = 'test_ik',$type_name = 'goods'
    	$id = 2;
    	$doc = [
    		'title'=>'今天星期三1',
    		'content'=>'你可以在一个创建索引 API 中指定任何参数。所有的参数通常会注入请求体中的 body 参数下'
    	];
    	$this->elasticController->add_doc($id,$doc);
    }
    
    //获取文档
    public function get_doc(){
//  	$id = 1,$index_name = 'test_ik',$type_name = 'goods'
    	$all_doc = $this->elasticController->get_doc();
    	p($all_doc);
    }

    
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值