1. 程式人生 > >ElasticSearch測試、IK分詞簡單測試(PHP)

ElasticSearch測試、IK分詞簡單測試(PHP)

以下全是測試程式碼:如有需要了解安裝 ElasticSearch和IK分詞的可參考:https://blog.csdn.net/weixin_42579642/article/details/84317099

use Elasticsearch\ClientBuilder;
class ElasticController extends Controller
{
    public function creat(){
        $esclient = ClientBuilder::create()
            ->setHosts(["172.20.10.14:9200"])
            ->build();
        $data = DB::table('user')->get();

        // 單次插入

/*        foreach($data as $k=>$v) {
            $params = [
                'index' => 'shop',
                'type' => 'user',
                'id' => $v->id,
                ];
                unset($v->id);
                foreach($v as $kk => $vv){
                    $params['body'][$kk] = $vv;
                }
            $res = $esclient->index($params);
        }*/

        // 批量分次插入

/*        $arr = [];
        foreach($data as $k=>$v){
            $arr[]['index'] = ['_id' => $v->id];
            $arr2 = [];
            foreach($v as $kk=>$vv){
                $arr2[$kk] = $vv;
            }
            $arr[] = $arr2;
        }
        //print_r(count($arr)/2/6);exit;
        $num = 6;  //設定多少條插入一次
        $count = count($arr)/2/$num;
        $res = [];
        for($i=0;$i<$count;$i++){
            $new_arr = [];
            $j = $i*2*$num;
            $jj = ($i+1)*2*$num;
            for($j;$j<$jj;$j++){
                $new_arr[] = $arr[$j];
            }
            $params = [
                'index' => 'my_indexa',
                'type' => 'my_typea',
                'body' => $new_arr
            ];
            $res[] = $esclient->bulk($params);
        }
        print_r($res);exit;*/

        //根據id獲取

/*        $params = [
            'index' => 'shop',
            'type' => 'user',
            'id' => '14',
        ];
        $res = $esclient->get($params);
        print_r($res);exit;*/

        //搜尋

/*        $search_params = [
            'index' => 'shop',
            'type' => 'user',
            'body'=> [
                'query'	=> [
                    'match'	=> [
                        'name'=>'羅'
                    ]
                ]
            ]
        ];
        $res = $esclient->search($search_params);
        print_r($res);*/

        //測試分詞效果(如果有效果證明安裝IK成功)

/*        $params = [
            'body' => [
                'text' => '天天敲程式碼非常高興',
                'analyzer'=>'ik_max_word' //ik_max_word 精細  ik_smart 粗略
            ]
        ];
        $res =  $esclient->indices()->analyze($params);
        print_r($res);*/

        // 建立空索引(必須是新索引名稱)
/*        $params = [
            'index' => 'my_index_user4',
        ];
        $res =  $esclient->indices()->create($params);*/

        // 建立對映(索引名必須為 my_index_user4)

/*        $params = [
            'index' => 'my_index_user4',
            'type' => 'my_type_user4',
            'body' => [
                'my_type_user4' => [
                    '_source' => [
                        'enabled' => true          //可選引數(可以減輕伺服器壓力)
                    ],
                    'properties' => [

                        'contents' => [
                            'type' => 'text', // 字串型
                            'analyzer'=>'ik_max_word', //ik_max_word 最細粒度拆分 ik_smart最粗粒度拆分
                            'search_analyzer'=> 'ik_max_word'
                        ]

                    ]
                ]
            ]
        ];

        $res = $esclient->indices()->putMapping($params);*/

        // 批量分次插入(索引名必須為 my_index_user4)

      /*  $data1 = DB::table('elastic_test')->get();
        $arr = [];
        foreach($data1 as $k=>$v){
            $arr[]['index'] = ['_id' => $v->id];
            $arr2 = [];
            foreach($v as $kk=>$vv){
                $arr2[$kk] = strval($vv);    //因為搜尋對整形無效所以把數值都改成字串(根據需要新增即可)
            }
            $arr[] = $arr2;
        }
        $num = 6;  //設定多少條插入一次
        $count = count($arr)/2/$num;
        $res = [];
        for($i=0;$i<$count;$i++){
            $new_arr = [];
            $j = $i*2*$num;
            $jj = ($i+1)*2*$num;
            for($j;$j<$jj;$j++){
                $new_arr[] = $arr[$j];
            }
            $params = [
                'index' => 'my_index_user4',
                'type' => 'my_type_user4',
                'body' => $new_arr
            ];
            $res[] = $esclient->bulk($params);
        }
        print_r($res);exit;*/

        //搜尋(索引名必須為 my_index_user4)

        $search_params = [
            'index' => 'my_index_user4',
            'type' => 'my_type_user4',
            'body' => [
                'from' => 4,
                'size' => 3,          //分頁展示,from相當於分頁中的偏移量,size相當於每頁展示的條數
                'query' => [
                    'match' => [
                        'contents' =>['query'=>'今天好好','operator' => 'AND'] //所有關鍵詞都必須出現 可以不連續(預設是有一個詞就匹配到)
                    ]
                ]
//                 'query' => [
//                    'match_phrase' => [                //必須完全匹配
//                        'contents' =>  '今天天氣'
//                    ]]

//                    'query' => [                      //萬用字元查詢  
//                      'wildcard' => [
//                        'name' =>  '*user*'           //?代表站一個字元如'???2345*'這是匹配第四位跟第7位是2345的,**類似於sql裡面like中的%%的用法
//                    ]]
//                    'query' => [                      //正則匹配  
//                      'regexp' => [
//                        'phone' =>  '135.*'           //匹配以135開頭的
//                    ]]
            ,
                //設定高亮
                "highlight" => [
                    "pre_tags" => [
                        '<span style="color:red">'
                    ],
                    "post_tags" => [
                        '</span>'

                    ],
                    'fields'=> [
                        'contents' => new \stdClass()
                    ]
                ]
            ]
        ];
        $res = $esclient->search($search_params);
        print_r($res);
    }


}