簡體   English   中英

elasticsearch-php更新返回錯誤請求,其中curl不

[英]elasticsearch-php update returns bad request where curl does not

我在嘗試更新索引時遇到了令人沮喪的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中的文檔:

更新文件

讓我們更新已建立索引的文檔:

 $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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM