yiisoft / yii2-elasticsearch

Yii 2 Elasticsearch extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
427 stars 253 forks source link

There is a problem with the spliced URL when using delete #295

Closed hkui closed 3 years ago

hkui commented 3 years ago

What steps will reproduce the problem?

$connect=\Yii::$app->get('es');

 $r=$connect->createCommand()->delete('book','book',null,['id'=>1]);
 var_dump($r);

What's expected?

What do you get instead?

{"error":"Incorrect HTTP method for uri [/book/_doc/?id=1] and method [DELETE], allowed: [POST]","status":405}

Additional info

It is mainly the problem of splicing URL when use the below function

 private function createUrl($path, $options = [])
    {
        if (!is_string($path)) {
            $url = implode('/', array_map(function ($a) {
                return urlencode(is_array($a) ? implode(',', $a) : $a);
            }, $path));
            if (!empty($options)) {
                $url .= '?' . http_build_query($options);
            }
        } else {
            $url = $path;
            if (!empty($options)) {
                $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($options);
            }
        }
        $node = $this->nodes[$this->activeNode];
        $protocol = isset($node['protocol']) ? $node['protocol'] : $this->defaultProtocol;
        $host = $node['http_address'];
        return [$protocol, $host, $url];
    }

The URL it generates is "/book/_doc/?id=1"

How can I use delete () correctly?

Q A
Yii version 2.0.39.3
PHP version 7.2.14
Operating system centos7
samdark commented 3 years ago

What should be generated?

hkui commented 3 years ago

What should be generated?

/book/_doc/1

beowulfenator commented 3 years ago

The proper signature for delete is:

public function delete($index, $type, $id, $options = [])

So, $connect->createCommand()->delete('book', '_doc', 1).