首页 > 其他分享 >查询并删除Elasticsearch库中指定字段重复的数据

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

时间:2023-02-23 11:46:45浏览次数:91  
标签:url 查询 json Elasticsearch relust 库中 post data id

比如我要查询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'=>'删除成功']);

 

 

 

标签:url,查询,json,Elasticsearch,relust,库中,post,data,id
From: https://www.cnblogs.com/zhaoying/p/17147373.html

相关文章