简体   繁体   中英

How to delete object in Algolia by specific key in JavaScript?

I have a object with this data.

{
     _id:"5ec6cdf648fdd1678c6f20a6",
     title:"Abc",
     objectID:"35922720"
}

How can I delete data by _id ?

Note, this is a custom property and not the standard Algolia property objectId .

There are only two ways to delete items in an Algolia index:

  • Delete Object: which deletes an object by the Algolia applied objectId . More here .
  • Delete By Query: which deletes objects meeting a supplied query. A limitation of this is that it only supports a few options of query (numeric, facet, or tag) and geo queries. More here

Based on this, you can delete an object directly using "Delete by Query" if it can be selected by a supported query option. If it cannot be deleted using Delete by Query, then you would need to retrieve the objects and delete them by the Algolia standard objectId property.

In this scenario, _id is a custom string property so you cannot use "Delete by Query", but would need to perform a select query first to retrieve the object's standard objectId property.

If you wanted to use Delete By Query and could modify the index, you could convert _id to a numeric field. Your id is a hexadecimal number in string representation so one approach could be to convert this to a decimal representation.

For example, if your object's custom _id property was numeric like the following, you could delete it using "Delete by Query".

{
  _id:12345,
  title:"Abc",
  objectID:"35922720"
}

The Delete By Query params would be:

{
  "params":"numericFilters=_id=12345"
}

See more on Numeric Filters here .

In the future, you may able to delete by a hexadecimal number as hexadecimal is supported in JSON5 but it would still need support by Algolia and a change in representation in your index. In this scenario, your object would look like:

{
  _id:0x5ec6cdf648fdd1678c6f20a6,
  title:"Abc",
  objectID:"35922720"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM