简体   繁体   English

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

[英]Search using PHP Curl not returning Aggregation results

My Mapping: 我的地图:

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

My Search Query JSON: 我的搜索查询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"
          }
        }
      }
    }
  }
}

When I am executing in command line using curl, I am getting correct output, which includes aggregations field in returned JSON: 当我使用curl在命令行中执行时,得到的输出正确,其中包括返回的JSON中的聚合字段:

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

Output: 输出:

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

However I am using PHP Curl, I am getting the same response, but response do not include the actual result from 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;
}

The output I am getting from php curl: 我从php curl得到的输出:

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

I am passing 'get' as method, and query json in $params. 我将“ get”作为方法传递,并在$ params中查询json。 What am I doing wrong here ? 我在这里做错了什么?

Got the problem, I didn't send the $params to my curl request in php. 遇到问题了,我没有将$ params发送到我在php中的curl请求中。

Modified the functions as: 将功能修改为:

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

And it worked for me. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM