魏长东

weichangdong

elastic search扫描模式

function makeLangAction()
    {
        $es = self::connectEs();
        $params = array(  
            "search_type" => "scan",
            "scroll" => "30s",
            "size" => 50,
            "index" => "360weather",  
            "body" => array(  
                "query" => array(  
                    "match_all" => array()  
                )  
            )  
        );  
          
        $docs = $es->search($params);
        $scroll_id = $docs['_scroll_id'];
        $redis = new RedisModel('write');
        echo "all num ".$docs['hits']['total']."\n";
        $num = 0;
        $all_tz = [];
        while (true) {   
            $response = $es->scroll(  
                array(  
                    "scroll_id" => $scroll_id,
                    "scroll" => "30s",
                )  
            );  
            if (count($response['hits']['hits']) > 0) {  
                //loop $response['hits']['hits'] data
                $scroll_id = $response['_scroll_id'];
                //$num += count($response['hits']['hits']); 
                foreach ($response['hits']['hits'] as $tmp) {
                    $tt = explode('_', $tmp['_id']);
                    $loc_id = $tt[0];
                    $data = $tmp['_source'];
                    $lang = $data['lang'];
                    $lang_info = json_encode([
                        'area' => $data['country_code'],
                        'province' => $data['province'],
                        'name' => $data['city'],
                        'tz' => $data['tz'],
                    ]);
                    $tz_num = Functions::computerTz($data['tz']);
                    /*
                    if (isset($all_tz[$data['tz']])) {
                        
                    } else {
                        $all_tz[$data['tz']] = $tz_num;
                    }
                    */
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,self::REDIS_CITY_LANG.$lang,$lang_info);
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,'tz',$tz_num);
                    $num++;
                } 
            } else {   
                break;  
            }  
        }
        
        echo 'tz num '.count($all_tz)."\n";
        print_R($all_tz);
        
        echo "set num ". $num ."\n";  
    }