Luna Luna - 2 months ago 21
PHP Question

Add new field to existing index in Elasticsearch

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}

Answer

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.

Comments