Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

refactor: count number of documents using hnswlib #1759

Open

Description

@jupyterjazz

Data storage in HnswDocumentIndex works in the following way:

  1. Vectors are stored on disk using hnswlib.
  2. All other types of data are saved in an SQLITE database.

One of the operations we frequently perform is determining the total number of documents (num_docs()). However, the only way to get number of documents from SQLITE is by scanning the entire table. Even though we've made efforts to reduce the number of times we use this functionality (#1729), it's still a time-consuming process.

For better performance, let's do the following: instead of scanning the SQLITE table, we can use hnswlib's get_current_count function to quickly get the number of documents in the index.

But there's a potential issue with this approach. What if documents don't have associated vectors? get_current_count would return 0.

We have two potential solutions:

  1. Notify/Warn users about this behavior and return 0.
  2. Use to the older method of counting using the SQL table if vector-less documents are detected.

Activity

  1. converted this from a draft issue on Aug 23, 2023
  2. added
    good-first-issueSuitable as your first contribution to DocArray!
    area/document-indexConcerning Document Index or a Document Index backend
    on Aug 23, 2023
  3. shobhit9957 commented on Aug 24, 2023

    @shobhit9957

    /attempt #1759

    I would like to solve this issue. Can I get some more help.

  4. JoanFM commented on Aug 24, 2023

    @JoanFM
    Member

    Hey @shobhit9957 ,

    Sure, the main refactoring to be done is to change the code here:

        def num_docs(self) -> int:
            """
            Get the number of documents.
            """
            if self._num_docs == 0:
                self._num_docs = self._get_num_docs_sqlite()
            return self._num_docs

    to something that does not rely on the _get_num_docs_sqlite but from a new private method _get_num_docs_hnsw

  5. shobhit9957 commented on Aug 24, 2023

    @shobhit9957

    so I should replace the _get_num_docs_sqlite to _get_num_docs_hnsw is that correct?
    I'm just a beginner, to this open-source-community. Just trying my hands out there...please help me out.

  6. JoanFM commented on Aug 24, 2023

    @JoanFM
    Member

    Yes, this would be the right approach

  7. shobhit9957 commented on Aug 24, 2023

    @shobhit9957

    Thanks! will do the PR Soon.

  8. shobhit9957 commented on Aug 24, 2023

    @shobhit9957

    Hey Joan. Submitted the PR, please check if there are any other issues or mistakes I've done in the PR, I would be happy to solve my mistakes and submit the PR again Joan. Thanks!

  9. shobhit9957 commented on Aug 24, 2023

    @shobhit9957

    def num_docs(self) -> int:
    """
    Get the number of documents.
    """
    if self._num_docs == 0:
    # Replace the old method with the new one
    self._num_docs = self._get_num_docs_hnsw()
    return self._num_docs

    def _get_num_docs_hnsw(self) -> int:
    """
    Get the number of documents using the HNSW method.
    This method should return the count of documents using the new technique.
    """
    # Implement your logic here to get the count using the HNSW method
    # For example, you might access some data or perform calculations
    # that help you quickly determine the number of documents.
    # Then return the count.
    return calculated_num_docs
    is this correct?

  10. moved this from Todo/Features to In progress by community in DocArray backlogon Sep 6, 2023
  11. munish0838 commented on Oct 22, 2023

    @munish0838

    Hi, is this issue fixed or needs contribution yet.
    Would like to contribute

  12. JoanFM commented on Oct 22, 2023

    @JoanFM
    Member

    Hey @munish0838, there is still work to be done

  13. munish0838 commented on Oct 24, 2023

    @munish0838

    I would like to start working on this issue

  14. JoanFM commented on Oct 24, 2023

    @JoanFM
    Member

    be my guest. I believe there are parts of the plan that were not applied yet.

  15. Kulraj69 commented on Oct 3, 2025

    @Kulraj69

    Hi Joan, Is this issue still open? I would like to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/document-indexConcerning Document Index or a Document Index backendgood-first-issueSuitable as your first contribution to DocArray!

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions