Elasticsearch-php: How to make URI Search

Created on 16 Nov 2016  ·  5Comments  ·  Source: elastic/elasticsearch-php

Summary of problem or feature request

Regarding the one-to-one mapping with REST API, is there a way to make a URI Search via the SDK such as the following from the API docs, including support for all the other query string parameters:

GET twitter/tweet/_search?q=user:kimchy

I reviewed the various Endpoint classes and nothing jumped out at me as supporting this, which could be done using something like $endpoint->setURI(...) or via setting the query string params.

Code snippet of problem

When following code from the docs, the body param follows the Search DSL. Is there a way to use URI Search with this SDK?

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);

Most helpful comment

Yep, it's possible. It's not immediately obvious how (sorry about that!), mostly because the URI method is generally a shorthand for commandline searches, not so much for programmatic clients. But it's definitely possible!

You can do it by calling the search endpoint and specifying a q param:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'q' => 'user:kimchy'
];
$response = $client->search($params);

All 5 comments

Yep, it's possible. It's not immediately obvious how (sorry about that!), mostly because the URI method is generally a shorthand for commandline searches, not so much for programmatic clients. But it's definitely possible!

You can do it by calling the search endpoint and specifying a q param:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'q' => 'user:kimchy'
];
$response = $client->search($params);

Sounds good. Will this work for all the other params supported by URI search as well, e.g. df, analyzer, lowercase_expanded_terms, analyze_wildcard, default_operator, etc.?

Yep! Any parameter that would go in the URI can be added as a "top-level" parameter in the $params object. So just keep adding the ones you're interested in:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'q' => 'user:kimchy',
    'analyzer' => 'foo'  // etc
];
$response = $client->search($params);

Very cool! Much thanks!

np, happy to help! :)

Was this page helpful?
0 / 5 - 0 ratings