2016-09-03 21 views
0
新字段添加到現有的索引

好,我創建了一個彈性的搜索索引稱爲用戶,看起來像這樣:在Elasticsearch

$response = $client->index([ 
     'index' => 'users', 
     'type' => 'user', 
     'id' => Auth::user()->id, 
     'body' => [ 
      'username' => Auth::user()->username, 
      'name' => Auth::user()->name, 
      'city' => Auth::user()->getCity(), 
     ], 

    ]); 

我已經收錄了一些數據。我想了一個名爲「位置」的新字段添加到該索引,以便它現在將是:

$response = $client->index([ 
     'index' => 'users', 
     'type' => 'user', 
     'id' => Auth::user()->id, 
     'body' => [ 
      'username' => Auth::user()->username, 
      'name' => Auth::user()->name, 
      'city' => Auth::user()->getCity(), 
      'location' => [$origin], 
     ], 

    ]); 

我的問題是我怎麼該字段添加到現有的數據。我已經有用戶,但沒有位置字段。我需要的是字段添加到老用戶的數據,這樣當用戶更新他們的信息,他們沒有得到任何碎片失蹤錯誤

{"error":{"root_cause":[{"type":"document_missing_exception","reason":"[user][130]: document missing","shard":"0","index":"users"}],"type":"document_missing_exception","reason":"[user][130]: document missing","shard":"0","index":"users"},"status":404} 

回答

1

我不確定你爲什麼看到碎片丟失了類似這樣的錯誤。通過Elasticsearch,默認情況下映射是動態的(請參閱Dynamic Mapping)。這意味着您應該能夠像索引API那樣創建索引API,以將新的位置信息添加到現有的和新的用戶文檔,並且能夠搜索這些信息。

這樣做的不利之處在於,您可能已經爲映射中的位置信息指定了某種類型的文檔建立了索引,並且後續文檔中的位置信息與此類型不匹配(請參閱Field datatypes)。也許,您可能想要考慮對Elasticsearch映射進行很好的控制,以避免遇到類似的問題,或者問題可能完全是其他問題。

編輯:根據最近關於document_missing_exception問題中提供的更多細節,這不是相關的。

+0

我的問題是當我創建索引時,我沒有'location'=> [$ origin]創建它,現在我想將它添加到我的索引中。當我在添加'location'=> [$ origin]之前註冊的用戶信息更新索引時,嘗試更新{「error」:{「root_cause」:[{「type」:「document_missing_exception」 ,「reason」:「[user] [130]:document missing」,「shard」:「0」,「index」:「users」}],「type」:「document_missing_exception」,「reason」:「[user ] [130]:文件丟失「,」碎片「:」0「,」索引「:」用戶「},」狀態「:404} – Luna

1

您可以通過使用Update API

如果這是實現這一目標一個新的索引 - 更新將失敗。

爲了避免這種情況 - 使用upsert。

+0

我知道如何更新文檔,但我在更新簽名ip的用戶之前添加'location'=> [$ origin]到索引時遇到問題。新用戶,我可以更新。它的用戶,如果我在添加到索引之前註冊,我遇到了問題 – Luna

+0

這是您的誤解。更新是現有值的一個選項,以便您想要添加字段(正是您想要實現的內容),更新的問題是當找不到id時,因此您必須使用Upsert(在未找到id時插入) 。 – Erik