2016-08-03 64 views
0

我的映射:搜索使用PHP捲曲沒有返回彙集結果

"properties": { 
     "userid": { 
      "type": "integer" 
     }, 
     "engid": { 
      "type": "short" 
     }, 
     "score": { 
      "type": "short", 
     }, 
     "name": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "submitTime": { 
      "type": "date", 
      "format": "yyyy-MM-dd HH:mm:ss" 
     } 
    } 

我的搜索查詢JSON:

{ 
    "size": 10, 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "range": { 
       "submitTime": { 
        "gt": "now-18d" 
       } 
       } 
      } 
     , 
           { 
            "terms": { 
             "userid": [1,2,3,4,5,6,7] 
            } 
           } 

      ], 
      "must_not": [ 
      { 
       "term": { 
       "name": "---" 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "engine": { 
     "terms": { 
     "field": "name", 
     "order": { 
      "_term": "asc" 
     } 
     }, 
     "aggs": { 
     "score": { 
      "terms": { 
      "field": "score" 
      } 
     } 
     } 
    } 
    } 
} 

當我在命令行中使用curl執行,我得到正確的輸出,這包括返回的JSON聚合字段:

curl -XGET http://localhost:9200/amas/engresults/_search?search_type=count -d "@t.json" 

輸出:

{"took":417,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":122049,"max_score":0.0,"hits":[]},"aggregations":{......}} 

但是我使用PHP捲曲,我收到了同樣的反應,但反應不包括從聚合實際結果:

function doHttpElastic($uri, $method, $params, &$retdata) { 
    //echo "$cmd URL: $uri \n"; 
    $url="http://".ELASTIC_HOST.':'.ELASTIC_PORT; 
    $uri=$url.$uri; 
    echo $uri; 
    $httpcode = -1; 
    $ch = curl_init($uri); 
    curl_setopt_array($ch, array(
     CURLOPT_RETURNTRANSFER => false, 
     CURLOPT_CUSTOMREQUEST => strtoupper($method)/*, 
     CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/ 
    )); 
    $retdata = curl_exec($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    return $httpcode; 
} 

輸出我從PHP捲曲獲得:

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 324119, 
     "max_score": 0.0, 
     "hits": [] 
    } 
} 

我傳遞'get'作爲方法,並在$ params中查詢json。我在這裏做錯了什麼?

回答

0

得到了問題,我沒有發送$ params我的curl請求在PHP中。

修改的功能爲:

function doHttpElastic($uri, $method, $params, &$retdata) { 
    //echo "$cmd URL: $uri \n"; 
    $url="http://".ELASTIC_HOST.':'.ELASTIC_PORT; 
    $uri=$url.$uri; 
    echo $uri; 
    $httpcode = -1; 
    $ch = curl_init($uri); 
    curl_setopt_array($ch, array(
     CURLOPT_RETURNTRANSFER => false, 
     CURLOPT_CUSTOMREQUEST => strtoupper($method)/*, 
     CURLOPT_HTTPHEADER => array('Content-Type: application/json')*/ 
    )); 
    **curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));** 
    $retdata = curl_exec($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    return $httpcode; 
} 

而且它爲我工作。