Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions app/backend/prepdocslib/searchmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,28 +471,33 @@ async def update_content(self, sections: list[Section], url: Optional[str] = Non

async with self.search_info.create_search_client() as search_client:
for batch_index, batch in enumerate(section_batches):
documents = [
{
documents = []
for section_index, section in enumerate(batch):
image_fields = {}
if self.search_images:
image_fields = {
"images": [
{
"url": image.url,
"description": image.description,
"boundingbox": image.bbox,
"embedding": image.embedding,
}
for image in section.chunk.images
]
}
document = {
"id": f"{section.content.filename_to_id()}-page-{section_index + batch_index * MAX_BATCH_SIZE}",
"content": section.chunk.text,
"category": section.category,
"sourcepage": BlobManager.sourcepage_from_file_page(
filename=section.content.filename(), page=section.chunk.page_num
),
"sourcefile": section.content.filename(),
"images": [
{
"url": image.url,
"description": image.description,
"boundingbox": image.bbox,
"embedding": image.embedding,
}
for image in section.chunk.images
],
**image_fields,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

**section.content.acls,
}
for section_index, section in enumerate(batch)
]
documents.append(document)
if url:
for document in documents:
document["storageUrl"] = url
Expand Down
Binary file removed data/en_An Occurrence at Owl Creek Bridge.pdf
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/test_searchmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,35 @@ async def mock_upload_documents(self, documents):
]


@pytest.mark.asyncio
async def test_update_content_no_images_when_disabled(monkeypatch, search_info):
"""Ensure no 'images' field is added when search_images is False (baseline case without any images)."""

documents_uploaded: list[dict] = []

async def mock_upload_documents(self, documents):
documents_uploaded.extend(documents)

monkeypatch.setattr(SearchClient, "upload_documents", mock_upload_documents)

manager = SearchManager(search_info, search_images=False)

test_io = io.BytesIO(b"test file")
test_io.name = "test/foo.pdf"
file = File(test_io)

section = Section(
chunk=Chunk(page_num=0, text="chunk text"),
content=file,
category="test",
)

await manager.update_content([section])

assert len(documents_uploaded) == 1, "Exactly one document should be uploaded"
assert "images" not in documents_uploaded[0], "'images' field should not be present when search_images is False"


class AsyncSearchResultsIterator:
def __init__(self, results):
self.results = results
Expand Down
Loading