2015-10-15 54 views
0

我遇到了Elasticsearch腳本過濾器和捲髮後URI的問題。Elasticsearch在php中添加腳本過濾器到URL

我傳遞使用的URL值如下

我與參數

$params = rawurlencode('firstName:John'); 
    $url= 'http://localhost:9200/jdbc/_search?q='.$params.'&pretty=true'; 

的json代碼URL:

$options='{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "script": { 
      "script": "(min..max).contains(doc.admitted_date.date.year)", 
      "params": { 
      "min": 1999, 
      "max": 2000 
      } 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "citycount": { 
     "cardinality": { 
     "field": "cityid", 
     "precision_threshold": 100 
     } 
    } 
    } 
}'; 

捲曲傳:

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$options); 
    ob_start(); 
    curl_exec ($ch); 
    curl_close ($ch); 
    $data = ob_get_contents(); 
    ob_end_clean(); 

當我通過t他上面的查詢,顯示錯誤的結果。因爲我們以兩種格式傳遞帶有URL和JSON的參數。所以有一次它只會從URL中獲取參數。有什麼辦法可以傳遞這兩個參數,或者有任何其他方式來傳遞這個參數嗎?有沒有辦法通過腳本在URL中Elasticsearch PHP

回答

1

這樣做的正確的方法是通過在查詢中的一切,包括比賽的firstName領域:

$options='{ 
    "query": { 
    "filtered": { 
     "query": { 
     "query_string": { 
      "query": "firstName:John" 
     } 
     }, 
     "filter": { 
     "script": { 
      "script": "(min..max).contains(doc.admitted_date.date.year)", 
      "params": { 
      "min": 1999, 
      "max": 2000 
      } 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "citycount": { 
     "cardinality": { 
     "field": "cityid", 
     "precision_threshold": 100 
     } 
    } 
    } 
}'; 

$url= 'http://localhost:9200/jdbc/_search?pretty=true'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1);    <---- also add this 
curl_setopt($ch, CURLOPT_POSTFIELDS, $options); 
ob_start(); 
curl_exec ($ch); 
curl_close ($ch); 
$data = ob_get_contents(); 
ob_end_clean();