Elasticsearch-PHP 處理JSON數組和對象

來源:互聯網
上載者:User

標籤:版本   res   body   沒有   代碼產生   高亮   span   json   cli   

PHP中處理JSON數組和對象

用戶端有一些混淆的資源是圍繞著JSON的數組和對象,以及如何在PHP中指定它們。特別是,問題是由Null 物件和空數組導致的。這篇文章回告訴你一些在Elasticsearch JSON API中的常用模式,以及如何轉換成為PHP表示形式。

 

Null 物件

Elasticsearch API 在很多地方使用空的JSON對象,這可能在PHP中會導致一些問題。與其他語言不同,PHP並沒有為空白對象提供一個“短”的標示,所以許多開發人員都不知道如何制定一個Null 物件。

考慮向查詢中添加高亮顯示:

 

  1. {  
  2.     "query" : {  
  3.         "match" : {  
  4.             "content" : "quick brown fox"  
  5.         }  
  6.     },  
  7.     "highlight" : {  
  8.         "fields" : {  
  9.             "content" : {}//這個Null 物件就是導致問題的地方  
  10.         }  
  11.     }  
  12. }  

 


問題是PHP會自動轉換"content" : {} 成 "content" : [],這對於Elasticsearch DSL來說已不再有效。我們需要告訴PHPNull 物件是顯試的對象,不是一個數組。如果要在PHP中定義一個查詢,你需要這樣做:

 

  1. $params[‘body‘] = array(  
  2.     ‘query‘ => array(  
  3.         ‘match‘ => array(  
  4.             ‘content‘ => ‘quick brown fox‘  
  5.         )  
  6.     ),  
  7.     ‘highlight‘ => array(  
  8.         ‘fields‘ => array(  
  9.             ‘content‘ => (object) array()//我們投擲一個空的數組到一個對象上來表示一個空的對象,JSON現在能夠編碼正確  
  10.         )  
  11.     )  
  12. );  
  13. $results = $client->search($params);  

 

 

通過使用一個空數群組轉換為對象,我們可以強制 json_encode 解析器正確輸出一個空的對象,而不是一個空數組。可悲的是,這個冗長的解決方案是PHP中唯一能夠實現目標的。因為PHP並沒有為空白對象提供一個“短”的標示。

數組對象

 

Elasticsearch DSL中另外一個通用模式是數組對象,例如,考慮在查詢中添加一個排序:

 

  1. {  
  2.     "query" : {  
  3.         "match" : { "content" : "quick brown fox" }  
  4.     },  
  5.     "sort" : [ //"sort"包含一個JSON數組對象  
  6.         {"time" : {"order" : "desc"}},  
  7.         {"popularity" : {"order" : "desc"}}  
  8.     ]  
  9. }  

 

 

這種安排是非常常見的,但是在PHP中可能會非常棘手,以為她需要一個嵌套的數組,PHP冗長的難解的方案還在繼續,為了構造一個數組對象,你確實需要一個數組的數組:

 

  1. $params[‘body‘] = array(  
  2.     ‘query‘ => array(  
  3.         ‘match‘ => array(  
  4.             ‘content‘ => ‘quick brown fox‘  
  5.         )  
  6.     ),  
  7.     ‘sort‘ => array( //這個數組編碼成"sort": []  
  8.         array(‘time‘ => array(‘order‘ => ‘desc‘)), //這個數組編碼成{"time": {"order": "desc"}}  
  9.         array(‘popularity‘ => array(‘order‘ => ‘desc‘)) //這個數組編碼成{"popularity": {"order": "desc"}}  
  10.     )  
  11. );  
  12. $results = $client->search($params);  

 

 

如果你使用的是PHP5.4+的版本,我強烈鼓勵你使用短數組文法,它使得這些嵌套的數組讀起來更簡單:

 

  1. $params[‘body‘] = [  
  2.     ‘query‘ => [  
  3.         ‘match‘ => [  
  4.             ‘content‘ => ‘quick brown fox‘  
  5.         ]  
  6.     ],  
  7.     ‘sort‘ => [  
  8.         [‘time‘ => [‘order‘ => ‘desc‘]],  
  9.         [‘popularity‘ => [‘order‘ => ‘desc‘]]  
  10.     ]  
  11. ];  
  12. $results = $client->search($params);  

 

 

空數組對象


偶爾地,你會遇到需要前面兩種模式,積分查詢函數是一個好例子,有時需要一個數組對象,這些對象可能是空的JSON對象。

 

下面給出這個查詢:

  1. {  
  2.     "query":{  
  3.         "function_score":{  
  4.             "functions":[  
  5.                 {  
  6.                 "random_score":{}  
  7.                 }  
  8.             ],  
  9.             "boost_mode":"replace"  
  10.         }  
  11.     }  
  12. }  

 

 

我們可以使用下面的PHP代碼產生:

 

    1. $params[‘body‘] = array(  
    2.     ‘query‘ => array(  
    3.         ‘function_score‘ => array(  
    4.             ‘functions‘ => array( //  這個編碼成:"functions": []  
    5.                 array( // 這個編碼成一個對象在數組裡:{"random_score": {}}  
    6.                     ‘random_score‘ => (object) array() // 這個編碼成一個空的JSON對象: "random_score": {}  
    7.                 )  
    8.             )  
    9.         )  
    10.     )  
    11. );  
    12. $results = $client->search($params);  

Elasticsearch-PHP 處理JSON數組和對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.