Elasticsearch-php: Can I set header in the search endpoint 'Content-Type'=> 'application/json'

Created on 27 Mar 2015  路  16Comments  路  Source: elastic/elasticsearch-php

Most helpful comment

@adilhashem You can set curl headers with the ['client]['curl'] param in 2.x:

$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
    'index' => 'test',
    'type' => 'test',
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ],
    'client' => [
        'curl' => [
            CURLOPT_HTTPHEADER => [
                'Content-type: application/json',
                'Authorization: foobarbaz'
            ]
        ]
    ]
];
$client->search($params);

It's done on a per-request basis right now, there isn't (currently) a way to specify it for the entire client. There's an open issue for that: #382

All 16 comments

It should be possible via setting custom curl options. Are you using ES-PHP 1.0 or 2.0? The syntax will be a little different for each.

I am using v1.3.3 Can you tell how to set header 'Content-Type'=> 'application/json' under this version
Also, do I need to set connectionPoolClass to CurlMultiConnection?

Untested, but I think this should work:

$params = [ 
  'guzzleOptions' => [
    'curl.options' => [
        CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ]
    ]
];
$client = new Elasticsearch\Client($params);

If it doesn't, I'll break out the debugger and test it to find the right invocation. But I think that should work :)

You don't have to use CurlMultiConnection if you don't want, the default Guzzle can be configured to set custom headers too.

Yeah! It worked for me! Thanks a lot!

Great! Happy to help :)

Hey! I have one more question. How to query in elasticsearch php for nested fields. For eg. If I want to search for field22 in below document.

"_source"
    "field1": "val1",
    "field2": {
        "field21": {
            "field22" : "data"
        }
    }

You can use the full dot notation path to the field:

$params = [
  'query' => [
    'match' => [
      'field2.field21.field22' => 'data'
    ]
  ];
]

Thanks! got it!

Great! Closing for now, feel free to reopen if you have more problems (or open a new ticket).

@polyfractal how would one set custom headers in 2.x?

@adilhashem You can set curl headers with the ['client]['curl'] param in 2.x:

$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
    'index' => 'test',
    'type' => 'test',
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ],
    'client' => [
        'curl' => [
            CURLOPT_HTTPHEADER => [
                'Content-type: application/json',
                'Authorization: foobarbaz'
            ]
        ]
    ]
];
$client->search($params);

It's done on a per-request basis right now, there isn't (currently) a way to specify it for the entire client. There's an open issue for that: #382

@polyfractal thanks!

Recent ES 6.0 comes with strict content-type checking, so now you have define the content type, is not optional anymore, thanks @polyfractal , I felt lost for a couple of days.

https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests

@donluismx FYI, if you're using Elasticsearch 6.0+, you need to upgrade your ES-PHP client to the 6.0 branch too. ES-PHP 6.0+ sets the content-type headers automatically: https://github.com/elastic/elasticsearch-php/commit/fd3b0f16f7e09cb2096ef5f2d656c9fd8dd3d61d

In #721 #721
file path: /var/www/html/projectName/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php

$headers = [];

    if (!empty($headersConfig)) {
        $headers = [];
        while (list($header, $headerValue) = each($headersConfig)) {
            array_push($headers, $header.': '.$headerValue);
        }
    }

//add below line at line number 114 in this file/////
$headers[] = "Content-Type: application/json";

@adilhashem You can set curl headers with the ['client]['curl'] param in 2.x:

$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
    'index' => 'test',
    'type' => 'test',
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ],
    'client' => [
        'curl' => [
            CURLOPT_HTTPHEADER => [
                'Content-type: application/json',
                'Authorization: foobarbaz'
            ]
        ]
    ]
];
$client->search($params);

It's done on a per-request basis right now, there isn't (currently) a way to specify it for the entire client. There's an open issue for that: #382

I try to use this code but for some reason, it doesn't work for I get. "Notice: Array to string conversion" error and the request is not being sent.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marksparrish picture marksparrish  路  6Comments

ezimuel picture ezimuel  路  6Comments

kabudu picture kabudu  路  4Comments

dinamic picture dinamic  路  5Comments

KarboniteKream picture KarboniteKream  路  5Comments