sanity-io / sanity-algolia

Utilities for indexing Sanity documents in Algolia
MIT License
67 stars 16 forks source link

Fixed multiple delete calls for the same index #24

Closed stbychkov closed 1 year ago

stbychkov commented 2 years ago

https://github.com/sanity-io/sanity-algolia/issues/23

Problem

There are multiple delete requests to the same Algolia search index if there are multiple types defined.

Steps to reproduce

  1. Create an indexer
const algoliaIndex = algolia.initIndex(process.env["ALGOLIA_INDEX_NAME"]);
const sanityIndexer = indexer(
      {
        post: { index: algoliaIndex },
        article: { index: algoliaIndex }
      },
      () => ({
        title: 'Hello',
      })
    )
  1. Delete a document in a Sanity dataset and look at the Algolia logs. There are 2 calls only for one delete operation. image with the same payload
    {
    "requests": [
    {
      "action": "deleteObject",
      "body": {
        "objectID": "1111111-1111-1111-1111-111111111111"
      }
    }
    ]
    }

    Code issue

There are multiple invocations of delete operations for all registered indexes, even if it is the same one.

Solution

There are a few possible solutions to fix this bug:

  1. Fetch the data for ids in deleted collection and filter objects by type as you did in saveObjects operation
  2. To avoid additional calls to the Sanity, it might be a good idea to put all indexers into a unique collection like Set and delete records only from unique indexers.
runeb commented 1 year ago

Thank you for the patch @stbychkov. Apologies for keeping it open so long