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);
    }

    
    
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章