Is it somehow possible to delete a specific field ...
# community-help
j
Is it somehow possible to delete a specific field inside all documents?
f
You can drop fields in a collection by updating said collection https://typesense.org/docs/0.24.0/api/collections.html#update-or-alter-a-collection
j
Dropping collection fields, do not drop document fields! I need to drop the document field
m
@JR just out of interest, what type of field are you trying to delete? I think we had a similar issue when deleting a float[] field with embeddings - the field was dropped from collection but persisted in document data.
j
string[]
I saved product categories with their names, i now decided to switch to
int32[]
category ids, but
categories
still exists after dropping, so i can not change the field type to
int32[]
My workaround now is: Exporting all data, convert category names to ids, dropping all documents, importing exported & converted data.
m
I think we managed it by updating all documents (by query) and setting the field value to an empty array first, then dropping the field from the schema. Not 100% sure tho, but something like that. Probably something unexpected with array fields, but I’ll wait for the TS guys to confirm :)
j
Yeah, maybe this is a better/faster workaround. But as you say, maybe a TS guy has an idea
f
Since you're reindexing the collection, you could use an alias and point to the first schema first, then index the new collection and point to that alias afterwards, to avoid both downtime and reindexing on the same collection
m
@Fanis Tharropoulos here is a reproducible example: after dropping the
categories
field from the schema, the documents still retain it with all the data. Is this the expected behaviour?
Copy code
curl "<http://localhost:8108/collections>" \
   -X POST \
   -H "Content-Type: application/json" \
   -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
   -d '{
     "name": "products",
     "fields": [
       {"name": "title", "type": "string", "optional": true},
       {"name": "categories", "type": "string[]", "optional": true}
     ]
   }'
Copy code
curl "<http://localhost:8108/collections/products/documents/import?action=upsert>" \
  -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
  -H "Content-Type: text/plain" \
  -X POST \
  -d '{"id": "1", "title":"Dress", "categories":["Clothing", "Dresses"]}
      {"id": "2", "title":"Top", "categories":["Clothing", "Tops"]}'
Copy code
curl "<http://localhost:8108/collections/products>" \
       -X PATCH \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
         "fields": [
           {"name": "categories", "drop": true}
         ]
       }'
Copy code
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
  "<http://localhost:8108/collections/products/documents/search?q=*>"
f
I'll check in in a minute!
k
Dropping a field only affects schema and in-memory index, not data. Because Typesense documents can store any number of fields that are not defined in the schema within the document. The only way to remove the field from the document is to update it.
m
Ok cool, thanks for confirming!
f
@Kishore Nallan Should we put a info in the docs regarding that operation?
🙌 1
👍 2
j
And thats the next question - how do i update the document(s) to delete a field? An endpoint for removing fields from documents would be nice, something like a partial un-index
k
You can use update by query: https://typesense.org/docs/26.0/api/documents.html#update-by-query You need a filter condition that targets all documents.
j
But how do i send the data?
categories: null
would set the
categories
to
null
not remove them, am I right?
k
Null will be treated as deletion during updates. The document might still have that null value, which you might have to handle in the client.
j
Okay, so it is not possible to really delete a document field without reindex the document it self. Maybe this should be also documented. Or at least mentioned that the described way is the only way to get rid of old document fields. Thank you very much 🙂
😁 1
k
Ok I just checked: sending
null
treats the operations as deletion for updates.
j
So sending
categories: null
would delete field
categories
in all documents?
k
Via update by query, yes.
j
👍