elasticsearch-php icon indicating copy to clipboard operation
elasticsearch-php copied to clipboard

The error when init object of elasticsearch-php

Open StartMoney opened this issue 2 years ago • 7 comments

Summary of problem or feature request

There's some error when i connected to elasticsearch use elasticsearch-php . my es server is : https://elasticsearch:9200, all this is configed in $cfg

        $cfg = [
            'host' => 'elasticsearch',
            'port' => '9200',
            'username' => 'elastic',
            'password' => 'mypassxxxxxx',
            'scheme'   => 'https',
            'ca'       => '/var/www/certs/ca/ca.crt'
        ];
            $_elasticsearch = \Elastic\Elasticsearch\ClientBuilder::create()
                ->setHosts([$cfg['scheme'] . '://' .$cfg['host'] . ':' . $cfg['port']])
                ->setBasicAuthentication($cfg['username'], $cfg['password'])
                ->setCABundle($cfg['ca'])
                ->build();

            $search_body = [
                'query' => [
                    'match' => [
                        'name_all' => 'keyword'
                    ]
                ],
                "highlight" => [
                    "fields" => [
                        "name_all" => (object)[]
                    ],
                    "pre_tags" => ["<p class='highliht'>"],
                    "post_tags" => ["</p>"]
                ]
            ];

        $result_obj = $_elasticsearch ->search([
            'index' => 'material_user_input',
            'body'  => $search_body
        ]);
        $result = $result_obj->asArray();

app            | Array
app            | (
app            |     [error] => Array
app            |         (
app            |             [root_cause] => Array
app            |                 (
app            |                     [0] => Array
app            |                         (
app            |                             [type] => security_exception
app            |                             [reason] => unable to authenticate user [elastic] for REST request [/material_user_input/_search]
app            |                             [header] => Array
app            |                                 (
app            |                                     [WWW-Authenticate] => Array
app            |                                         (
app            |                                             [0] => Basic realm="security" charset="UTF-8"
app            |                                             [1] => Bearer realm="security"
app            |                                             [2] => ApiKey
app            |                                         )
app            |
app            |                                 )
app            |
app            |                         )
app            |
app            |                 )
app            |
app            |             [type] => security_exception
app            |             [reason] => unable to authenticate user [elastic] for REST request [/material_user_input/_search]
app            |             [header] => Array
app            |                 (
app            |                     [WWW-Authenticate] => Array
app            |                         (
app            |                             [0] => Basic realm="security" charset="UTF-8"
app            |                             [1] => Bearer realm="security"
app            |                             [2] => ApiKey
app            |                         )
app            |
app            |                 )
app            |
app            |         )
app            |
app            |     [status] => 401
app            | )

The ca file is ok :

root@f30ddb6a191e:/var/www/code# curl --cacert /var/www/certs/ca/ca.crt -u elastic https://elasticsearch:9200
Enter host password for user 'elastic':
{
  "name" : "elasticsearch",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "e5xxxxxxxxxxx_-YC1dcA",
  "version" : {
    "number" : "8.2.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "b174af6xxxxxxxxxx2b9282c5",
    "build_date" : "2022-04-20T10:35:10.180408517Z",
    "build_snapshot" : false,
    "lucene_version" : "9.1.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}
  • Operating System
  • PHP Version: 7.4.28
  • ES-PHP client version: 8.2.0
  • Elasticsearch version: 8.2.0

StartMoney avatar Jun 20 '22 09:06 StartMoney

@StartMoney does your password contain any special characters?

hamzamogni avatar Jun 23 '22 13:06 hamzamogni

@StartMoney does your password contain any special characters?

No,it always shows this error whatever my password is. But I seem to have found the reason. Its the password we set in .env, It did not take effect to elasticsearch-php . I used the password rest tool changed the password of user "elastic" again , like this: bin/elasticsearch-reset-password -u elastic -i seted password to the same in .env. And use this : curl -s -XPOST --cacert config/certs/ca/ca.crt -u elastic:mypass_in_.env -H "Content-Type: application/json" https://elasticsearch:9200/_security/user/elastic/_password -d "{\"password\":\"mypass_in_.env\"}" to set again. Reset the password again and again , now , it's runs ok. I think there must be a bug , that when we set password in .env, it's not take effect for all the place where password stored . when we reset password again and again , it works.

StartMoney avatar Jun 27 '22 01:06 StartMoney

@StartMoney I confirm that elasticsearch-php do not use any environmental variables for password. You need to pass it using the ClientBuilder::setBasicAuthentication() method. Anyway, did you solve the issue? I didn't understand when you said that you had to reset the password again and again.

ezimuel avatar Jun 27 '22 17:06 ezimuel

@ezimuel Hi, thanks for reply. I followed this to install elasticsearch, and my php is runing by the same docker-compose.yml https://www.elastic.co/guide/en/elasticsearch/reference/8.2/docker.html The password is seted in environment , in .env file. When all is start up , everything is fine except elasticsearch-php . Then I reseted my password "mypass" to "mypass" , yes , nochanges , the new is the same as the old . 1: bin/elasticsearch-reset-password -u elastic -i 2: curl -s -XPOST --cacert config/certs/ca/ca.crt -u elastic:mypass -H "Content-Type: application/json" https://elasticsearch:9200/_security/user/elastic/_password -d "{\"password\":\"mypass\"}" 3: elasticsearch-php is ok. “Change password" twice get things back on track. I don't know why , but it works.

StartMoney avatar Jun 28 '22 00:06 StartMoney

@ezimuel If you do more tests , rember to use docker-compose down -v remove volumes which was made by last test. or you can use : volumes: - ./certs:/usr/share/elasticsearch/config/certs - ./esdata01:/usr/share/elasticsearch/data and docker-compose.yml down, and delete ./certs , ./esdata01, ./esdata02 and ./esdata03 , then make another test.

StartMoney avatar Jun 28 '22 01:06 StartMoney

@StartMoney can you try to follow the instructions reported in the Elasticsearch-php documentation, here? Let me know if you have the same issue, thanks.

ezimuel avatar Jun 29 '22 10:06 ezimuel

OK, I have tested . It's not the problem of Elasticsearch-php ,it's the problem of building elasticsearch with doker-compose.yml Maybe I should post a new comment for docker of elasticsearch. Thanks for reply.

StartMoney avatar Jun 30 '22 07:06 StartMoney