gpt4 book ai didi

php - elasticsearch-php更新返回错误请求,其中curl不

转载 作者:行者123 更新时间:2023-12-03 02:04:42 37 4
gpt4 key购买 nike

我在尝试更新索引时遇到了令人沮丧的elasticsearch问题。无论我尝试什么,它都将返回“错误请求”(下面的堆栈),而更新与cURL一起正常运行。我试图用$ u ['body']作为JSON和数组进行更新调用。

这工作正常:

curl -XPOST 'http://localhost:9200/my_index/twitter/555805761389867009/_update' -d '{"doc": {"_located":"1","country_code":"IL"}}'

但是,此代码:
<?php
require 'vendor/autoload.php';
require 'db.php';

$q = "SELECT * FROM countries";
$res = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($res)) {
$find[] = "/.*?".$row['c_name'].".*?/imUs";
$replace[] = $row['c_short'];
}
$client = new Elasticsearch\Client();

$tmp['index'] = "my_index";
$tmp['type'] = "twitter";
$tmp['body']['query']['match']['_located'] = 0;
$tmp['fields'] = 'text';
$tmp['size'] = 100;
$store = $client->search($tmp);
foreach($store['hits']['hits'] as $field) {
if($m = preg_replace($find,$replace,$field['fields']['text'][0])) {
if(strlen($m)==2) {
unset($u);
$u['index'] = 'my_index';
$u['type'] = 'twitter';
$u['id'] = $field['_id'];
$u['body'] = json_encode(array('_located'=>"1", "country_code"=>$m));
print_r($u);
$client->update($u);
die();
}
}
}
?>

返回值:
Array
(
[index] => my_index
[type] => twitter
[id] => 555805761389867009
[body] => {"_located":"1","country_code":"IL"}
)

PHP Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException' with message 'Client error response
[status code] 400
[reason phrase] Bad Request
[url] http://localhost:9200/my_index/twitter/555805761389867009/_update' in /home/martin/el-test/elasticsearch-php/vendor/guzzle/http/Guzzle/Http/Exception/BadResponseException.php:43
Stack trace:
#0 /home/martin/el-test/elasticsearch-php/vendor/guzzle/http/Guzzle/Http/Message/Request.php(145): Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\EntityEnclosingRequest), Object(Guzzle\Http\Message\Response))
#1 [internal function]: Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#2 /home/martin/el-test/elasticsearch-php/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(164): call_user_func(Array, Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#3 in /home/martin/el-test/elasticsearch-php/src/Elasticsearch/Connections/GuzzleConnection.php on line 271

最佳答案

我相信:

 $u['body']
应该:
$u['body']['doc']
至少根据github中的文档:

Update a document

Let's update a document we have indexed:

$updateParams['index']          = 'my_index';
$updateParams['type'] = 'my_type';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc'] = array('my_key' => 'new_value');

$retUpdate = $client->update($updateParams);

https://github.com/elasticsearch/elasticsearch-php

关于php - elasticsearch-php更新返回错误请求,其中curl不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034472/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com