<html>
<head><title>SOLR测试</title></head>
<body>
<form action='' method='post' name='solotestform'>
<p>城市：<input type='text' name='city' value='<?=$city?>' />nj 类型：<input type='text' name='tbl' value='<?=$tbl?>' />sell/rent</p>
<p>板块1：<input type='text' name='streetid1' value='<?=$streetid1?>' />105建康路 板块2：<input type='text' name='streetid2' value='<?=$streetid2?>' />106莫愁路 板块3：<input type='text' name='streetid3' value='<?=$streetid3?>' />169中山门</p>
<p>物业类型：<input type='text' name='infotype' value='<?=$infotype?>' />1住宅2别墅3写字楼4商铺5厂房6车库</p>
<p>小区名：<input type='text' name='blockname' value='<?=$blockname?>' />时代雅居</p>
<p>面积：<input type='text' name='buildarea1' value='<?=$buildarea1?>' /> -- <input type='text' name='buildarea2' value='<?=$buildarea2?>' />非区间两值相等</p>
<p>建筑年代：<input type='text' name='buildyear1' value='<?=$buildyear1?>' /> -- <input type='text' name='buildyear2' value='<?=$buildyear2?>' />非区间两值相等</p>
<p>价格：<input type='text' name='price1' value='<?=$price1?>' /> -- <input type='text' name='price2' value='<?=$price2?>' />非区间两值相等</p>
<p>租凭方式：<input type='text' name='renttype' value='<?=$renttype?>' />整租/合租/日租</p>
<p>操作：<input type='hidden' name='test' value='1' /><input type='submit' name='formsubmit' value='提交' /></p>
</form>
</body>
</html>
<?php

if($test == 1)
{
    $solr_conf_arr = array(
        "nj" => array("sell"=>"http://172.17.1.171:8081/nj_search/count?app=sell",
                      "rent"=>"http://172.17.1.171:8081/nj_search/count?app=rent"),
        "hf" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=hf&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=hf&tbl=rent"),
        "wx" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=wx&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=wx&tbl=rent"),
        "sz" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=sz&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=sz&tbl=rent"),
        "hz" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=hz&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=hz&tbl=rent"),
        "cz" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=cz&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=cz&tbl=rent"),
        "xa" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=xa&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=xa&tbl=rent"),
        "ks" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=ks&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=ks&tbl=rent"),
        "cq" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=cq&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=cq&tbl=rent"),
        "wh" => array("sell"=>"http://172.17.1.37:8080/sell_rent_search/count?city=wh&tbl=sell",
                      "rent"=>"http://172.17.1.37:8080/sell_rent_search/count?city=wh&tbl=rent")
    );

    $city = count($solr_conf_arr[$city]) == 2 ? $city : 'nj';
    $tbl = 'sell' == $tbl ? 'sell' : 'rent';

    $solr = new Solr($solr_conf_arr[$city][$tbl]);
    
    if($blockname != '')
    {
        $solr->setQueryField(array("blockshowname"=>$blockname));//array("blockshowname"=>"天润城", "address"=>"天润城")
    }
    else
    {
        $solr->setQueryField("");
    }
    
    $streetarr = $ffarr = array();
    $ffarr[] = array("oper"=>"NOT", "field"=>"uid", "inneroper"=>":", "value"=>0);
    $ffarr[] = array("oper"=>"AND", "field"=>"esta", "inneroper"=>":", "value"=>1);
    
    if($streetid1 > 0)
    {
        $streetarr[] = $streetid1;
    }
    if($streetid2 > 0)
    {
        $streetarr[] = $streetid2;
    }
    if($streetid3 > 0)
    {
        $streetarr[] = $streetid3;
    }
    if($streetarr)
    {
        $ffarr[] = array("oper"=>"AND", "field"=>"streetid", "inneroper"=>"OR", "value"=>$streetarr);
    }
    if($infotype > 0)
    {
        $ffarr[] = array("oper"=>"AND", "field"=>"infotype", "inneroper"=>":", "value"=>$infotype);
    }
    if($buildarea1 > 0 && $buildarea2 > 0)
    {
        if($buildarea1 < $buildarea2)
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"buildarea", "inneroper"=>"TO", "value"=>array($buildarea1,$buildarea2));
        }
        else
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"buildarea", "inneroper"=>":", "value"=>$buildarea1);
        }
    }
    if($buildyear1 > 0 && $buildyear2 > 0)
    {
        if($buildyear1 < $buildyear2)
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"buildyear", "inneroper"=>"TO", "value"=>array($buildyear1,$buildyear2));
        }
        else
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"buildyear", "inneroper"=>":", "value"=>$buildyear1);
        }
    }
    if($price1 > 0 && $price2 > 0)
    {
        if($price1 < $price2)
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"price", "inneroper"=>"TO", "value"=>array($price1,$price2));
        }
        else
        {
            $ffarr[] = array("oper"=>"AND", "field"=>"price", "inneroper"=>":", "value"=>$price1);
        }
    }
    if($renttype != '' && $tbl == 'rent')
    {
        $ffarr[] = array("oper"=>"AND", "field"=>"renttype", "inneroper"=>":", "value"=>$renttype);
    }
    

    /*$ffarr = array(
        array("oper"=>"NOT",  "field"=>"uid",       "inneroper"=>":",   "value"=>0),
        array("oper"=>"AND",  "field"=>"esta",      "inneroper"=>":",   "value"=>1),
        array("oper"=>"AND",  "field"=>"district",  "inneroper"=>":",   "value"=>"浦口区"),
        array("oper"=>"AND",  "field"=>"buildarea",     "inneroper"=>"TO",  "value"=>array(60,100)),
        array("oper"=>"AND",  "field"=>"price",     "inneroper"=>"TO",  "value"=>array(60,100)),
        array("oper"=>"AND",  "field"=>"room",      "inneroper"=>":",   "value"=>2),
        array("oper"=>"AND",  "field"=>"hall",      "inneroper"=>":",   "value"=>1),
        array("oper"=>"AND",  "field"=>"blockid",   "inneroper"=>"OR",  "value"=>array(1538,7140))
    );
    
    $solr->setShowField(array("id"));
    $solr->setOrderField(array("price"=>"desc"));
    $solr->setStartNum(0);
    $solr->setRowsNum(10);
    */
    
    if($ffarr)
    {
        $solr->setFilterField($ffarr);
    }
    $solr->setCountNum(1000);
    $solr->setCountField(array("uid"));
    $arr = $solr->query('countgroup');
    
    echo '本次查询符合条件经纪人：';
    echo count($arr['uid']);
    echo '人<br />';
    
    if(count($arr['uid']) > 1)
    {
        foreach($arr['uid'] as $key=>$value)
        {
            $uidstr .= $uidstr == '' ? $key : ','.$key;
        }
        
        include_once('../core/conf/conf_'.$city.'.php');
        include_once('../core/lib/class_adodb.php'); //包含配置变量文件
        
        //不确定是否配置了从库
        $dbback = new TINY_ADODB($conf_dbuser_type,$conf_dbback_host,$conf_db_user,$conf_db_password,$conf_db_name,$conf_db_charset,$conf_db_debug,$conf_db_connect);

        $uarr = $dbback->GetAll("select b.truename,a.agentshortname from broker b left join agency a on b.agentcode = a.agentcode where b.uid in ($uidstr) order by b.agentcode asc");
        
        if($uarr)
        {
            foreach($uarr as $val)
            {
                echo "<div style='clear:both;'><div style='float:left;width:200px;'>经纪人姓名：".$val['truename']."</div><div style='float:left;width:400px;'>所属公司：".$val['agentshortname'].'</div></div>';
            }
        }
    }
}
/*
 * 中间件solr
 * Fisher 2014-5-5
*/
class Solr
{
    private $api_url = "";  //应用接口地址

    private $show_field_str   = "*"; //显示字段
    private $query_field_str  = "&q=";  //检索字段
    private $filter_field_str = "";  //过滤字段
    private $order_field_str  = "";  //排序字段
    private $start_num        = "&s=0";  //位移量
    private $rows_num         = "&r=1";  //查询条数
    private $count_num        = "&fn=1000"; //统计分组获取条数
    private $count_field_str  = "";  //统计分组

    private $mc = "nomc";//MC对象    
    
    //构造方法
    public function __construct($api_url)
    {
        $this->api_url = $api_url;
    }
    
    //设置MC对象
    public function setMc($mc)
    {
        $this->mc = is_object($mc) ? $mc : "nomc";
    }
    
    //查询数据
    public function query($query_type = 'normal')
    {
        //normal：普通模式  countgroup：统计分组模式
        $url = $query_type == 'normal' ? $this->api_url.$this->show_field_str.$this->query_field_str.$this->filter_field_str.$this->order_field_str.$this->start_num.$this->rows_num : $this->api_url.$this->count_field_str.$this->query_field_str.$this->filter_field_str.$this->count_num;
        //echo $url."<br /><br />";
        //是否启用MC
        if("nomc" == $this->mc)
        {
            $str = $this->curl_get_contents($url);
        }
        else
        {
            $memkey = md5($url);
            $cache = $this->mc->get($memkey);
            if($cache['isok'] == 1)
            {
                $str = $cache['data'];
            }
            else
            {
                $str = $this->curl_get_contents($url);
                $this->mc->add($memkey, array('isok'=>1, 'data'=>$str), false, 5);
            }
        }
        
        $sss = json_decode($str);
        $arr = $this->obj2arr(json_decode($str));

		return $arr;
    }
    
    //CURL
    public function curl_get_contents($url,$t_url="")
	{
		$ch = curl_init();
        $t_url = $t_url ? $t_url : "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $t_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $str = curl_exec($ch);
        curl_close($ch);

        return $str;
	}

    //设置显示字段
    public function setShowField($show_field_arr = array())
    {
        $show_field_str = "";
        if(is_array($show_field_arr) && !empty($show_field_arr))
        {
            foreach($show_field_arr as $value)
            {
                $show_field_str .= "&sf1=$value";
            }
        }

        $this->show_field_str = $show_field_str;
    }

    //设置检索字段
    public function setQueryField($query_field_arr = array())
    {
        $query_field_str = "";
        if(is_array($query_field_arr) && !empty($query_field_arr))
        {
            $field_str = "";
            foreach($query_field_arr as $key=>$value)
            {
                $field_str .= $field_str == '' ? "$key%3A".urlencode($this->gbk2utf($value)) : "+OR+$key%3A".urlencode($this->gbk2utf($value));
            }
            
            $query_field_str = "&q=".$field_str;
        }
        else
        {
            $query_field_str = "&q=";
        }
        
        $this->query_field_str = $query_field_str;
    }

    //设置过滤字段
    public function setFilterField($filter_field_arr = array())
    {
        $filter_field_str = "";
        
        if(is_array($filter_field_arr) && !empty($filter_field_arr))
        {
            $field_str = "";

            foreach($filter_field_arr as $key=>$value)
            {
                
                $f_str = "";

                switch($value["inneroper"])
                { 
                    case "TO":  
                                $f_str = $value["oper"]."+".$value["field"];
                                $f_str .= "%3A[".intval($value["value"][0])."+TO+".intval($value["value"][1])."]";
                                break;
                    case "OR":  
                    case "AND": 
                                $f_str = $value["oper"]."+(";
                                $fs = "";
                                foreach((array)$value["value"] as $val)
                                {
                                    $fs .= $fs == '' ? $value["field"]."%3A".$val : "+".$value["inneroper"]."+".$value["field"]."%3A".$val;
                                }
                                $f_str .= $fs.")";
                                break;
                    default:    
                                $f_str = $value["oper"]."+".$value["field"];
                                $f_str .= "%3A".urlencode($this->gbk2utf($value["value"]));
                                break;
                }

                $field_str .= $field_str == '' ? $f_str : "+".$f_str;
            }

            $filter_field_str = "&fq=".$field_str;
        }

        $this->filter_field_str = $filter_field_str;
    }

    //设置排序字段
    public function setOrderField($order_field_arr = array())
    {
        $order_field_str = "";
        if(is_array($order_field_arr) && !empty($order_field_arr))
        {   
            $order_field_str .= "&of=";
            $order_str = "";
            foreach($order_field_arr as $key=>$value)
            {
                $order_str .= $order_str == "" ? $key."+".$value : "%2C".$key."+".$value;
            }
            $order_field_str .= $order_str;
        }
        $this->order_field_str = $order_field_str;
    }

    //设置统计分组
    public function setCountField($count_field_arr = array())
    {

        $count_field_str = "";
        if(is_array($count_field_arr) && !empty($count_field_arr))
        {
            $count_field_str .= "&ft=";
            $count_str = "";
            foreach($count_field_arr as $key=>$value)
            {
                $count_str .= $count_str == "" ? $value : "%2C".$value;
            }
            $count_field_str .= $count_str;
        }
        $this->count_field_str = $count_field_str;
    }
    
    //设置位移量
    public function setStartNum($start_num)
    {
        $start_num = intval($start_num);
        $this->start_num = "&s=".$start_num;
    }
        
    //设置查询条数
    public function setRowsNum($rows_num)
    {
        $rows_num = intval($rows_num);
        $this->rows_num = "&r=".$rows_num;
    }

    //设置查询条数
    public function setCountNum($count_num)
    {
        $count_num = intval($count_num);
        $this->count_num = "&fn=".$count_num;
    }

    //GBK转UTF8编码
    public function gbk2utf($strInput)
    {
        return $this->is_gbk($strInput) ? $this->strIconv($strInput, "gbk", "utf-8") : $strInput;
    }

    //UTF8转GBK编码
    public function utf2gbk($strInput)
    {
        return $this->is_utf8($strInput) ? $this->strIconv($strInput, "utf-8", "gbk") : $strInput;
    }

    //是否UTF8格式编码
    public function is_utf8($strInput)
    {
        return $strInput === iconv("gbk", "utf-8", iconv("utf-8", "gbk", $strInput));
    }

    //是否gbk格式编码
    public function is_gbk($strInput)
    {
        return $strInput === iconv("utf-8", "gbk", iconv("gbk", "utf-8", $strInput));
    }

    //替换数组编码
    public function strIconv($strInput,$inChar="",$outChar="")
    {
        $strOutput = iconv($inChar, $outChar, $strInput);
        return $strOutput !== false ? $strOutput : $strInput;
    }

    //JSON格式数据递归转化为ARRAY
    public function obj2arr($obj){
        $ret = array();
        if(gettype($obj) == 'array' || gettype($obj) == 'object')
        {
            foreach($obj as $key =>$value){
                if(gettype($value) == 'array' || gettype($value) == 'object'){
                    $ret[$key] = $this->obj2arr($value);
                }
                else{
                    $ret[$key] = $value;
                }
            }    
        }
        
        return $ret;
    }
}
?>