php操作elasticsearch数据库的方法

 =====为本人原创,转载请注明出处! =====

 

一、命令

/**
 *      _search                     查询命令
 *      _update_by_query            更新命令
 *      _delete_by_query            删除命令
*/

 

 

二、指定字段查询

$url = 'http://192.168.10.2:9200/article/_search';
$data = [
    'query'=>['bool'=>['must'=>[['term'=>['article_id'=>'3842959']]]]]
];
$post_data = json_encode($data);
$relust = curl_json($url,$post_data);
print_r($relust);

 

 

三、指定类别的关键词查询

        $keywords = '韩晓玲';
        $cateid = 128;

        $a = json_decode('{}');

        $url = 'http://192.168.0.1/article/_search';

        if($ent_id){
            $data['query']['bool']['must'][0]['term']['cateid'] = $cateid;
        }

        $data['query']['bool']['should'][0]['match_phrase']['title'] = $keywords;
        $data['query']['bool']['should'][1]['match_phrase']['content'] = $keywords;

        $data['from'] = 0;//从第几页开始获取
        $data['size'] = 10;//每页显示多少条
        $data['sort'] = [];//排序

        //下面是搜索关键词高亮设置
        $a = json_decode('{}');
        $data['highlight']['fields'][0]['title'] = $a;
        $data['highlight']['fields'][1]['content'] = $a;
        $data['highlight']['pre_tags'] = '<font color="red">';
        $data['highlight']['post_tags'] = '</font>';
        $data['highlight']['fragment_size'] = 20;

        $post_data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $relust = curl_post($url,$post_data);
        print_r($relust);

 

 

四、批量展示

批量查询20条数据,并以article_id字段进行倒序方式来排序展示。

        $url = 'http://192.168.10.2:9200/article/_search';
        $data = [
            'query'=>['bool'=>['must'=>[]]],'size'=>20,'sort'=>['article_id'=>'desc']
        ];
        $post_data = json_encode($data);
        $relust = curl_json($url,$post_data);
        print_r($relust);

 

 

 

五、更新

$url = 'http://192.168.10.2:9200/article/_update_by_query';
$data = [
    'query'=>['bool'=>['must'=>[['term'=>['article_id'=>'3842959']]]]],
    'script'=>['inline'=>"ctx._source['article_is_show'] = 0"]
];
$post_data = json_encode($data);
$relust = curl_json($url,$post_data);
print_r($relust);
exit;

 

 如果需要同时更新多个字段,那就加分号隔开ctx._source['article_title'],记住字符类型的要添加上引号,数字类型的不要添加引号,如下:

$url = 'http://192.168.10.2:9200/article/_update_by_query';
$data = [
    'query'=>['bool'=>['must'=>[['term'=>['article_id'=>'3842959']]]]],
    'script'=>['inline'=>"ctx._source['article_title'] = '".$title."';ctx._source['article_is_show'] = 0;"]
];
$post_data = json_encode($data);
$relust = curl_json($url,$post_data);
print_r($relust);
exit;

 

 

六、删除

$url = 'http://192.168.10.2:9200/article/_delete_by_query';
$data = [
    'query'=>['bool'=>['must'=>[['term'=>['article_id'=>'3842959']]]]]
];
$post_data = json_encode($data);
$relust = curl_json($url,$post_data);
print_r($relust);
exit;

 

 

具体$data中的命令,请参阅elasticsearch文档

 

七、curl

function curl_post($url,$data)
{
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl, CURLOPT_HTTPHEADER,array(
        'Content-Type: application/json; charset=utf-8',
        'Cache-Control: no-cache',
        'Pragma: no-cache'
    ));
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    $result = curl_exec($curl);
    curl_close($curl);
    $datalist = json_decode($result,true);
    return $datalist;
}

 

posted @ 2022-09-22 10:40  赵瑛  阅读(245)  评论(0编辑  收藏  举报

版权所有 © 2022 沅来是澧

如果有程序开发、网站建设等需求的请联系我,QQ:47419233