elasticsearch curl获取分词搜索结果

//es获取内容分词
function getEsAnalyze($text,$analyzer = 'ik_smart',$dbcfg = ''):array
{
    if(empty($dbcfg)){
        include DT_ROOT.'/file/config/es.inc.php';
    }else{
        include DT_ROOT.'/connection.inc.php';
    }
    if(empty($ElasticSearchConf)){
        return null;
    }
    $ElasticSearchConf = $ElasticSearchConf[0];
    $url = $ElasticSearchConf['scheme'].'://'.$ElasticSearchConf['host'].':'.$ElasticSearchConf['port'];
    $header = [];
    if(!empty($ElasticSearchConf['user'])){
        $header[] =  "Authorization: Basic ".base64_encode($ElasticSearchConf['user'].":".$ElasticSearchConf['pass']); //添加头,在name和pass处填写对应账号密码
    }
    $param = [
        "text"=>$text,
        "analyzer"=>$analyzer//ik_max_word
    ];
    try{
        $url.= '/_analyze';
        $header[] = 'Content-Type: application/json';
        $result = dcurl($url,json_encode($param),'',$header);
    }catch (\Exception $e){
        $this->err($e->getMessage());
    }
    if($result){
        $result = json_decode($result,1);
        $result = array_column($result['tokens'],'token');
        $result = array_filter($result,function ($v){
            return mb_strlen($v) > 1 && !is_numeric($v);
        });
    }else{
        $result = null;
    }
    return $result;
}

  

posted @ 2022-01-06 11:24  飞鹰之歌  阅读(172)  评论(0)    收藏  举报