查询并删除Elasticsearch库中指定字段重复的数据

比如我要查询Elasticsearch库中字段为:id的有没有重复数据,可以用下面的方法,先查询出来,再执行删除。

$from = 0;
$size = 0;
$url = 'http://127.0.0.1/aaaaaa/_search';
$data['from'] = $from;
$data['size'] = $size;
$data['aggs']['my']['terms'] = ['field'=>'id'];

$post_data = json_encode($data,JSON_UNESCAPED_UNICODE);
$relust = curl_post($url,$post_data);
$relust = json_decode($relust,true);
if(!empty($relust['aggregations']['my']['buckets'])){
    foreach ($relust['aggregations']['my']['buckets'] as $k=>$v)
    {
        $doc_id = $v['key'];
        $url = 'http://127.0.0.1/aaaaaa/_delete_by_query';
        $data = [
            'query'=>['bool'=>['must'=>[['term'=>['id'=>$id]]]]]
        ];
        $data['query']['bool']['filter'][0]['bool']['must']['match_phrase']['id'] = $id;
        $post_data = json_encode($data);
        $relust = curl_post($url,$post_data);
    }
}
return json(['code'=>10000,'msg'=>'删除成功']);

 

 

 
posted @ 2023-02-23 11:36  赵瑛  阅读(456)  评论(0编辑  收藏  举报

版权所有 © 2022 沅来是澧

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