- PHP Client: other versions:
- Overview
- Quickstart
- Installation
- Configuration
- Inline Host Configuration
- Extended Host Configuration
- Authorization and Encryption
- Set retries
- Enabling the Logger
- Configure the HTTP Handler
- Setting the Connection Pool
- Setting the Connection Selector
- Setting the Serializer
- Setting a custom ConnectionFactory
- Set the Endpoint closure
- Building the client from a configuration hash
- Per-request configuration
- Future Mode
- Dealing with JSON Arrays and Objects in PHP
- Index Management Operations
- Indexing Documents
- Getting Documents
- Updating Documents
- Deleting documents
- Search Operations
- Namespaces
- Security
- Connection Pool
- Selectors
- Serializers
- PHP Version Requirement
- Breaking changes from 5.x
- Community DSLs
- Community Integrations
- Reference - Endpoints
- Elasticsearch\Client
- Elasticsearch\ClientBuilder
- Elasticsearch\Namespaces\CatNamespace
- Elasticsearch\Namespaces\ClusterNamespace
- Elasticsearch\Namespaces\IndicesNamespace
- Elasticsearch\Namespaces\IngestNamespace
- Elasticsearch\Namespaces\NodesNamespace
- Elasticsearch\Namespaces\RemoteNamespace
- Elasticsearch\Namespaces\SnapshotNamespace
- Elasticsearch\Namespaces\TasksNamespace
IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
current release documentation.
Arrays of Objects
editArrays of Objects
editAnother common pattern in Elasticsearch DSL is an array of objects. For example, consider adding a sort to your query:
{ "query" : { "match" : { "content" : "quick brown fox" } }, "sort" : [ {"time" : {"order" : "desc"}}, {"popularity" : {"order" : "desc"}} ] }
This arrangement is very common, but the construction in PHP can be tricky since it requires nesting arrays. The verbosity of PHP tends to obscure what is actually going on. To construct an array of objects, you actually need an array of arrays:
$params['body'] = array( 'query' => array( 'match' => array( 'content' => 'quick brown fox' ) ), 'sort' => array( array('time' => array('order' => 'desc')), array('popularity' => array('order' => 'desc')) ) ); $results = $client->search($params);
This array encodes the |
|
This array encodes the |
|
This array encodes the |
If you are on PHP 5.4+, I would strongly encourage you to use the short array syntax. It makes these nested arrays much simpler to read:
$params['body'] = [ 'query' => [ 'match' => [ 'content' => 'quick brown fox' ] ], 'sort' => [ ['time' => ['order' => 'desc']], ['popularity' => ['order' => 'desc']] ] ]; $results = $client->search($params);
Was this helpful?
Thank you for your feedback.