Reputation: 557
Ok I've created an elastic search index called users that looks like this:
$response = $client->index([
'index' => 'users',
'type' => 'user',
'id' => Auth::user()->id,
'body' => [
'username' => Auth::user()->username,
'name' => Auth::user()->name,
'city' => Auth::user()->getCity(),
],
]);
I already indexed some data. I want to add a new field called 'location' to this index so that it will now be:
$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],
],
]);
my question is how do I add that field to the already existing data. I already have users , but without the location field. I need to add that field to the old users data so that when a user updates their info, They don't get any shard missing errors
{"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}
Upvotes: 2
Views: 9812
Reputation: 21
If you want your field to be populated with already indexed data, you should create a new index with desired mapping and reindex all your data. A good example is shown at https://www.youtube.com/watch?v=YSd1aV3iVhM&list=PL_mJOmq4zsHbcdoeAwNWuhEWwDARMMBta&index=23
Upvotes: 0
Reputation: 1166
I am not sure why you are seeing shards missing errors for something like this. With Elasticsearch, mappings are dynamic by default (see Dynamic Mapping). This means you should be able to hit the index API as you are already doing to add new location information to existing and to new user documents and be able to search this information.
The downside to this is that you may have indexed a document where location information was assigned a certain type in the mapping and subsequent documents have location information that do not match this type (see Field datatypes). Perhaps, you might want to look into having a great control over the Elasticsearch mappings to avoid running into issues like that or maybe the issue is something else entirely.
EDIT: based on more details provided in the question recently regarding the document_missing_exception
, this is not as relevant.
Upvotes: 1
Reputation: 73
You can achieve that by using the Update API
If that's a new index - the update will fail.
In order to avoid that - use upsert.
Upvotes: 1