Skip to content
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

Can I set header in the search endpoint 'Content-Type'=> 'application/json' #205

Closed
charuag opened this issue Mar 27, 2015 · 16 comments
Closed

Comments

@charuag
Copy link

charuag commented Mar 27, 2015

No description provided.

@polyfractal
Copy link
Contributor

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.

@charuag
Copy link
Author

charuag commented Mar 27, 2015

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?

@polyfractal
Copy link
Contributor

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.

@charuag
Copy link
Author

charuag commented Mar 27, 2015

Yeah! It worked for me! Thanks a lot!

@charuag charuag closed this as completed Mar 27, 2015
@polyfractal
Copy link
Contributor

Great! Happy to help :)

@charuag
Copy link
Author

charuag commented Mar 28, 2015

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"
        }
    }

@charuag charuag reopened this Mar 28, 2015
@polyfractal
Copy link
Contributor

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

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

@charuag
Copy link
Author

charuag commented Mar 30, 2015

Thanks! got it!

@polyfractal
Copy link
Contributor

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

@adilhashem
Copy link

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

@polyfractal
Copy link
Contributor

@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

@adilhashem
Copy link

@polyfractal thanks!

@donluismx
Copy link

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

@polyfractal
Copy link
Contributor

@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: fd3b0f1

@dhirendrasingh-kiwi
Copy link

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";

@nivpenso
Copy link

nivpenso commented Nov 8, 2018

@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.

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

6 participants