Arrays of empty objects

edit

Occasionally, you’ll encounter DSL that requires both of the previous patterns. The function score query is a good example, it sometimes requires an array of objects, and some of those objects might be empty JSON objects.

Given this query:

{
   "query":{
      "function_score":{
         "functions":[
            {
               "random_score":{}
            }
         ],
         "boost_mode":"replace"
      }
   }
}

We can build it using the following PHP code:

$params['body'] = array(
    'query' => array(
        'function_score' => array(
            'functions' => array(  
                array(  
                    'random_score' => (object) array() 
                )
            )
        )
    )
);
$results = $client->search($params);

This encodes the array of objects: "functions" : []

This encodes an object inside the array: { "random_score": {} }

This encodes the empty JSON object: "random_score": {}