首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elasticsearch-php更新返回糟糕的请求,而curl不返回

elasticsearch-php更新返回糟糕的请求,而curl不返回
EN

Stack Overflow用户
提问于 2015-01-19 22:17:14
回答 1查看 2.1K关注 0票数 0

我有一个令人沮丧的elasticsearch问题,我试图更新一个索引。无论我尝试什么,只要返回“坏请求”(下面的堆栈),而更新与cURL很好地工作。我尝试使用$u‘’body‘作为JSON和数组进行更新调用。

这样做很好:

代码语言:javascript
复制
curl -XPOST 'http://localhost:9200/my_index/twitter/555805761389867009/_update' -d '{"doc": {"_located":"1","country_code":"IL"}}'

然而,这段代码:

代码语言:javascript
复制
<?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();
      }
    }
  }
?>

返回:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-20 01:28:40

我认为:

代码语言:javascript
复制
 $u['body']

应:

代码语言:javascript
复制
$u['body']['doc']

至少根据github的文件:

更新文档 让我们更新一个已编入索引的文档: $updateParams'index‘= 'my_index';$updateParams'type’= 'my_type';$updateParams'id‘= 'my_id';$updateParams’‘body’= array('my_key‘=> 'new_value');$retUpdate = $client->update($updateParams);

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28034472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档