Skip to content

Use POST method for search requests with body #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gleb-svitelskiy opened this issue Mar 15, 2018 · 2 comments
Closed

Use POST method for search requests with body #737

gleb-svitelskiy opened this issue Mar 15, 2018 · 2 comments

Comments

@gleb-svitelskiy
Copy link

Summary of problem or feature request

GET requests with a body are wrong for some proxies. POST request type works. GET requests can't contain the body, that is the reason.

Code snippet of problem

use Elasticsearch\ClientBuilder;
...
$client = ClientBuilder::create()->setHosts(['http://elastic:password@localhost/'])->build();
            $query = [
                "body" => [
                    [
                        'index' => 'index_name',
                        'type' => 'type_name',
                        'timeout' => '3s',
                    ],
                    [
                        'query' => [
                            'match_all' => []
                        ],
                        "size" => 1
                    ]
                ]
            ];
$client->msearch($query);

-->

System details

  • Operating System CentOS 6.9
  • PHP Version 7.1.3
  • ES-PHP client version v6.0.1
  • Elasticsearch version 6.2
@gleb-svitelskiy
Copy link
Author

gleb-svitelskiy commented Mar 27, 2018

[gleb.svitelskiy@dev.localhost ~]$ curl -XGET -H 'Content-Type: application/json' -o /dev/null -sSL -D - http://elasticsearch.localhost:9200/test_index/_search?pretty -d '
> {
>   "size": 1,
>   "_source": false
> }
> '
HTTP/1.1 411 Length Required
Server: squid
Mime-Version: 1.0
Date: Tue, 27 Mar 2018 06:23:52 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 4547
X-Squid-Error: ERR_INVALID_REQ 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: NONE from localhost.localdomain:3128
Via: 1.1 localhost.localdomain (squid)
Connection: close

[gleb.svitelskiy@dev.localhost ~]$ curl -XPOST -H 'Content-Type: application/json' -o /dev/null -sSL -D - http://elasticsearch.localhost:9200/test_index/_search?pretty -d '
> {
>   "size": 1,
>   "_source": false
> }
> '
HTTP/1.1 200 OK
Date: Tue, 27 Mar 2018 06:23:58 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 354
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.1 localhost.localdomain (squid)
Connection: keep-alive

[gleb.svitelskiy@dev.localhost ~]$ curl -XGET -H 'Content-Type: application/json' -o /dev/null -sSL -D - 'http://elasticsearch.localhost:9200/test_index/_search?pretty=pretty&size=1'
HTTP/1.1 200 OK
Date: Tue, 27 Mar 2018 06:41:25 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 2593
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.1 localhost.localdomain (squid)
Connection: keep-alive

@polyfractal
Copy link
Contributor

Both are valid. GET requests can use a body; it isn't forbidden by the HTTP spec. See the callout titled "A GET Request with a Body?" at: https://www.elastic.co/guide/en/elasticsearch/guide/current/_empty_search.html for more details.

That said, if you need to change the HTTP method to better suit your environment, you can override it:

$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
    'index' => 'test',
    'type' => 'test',
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ],
    'client' => [
        'curl' => [
            CURLOPT_CUSTOMREQUEST => 'POST'
        ]
    ]
];
$client->search($params);

As an aside, running search requests through a caching proxy is probably a bad idea. They could easily cache a search request that is no longer valid because the underlying data has changed. Caching other API endpoints may make sense, but caching search rarely does. Elasticsearch has several built-in caches at various levels, so a top-level caching layer is not usually needed (and can lead to data inconsistency or confusing situations).

E.g. imagine the results are cached, user clicks on one of the hits for more information then the following GetDocument API fails because the document was actually deleted (but the search hits were cached).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants