A more complicated example

edit

Let’s construct a slightly more complicated example: a filtered query that contains both a filter and a query. This is a very common activity in elasticsearch queries, so it will be a good demonstration.

The curl version of the query:

curl -XGET 'localhost:9200/my_index/my_type/_search' -d '{
    "query" : {
        "filtered" : {
            "filter" : {
                "term" : { "my_field" : "abc" }
            },
            "query" : {
                "match" : { "my_other_field" : "xyz" }
            }
        }
    }
}'


And in PHP:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'filtered' => [
                'filter' => [
                    'term' => [ 'my_field' => 'abc' ]
                ],
                'query' => [
                    'match' => [ 'my_other_field' => 'xyz' ]
                ]
            ]
        ]
    ]
];


$results = $client->search($params);