使用PHP Curl搜索不返回聚合结果

时间:2016-08-03 11:13:13

标签: php curl elasticsearch php-curl

我的映射:

"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 Curl,我得到相同的响应,但响应不包括聚合的实际结果:

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 curl得到的输出:

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

我传递'get'作为方法,并在$ params中查询json。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

遇到问题,我没有将$ params发送到php中的curl请求。

将功能修改为:

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;
}

它对我有用。