How to return metadata with query results?

I’ve been able to add metadata to the PDFs I upload to a Vectara corpus. The attached image shows one of the pieces of metadata. When I make a query to the corpus from my application I only get one piece of metadata returned with the chunks:

    "section": "296"

How can I return the metadata that I’ve added to each document?

What you’re probably getting tripped up on is that there are 2 types of metadata: document-level metadata and section-level metadata. Seeing just { "section": "296" } is showing you that there’s no section-level metadata other than the section number. But each section returned should also have a documentIndex JSON attribute which will point you to the index of the document array which may contain document-level metadata. So if you want to get the document-level metadata for a specific section, you should use some logic like

documentIndex = responseSet[0]['response'][i]['documentIndex']
documentMetadata = responseSet[0]['document'][documentIndex]['metadata']

There’s an example in the docs at Reading Metadata | Vectara Docs.

This format is because multiple sections that answer the user’s question could come back from the same document, so it’s possible to get >1 hit (in the form of > 1 sections) for the same document.

1 Like

Thank you, @shane! That’s the information I was looking for.