2016-07-25 73 views
1

我們目前正在我們的webapp中實施ElasticSearch。我們通過使用官方elasticsearch-php framework來做到這一點。我們將這一直貫徹到模型中來測試事情,因爲它對我們所有人都是新的。ElasticSearch沒有更新

public function afterSave() 
{ 
    $client = \Elasticsearch\ClientBuilder::create()->build(); 
    $params = [ 
     'index' => 'testindex', 
     'type' => 'user', 
     'id' => $this->id, 
     'body' => [ 
      'id' => $this->id, 
      'name' => $this->firstname.' '.$this->lastname, 
      'email' => $this->email, 
      'test' => '' 
     ] 
    ]; 

    $response = $client->index($params); 
} 

public function afterUpdate() 
{ 
    $client  = \Elasticsearch\ClientBuilder::create()->build(); 
    $params  = [ 
     'index' => 'testindex', 
     'type' => 'user', 
     'id' => $this->id 
    ]; 

    $params['body']['doc'] = [ 
     'test' => 'test2' 
    ]; 

    $response = $client->update($params); 
    var_dump($client->update($params)); 
    var_dump($response); 
} 

當我保存模型時,它會自動將它添加到ES,但是當我更新它時,它不會更新模型。當我回應時,它不會拋出任何錯誤。

array(4) { ["index"]=> string(11) "testindex" ["type"]=> string(5) "user" ["id"]=> string(5) "20604" ["body"]=> array(1) { ["doc"]=> array(1) { ["test"]=> string(5) "test2" } } } 

array(4) { ["_index"]=> string(11) "testindex" ["_type"]=> string(5) "user" ["_id"]=> string(5) "20604" ["_version"]=> int(2) } 

array(4) { ["_index"]=> string(11) "testindex" ["_type"]=> string(5) "user" ["_id"]=> string(5) "20604" ["_version"]=> int(3) } 

我在做什麼錯在這裏?

+0

的'_version'正確遞增每次更新,現在看來,這意味着更新工作。運行'curl -XGET localhost:9200/testindex/user/20604'時會得到什麼? – Val

+0

相同,不正確的輸出。 '{ 「_index」: 「testindex」, 「_類型」: 「用戶」, 「_ ID」: 「20604」, 「_版本」:4 「找到」:真, 「_源」:{ 「ID」: 「20604」 ,「name」:「test person」,「email」:「[email protected]」,「test」:「」}}' –

+0

如果你運行我從你的shell給你的curl,你應該簡單地獲取文檔ID爲'20604',請分享。 – Val

回答

0

如果您使用的是Phalcon,這可能是由撥打afterUpdate()afterSave()update()造成的。您應該在afterSave()方法中放置幾個​​echo;如果你看到他們兩次,這是問題。

您可以通過使您的afterSave()句柄更新和afterCreate()句柄創建來防止此行爲。然後,您可以調用$model->create()創建模型,並通過致電$model->save()進行更新。有關此行爲

的更多信息,可以發現:https://docs.phalconphp.com/en/latest/reference/models.html#create-update-with-confidence