2016-07-25 31 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