LlamaIndexTS icon indicating copy to clipboard operation
LlamaIndexTS copied to clipboard

Get Node from Document Store with relationships

Open garviand opened this issue 9 months ago • 2 comments

Hi, I have the following code where I create an index from documents and then fetch a node.

const index = await VectorStoreIndex.fromDocuments(documents, {
  serviceContext: this.getServiceContext()
})
// some other stuff
const node = await index.docStore.getDocument('some-node-id', false);

The returned node doesn't have relationship information. Is this on purpose?

It seems like it is not being included when converting JSON to a TextNode in storage/docStore/utils.ts

export function jsonToDoc(docDict) {
    const docType = docDict[TYPE_KEY];
    const dataDict = JSON.parse(docDict[DATA_KEY]);
    let doc;
    if (docType === ObjectType.DOCUMENT) {
        doc = new Document({
            text: dataDict.text,
            id_: dataDict.id_,
            embedding: dataDict.embedding,
            hash: dataDict.hash,
            metadata: dataDict.metadata,
        });
    } else if (docType === ObjectType.TEXT) {
        doc = new TextNode({
            text: dataDict.text,
            id_: dataDict.id_,
            hash: dataDict.hash,
            metadata: dataDict.metadata,
            // relationships: dataDict.relationships, # not in existing code
        });
    } else {
        throw new Error(`Unknown doc type: ${docType}`);
    }
    return doc;
}

Curious if this is done for a specific reason, and if there are any recommended ways to fetch nodes from an index while retaining their relationships.

garviand avatar May 22 '24 19:05 garviand