簡體   English   中英

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

[英]Search using PHP Curl not returning Aggregation results

我的地圖:

"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。 我在這里做錯了什么?

遇到問題了,我沒有將$ 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;
}

它為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM