Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
[meilisearch] index community blog posts
The search sync only read the official blog listing (`allBlogs` of /api/blog).
Community posts come from their own paginated listing, /api/blog/community,
which never overlaps with the official one. Index both as title-only `blog`
documents, the product hub search renders as blog posts.

The community listing pages through live posts with an offset, so a post
published or removed mid-crawl shifts the pages: crawl again (up to 3 times)
when the pages don't add up to the announced total, instead of failing the run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
mishig25 and claude committed Oct 28, 2026
commit b6854568a5cfd372518caf95c2187d116aa44caf
90 changes: 53 additions & 37 deletions src/doc_builder/process_hf_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
HF_DATASET_API_URL = f"https://huggingface.co/api/datasets/{HF_DATASET_REPO}/tree/main"
HF_DATASET_BASE_URL = f"https://huggingface.co/datasets/{HF_DATASET_REPO}/resolve/main"
HF_BLOG_API_URL = "https://huggingface.co/api/blog"
HF_COMMUNITY_BLOG_API_URL = f"{HF_BLOG_API_URL}/community"
BLOG_LISTING_ATTEMPTS = 3
HF_BASE_URL = "https://huggingface.co"


Expand Down Expand Up @@ -379,44 +381,58 @@ def process_api_html_file(file_path: Path, library_name: str, base_dir: Path) ->
return chunks


def fetch_blog_listing(api_url: str, items_key: str) -> dict[str, str]:
"""Titles by URL of every post in a paginated blog listing."""
# The community listing pages through live posts with an offset, so a post published or removed mid-crawl shifts
# the pages: crawl again until the pages add up to the announced total
for _attempt in range(BLOG_LISTING_ATTEMPTS):
first_response = httpx.get(api_url, params={"p": 0}, timeout=60, follow_redirects=True)
first_response.raise_for_status()
first_page = first_response.json()

total_items = first_page["numTotalItems"]
items_per_page = first_page["numItemsPerPage"]
if not isinstance(total_items, int) or not isinstance(items_per_page, int) or items_per_page <= 0:
raise ValueError("Blog API returned invalid pagination metadata")

pages = [first_page]
for page_index in range(1, ceil(total_items / items_per_page)):
response = httpx.get(
api_url,
params={"p": page_index},
timeout=60,
follow_redirects=True,
)
response.raise_for_status()
pages.append(response.json())

blog_items = []
for page in pages:
page_items = page.get(items_key)
if not isinstance(page_items, list):
raise ValueError(f"Blog API response is missing {items_key}")
blog_items.extend(page_items)

unique_items = {}
for item in blog_items:
title = item.get("title")
blog_url = item.get("url")
if not isinstance(title, str) or not title.strip() or not isinstance(blog_url, str) or not blog_url:
raise ValueError("Blog API returned an item without a title or URL")
unique_items[blog_url] = title.strip()

if len(unique_items) == total_items:
return unique_items

raise ValueError(f"Blog API returned {len(unique_items)} unique {items_key} items, expected {total_items}")


def fetch_blog_chunks() -> list[Chunk]:
first_response = httpx.get(HF_BLOG_API_URL, params={"p": 0}, timeout=60, follow_redirects=True)
first_response.raise_for_status()
first_page = first_response.json()

total_items = first_page["numTotalItems"]
items_per_page = first_page["numItemsPerPage"]
if not isinstance(total_items, int) or not isinstance(items_per_page, int) or items_per_page <= 0:
raise ValueError("Blog API returned invalid pagination metadata")

pages = [first_page]
for page_index in range(1, ceil(total_items / items_per_page)):
response = httpx.get(
HF_BLOG_API_URL,
params={"p": page_index},
timeout=60,
follow_redirects=True,
)
response.raise_for_status()
pages.append(response.json())

blog_items = []
for page in pages:
all_blogs = page.get("allBlogs")
if not isinstance(all_blogs, list):
raise ValueError("Blog API response is missing allBlogs")
blog_items.extend(all_blogs)

unique_items = {}
for item in blog_items:
title = item.get("title")
blog_url = item.get("url")
if not isinstance(title, str) or not title.strip() or not isinstance(blog_url, str) or not blog_url:
raise ValueError("Blog API returned an item without a title or URL")
unique_items[blog_url] = title.strip()

if len(unique_items) != total_items:
raise ValueError(f"Blog API returned {len(unique_items)} unique allBlogs items, expected {total_items}")
# Official posts and community posts are disjoint listings, both served under /blog
unique_items = {
**fetch_blog_listing(HF_BLOG_API_URL, "allBlogs"),
**fetch_blog_listing(HF_COMMUNITY_BLOG_API_URL, "posts"),
}

return [
Chunk(
Expand Down
58 changes: 50 additions & 8 deletions tests/test_search_ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
from doc_builder.commands import embeddings as embeddings_command
from doc_builder.process_hf_docs import (
HF_BLOG_API_URL,
HF_COMMUNITY_BLOG_API_URL,
fetch_blog_chunks,
fetch_blog_listing,
markdown_file_to_url,
process_all_libraries,
process_api_html_file,
Expand Down Expand Up @@ -180,9 +182,9 @@ def json(self):
return self.payload


def test_fetch_blog_chunks_paginates_and_uses_only_all_blogs(monkeypatch):
def test_fetch_blog_chunks_paginates_official_and_community_listings(monkeypatch):
pages = {
0: {
(HF_BLOG_API_URL, 0): {
"numTotalItems": 3,
"numItemsPerPage": 2,
"allBlogs": [
Expand All @@ -198,18 +200,26 @@ def test_fetch_blog_chunks_paginates_and_uses_only_all_blogs(monkeypatch):
"canonical": False,
},
],
"communityBlogPosts": [{"title": "Ignored community post", "url": "/blog/user/ignored"}],
"communityBlogPosts": [{"title": "Sidebar community post", "url": "/blog/user/sidebar"}],
},
1: {
(HF_BLOG_API_URL, 1): {
"allBlogs": [{"title": "Last post", "url": "/blog/last-post"}],
"communityBlogPosts": [{"title": "Also ignored", "url": "/blog/user/also-ignored"}],
"communityBlogPosts": [{"title": "Sidebar community post", "url": "/blog/user/sidebar"}],
},
(HF_COMMUNITY_BLOG_API_URL, 0): {
"numTotalItems": 2,
"numItemsPerPage": 1,
"posts": [{"title": "Community post", "url": "/blog/user/community-post", "canonical": False}],
},
(HF_COMMUNITY_BLOG_API_URL, 1): {
"posts": [{"title": "Older community post", "url": "/blog/other-user/older"}],
},
}
calls = []

def fake_get(url, *, params, timeout, follow_redirects):
calls.append((url, params, timeout, follow_redirects))
return FakeResponse(pages[params["p"]])
return FakeResponse(pages[url, params["p"]])

monkeypatch.setattr("doc_builder.process_hf_docs.httpx.get", fake_get)

Expand All @@ -218,20 +228,52 @@ def fake_get(url, *, params, timeout, follow_redirects):
assert calls == [
(HF_BLOG_API_URL, {"p": 0}, 60, True),
(HF_BLOG_API_URL, {"p": 1}, 60, True),
(HF_COMMUNITY_BLOG_API_URL, {"p": 0}, 60, True),
(HF_COMMUNITY_BLOG_API_URL, {"p": 1}, 60, True),
]
assert [chunk.text for chunk in chunks] == [
"First post",
"Namespaced post",
"Last post",
"Community post",
"Older community post",
]
assert [chunk.text for chunk in chunks] == ["First post", "Namespaced post", "Last post"]
assert [chunk.source_page_url for chunk in chunks] == [
"https://huggingface.co/blog/first-post",
"https://huggingface.co/blog/acme/namespaced-post",
"https://huggingface.co/blog/last-post",
"https://huggingface.co/blog/user/community-post",
"https://huggingface.co/blog/other-user/older",
]
assert all(chunk.package_name == "blog" for chunk in chunks)
assert all(chunk.text == chunk.source_page_title for chunk in chunks)
assert all(chunk.headings == [f"# {chunk.text}"] for chunk in chunks)
assert "Ignored community post" not in {chunk.text for chunk in chunks}
# The listing's sidebar only previews community posts, which come from the community listing
assert "Sidebar community post" not in {chunk.text for chunk in chunks}
assert "This body must not be indexed." not in {chunk.text for chunk in chunks}


def test_fetch_blog_listing_crawls_again_when_pages_shift(monkeypatch):
newest = {"title": "Newest", "url": "/blog/user/newest"}
oldest = {"title": "Oldest", "url": "/blog/user/oldest"}
# The post between them is removed after the first page is fetched, so "Oldest" moves up past the crawl
responses = iter(
[
{"numTotalItems": 3, "numItemsPerPage": 1, "posts": [newest]},
{"posts": [oldest]},
{"posts": []},
{"numTotalItems": 2, "numItemsPerPage": 1, "posts": [newest]},
{"posts": [oldest]},
]
)
monkeypatch.setattr("doc_builder.process_hf_docs.httpx.get", lambda *args, **kwargs: FakeResponse(next(responses)))

assert fetch_blog_listing(HF_COMMUNITY_BLOG_API_URL, "posts") == {
"/blog/user/newest": "Newest",
"/blog/user/oldest": "Oldest",
}


def test_fetch_blog_chunks_rejects_incomplete_results(monkeypatch):
response = FakeResponse(
{
Expand Down
Loading