hn-search

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Hacker News Search

Hacker News 搜索

Search and monitor Hacker News stories, comments, and users via the Algolia HN Search API. No API key required.
通过Algolia HN Search API搜索和监控Hacker News的帖子、评论和用户。无需API密钥。

When to Use

使用场景

  • User asks about Hacker News discussions on a topic
  • User wants to find HN posts about a company, product, or technology
  • User wants to monitor HN for mentions of something
  • User asks "what's trending on HN" or "what did HN think about X"
  • User wants to find Show HN / Ask HN / Launch HN posts
  • 用户询问特定主题的Hacker News讨论内容
  • 用户想要查找关于某家公司、产品或技术的HN帖子
  • 用户想要监控HN上对某事物的提及
  • 用户询问“HN上现在有什么热门内容”或“HN对X的评价如何”
  • 用户想要查找Show HN / Ask HN / Launch HN类型的帖子

API Overview

API概述

Base URL:
https://hn.algolia.com/api/v1
Two search endpoints:
  • /search
    — Relevance-sorted (best for finding specific topics)
  • /search_by_date
    — Date-sorted (best for monitoring / recent activity)
Rate Limits: 10,000 requests/hour (generous, no auth needed)
基础URL:
https://hn.algolia.com/api/v1
两个搜索端点:
  • /search
    — 按相关性排序(最适合查找特定主题)
  • /search_by_date
    — 按日期排序(最适合监控/查看近期动态)
请求限制: 每小时10000次请求(额度充足,无需身份验证)

How to Search

搜索方法

Step 1: Build the URL

步骤1:构建URL

https://hn.algolia.com/api/v1/search?query=QUERY&tags=TAG&hitsPerPage=N&numericFilters=FILTERS
https://hn.algolia.com/api/v1/search?query=QUERY&tags=TAG&hitsPerPage=N&numericFilters=FILTERS

Step 2: Fetch with
web_fetch

步骤2:使用
web_fetch
发起请求

Use
web_fetch
to call the API. Response is JSON.
使用
web_fetch
调用API,返回结果为JSON格式。

Parameters

参数说明

ParameterDescriptionExample
query
Search terms (URL-encoded)
query=openai+gpt
tags
Filter by type (see below)
tags=story
hitsPerPage
Results per page (max 1000)
hitsPerPage=20
page
Page number (0-indexed)
page=0
numericFilters
Numeric filters (see below)
numericFilters=points>100
参数说明示例
query
搜索关键词(需URL编码)
query=openai+gpt
tags
按类型过滤(见下文)
tags=story
hitsPerPage
每页结果数量(最大1000)
hitsPerPage=20
page
页码(从0开始)
page=0
numericFilters
数值过滤条件(见下文)
numericFilters=points>100

Tag Filters

标签过滤规则

Use
tags
to filter by content type:
TagDescription
story
Stories only
comment
Comments only
show_hn
Show HN posts
ask_hn
Ask HN posts
front_page
Currently on front page
author_USERNAME
Posts by a specific user
story_ID
Comments on a specific story
Combine tags with commas (AND) or parentheses with commas (OR):
  • tags=story,show_hn
    → Show HN stories (AND)
  • tags=(story,comment)
    → Stories OR comments
使用
tags
参数按内容类型过滤:
标签说明
story
仅显示帖子
comment
仅显示评论
show_hn
Show HN类型的帖子
ask_hn
Ask HN类型的帖子
front_page
当前首页的内容
author_USERNAME
特定用户发布的内容
story_ID
特定帖子下的评论
组合标签时使用逗号(逻辑与)或带逗号的括号(逻辑或):
  • tags=story,show_hn
    → 仅显示Show HN类型的帖子(逻辑与)
  • tags=(story,comment)
    → 显示帖子或评论(逻辑或)

Numeric Filters

数值过滤条件

FilterDescription
points>N
Minimum points/upvotes
num_comments>N
Minimum comments
created_at_i>TIMESTAMP
After Unix timestamp
created_at_i<TIMESTAMP
Before Unix timestamp
Combine with commas:
numericFilters=points>100,num_comments>50
过滤条件说明
points>N
最小点赞数
num_comments>N
最小评论数
created_at_i>TIMESTAMP
晚于指定Unix时间戳
created_at_i<TIMESTAMP
早于指定Unix时间戳
使用逗号组合多个条件:
numericFilters=points>100,num_comments>50

Date Ranges

日期范围搜索

To search within a time window, use Unix timestamps with
created_at_i
:
Calculate the current Unix timestamp first (e.g., via
exec: date +%s
), then subtract:
WindowSubtract from now
Last 24 hours
- 86400
Last 7 days
- 604800
Last 30 days
- 2592000
Example: if now is
1705312200
, last 7 days =
numericFilters=created_at_i>1704707400
要在特定时间窗口内搜索,可结合
created_at_i
使用Unix时间戳:
先计算当前Unix时间戳(例如通过
exec: date +%s
),再进行减法计算:
时间窗口从当前时间减去
过去24小时
- 86400
过去7天
- 604800
过去30天
- 2592000
示例:如果当前时间戳是
1705312200
,那么过去7天的条件为
numericFilters=created_at_i>1704707400

Response Format

响应格式

Each hit contains:
json
{
  "objectID": "12345",
  "title": "Story Title",
  "url": "https://example.com/article",
  "author": "username",
  "points": 150,
  "num_comments": 42,
  "created_at": "2024-01-15T10:30:00Z",
  "created_at_i": 1705312200,
  "story_text": "Text for Ask HN / Show HN (HTML)",
  "_tags": ["story", "author_username", "story_12345"]
}
For comments, hits also include:
json
{
  "comment_text": "The comment body (HTML)",
  "story_id": 12345,
  "story_title": "Parent Story Title",
  "story_url": "https://example.com",
  "parent_id": 12344
}
The response wrapper includes:
json
{
  "hits": [...],
  "nbHits": 1000,
  "page": 0,
  "nbPages": 50,
  "hitsPerPage": 20
}
每个搜索结果(hit)包含以下字段:
json
{
  "objectID": "12345",
  "title": "Story Title",
  "url": "https://example.com/article",
  "author": "username",
  "points": 150,
  "num_comments": 42,
  "created_at": "2024-01-15T10:30:00Z",
  "created_at_i": 1705312200,
  "story_text": "Text for Ask HN / Show HN (HTML)",
  "_tags": ["story", "author_username", "story_12345"]
}
对于评论结果,还会包含以下字段:
json
{
  "comment_text": "The comment body (HTML)",
  "story_id": 12345,
  "story_title": "Parent Story Title",
  "story_url": "https://example.com",
  "parent_id": 12344
}
响应包装器包含以下字段:
json
{
  "hits": [...],
  "nbHits": 1000,
  "page": 0,
  "nbPages": 50,
  "hitsPerPage": 20
}

Constructing HN Links

构建HN链接

  • Story:
    https://news.ycombinator.com/item?id={objectID}
  • Comment:
    https://news.ycombinator.com/item?id={objectID}
  • User:
    https://news.ycombinator.com/user?id={author}
  • 帖子链接:
    https://news.ycombinator.com/item?id={objectID}
  • 评论链接:
    https://news.ycombinator.com/item?id={objectID}
  • 用户主页链接:
    https://news.ycombinator.com/user?id={author}

Step-by-Step Instructions

分步操作指南

Searching for Stories on a Topic

搜索特定主题的帖子

  1. URL-encode the query
  2. Fetch:
    https://hn.algolia.com/api/v1/search?query=YOUR_QUERY&tags=story&hitsPerPage=10
  3. Parse the JSON response
  4. For each hit, present: title, points, num_comments, author, date, HN link, and original URL
  1. 对搜索关键词进行URL编码
  2. 发起请求:
    https://hn.algolia.com/api/v1/search?query=YOUR_QUERY&tags=story&hitsPerPage=10
  3. 解析JSON响应结果
  4. 对每个结果,展示:标题、点赞数、评论数、作者、发布日期、HN链接及原始文章链接

Finding Recent/Trending Discussions

查找近期/热门讨论

  1. Calculate Unix timestamp for your time window (e.g., 7 days ago)
  2. Fetch:
    https://hn.algolia.com/api/v1/search?query=YOUR_QUERY&tags=story&numericFilters=points>50,created_at_i>TIMESTAMP&hitsPerPage=10
  3. Sort results by points or comments for "trending"
  1. 计算目标时间窗口对应的Unix时间戳(例如7天前)
  2. 发起请求:
    https://hn.algolia.com/api/v1/search?query=YOUR_QUERY&tags=story&numericFilters=points>50,created_at_i>TIMESTAMP&hitsPerPage=10
  3. 按点赞数或评论数对结果排序,筛选“热门”内容

Getting Comments on a Story

获取某帖子下的评论

  1. Get the story's objectID from a search
  2. Fetch:
    https://hn.algolia.com/api/v1/search?tags=comment,story_STORYID&hitsPerPage=20
  3. Present comment_text, author, points for each
  1. 通过搜索获取目标帖子的objectID
  2. 发起请求:
    https://hn.algolia.com/api/v1/search?tags=comment,story_STORYID&hitsPerPage=20
  3. 展示每条评论的内容、作者及点赞数

Monitoring a Topic (Show Recent Mentions)

监控特定主题(查看近期提及内容)

  1. Use
    /search_by_date
    instead of
    /search
  2. Fetch:
    https://hn.algolia.com/api/v1/search_by_date?query=YOUR_QUERY&tags=(story,comment)&hitsPerPage=20
  3. Results are newest-first — useful for "what's new about X on HN"
  1. 使用
    /search_by_date
    端点替代
    /search
  2. 发起请求:
    https://hn.algolia.com/api/v1/search_by_date?query=YOUR_QUERY&tags=(story,comment)&hitsPerPage=20
  3. 结果按最新排序——适用于查看“HN上关于X的最新动态”

Finding a User's Posts

查找某用户的发布内容

  1. Fetch:
    https://hn.algolia.com/api/v1/search?tags=author_USERNAME,story&hitsPerPage=20
  2. For their comments:
    tags=author_USERNAME,comment
  1. 发起请求:
    https://hn.algolia.com/api/v1/search?tags=author_USERNAME,story&hitsPerPage=20
  2. 若要查找该用户的评论:使用
    tags=author_USERNAME,comment

Output Format

输出格式

Present results as a clean list:
undefined
将结果整理为清晰的列表:
undefined

HN Results for "query" (N total)

关键词“query”的HN搜索结果(共N条)

  1. Story Title (150 pts, 42 comments) By username · Jan 15, 2024 🔗 https://example.com/article 💬 https://news.ycombinator.com/item?id=12345
  2. ...

For comments:
  1. 帖子标题(150赞,42条评论) 作者:username · 2024年1月15日 🔗 https://example.com/article 💬 https://news.ycombinator.com/item?id=12345
  2. ...

评论结果格式:

HN Comments on "Story Title"

帖子“Story Title”的HN评论

  1. username (12 pts) · Jan 15, 2024
    First ~200 chars of the comment text... 💬 https://news.ycombinator.com/item?id=12345
undefined
  1. username(12赞)· 2024年1月15日
    评论内容前200字符... 💬 https://news.ycombinator.com/item?id=12345
undefined

Error Handling

错误处理

  • Empty results: Tell the user no results were found. Suggest broadening the query or removing filters.
  • API error / timeout: Retry once. If still failing, inform the user the HN search API may be temporarily down.
  • Rate limited (429): Unlikely at 10k/hr, but if hit, wait 60 seconds and retry.
  • Malformed response: Check the URL construction — common issues are unencoded special characters in the query.
  • 无结果: 告知用户未找到结果,建议放宽搜索条件或移除过滤规则。
  • API错误/超时: 重试一次。若仍失败,告知用户HN搜索API可能暂时无法访问。
  • 请求受限(429状态码): 每小时10000次的额度很难触发,但如果遇到,等待60秒后重试。
  • 响应格式异常: 检查URL构建是否正确——常见问题是搜索关键词中的特殊字符未编码。

Examples

使用示例

Example 1: "What's HN saying about Rust?"

示例1:“HN上关于Rust的讨论有哪些?”

Fetch: https://hn.algolia.com/api/v1/search?query=rust+programming&tags=story&hitsPerPage=5&numericFilters=points>50
Fetch: https://hn.algolia.com/api/v1/search?query=rust+programming&tags=story&hitsPerPage=5&numericFilters=points>50

Example 2: "Find Show HN posts about AI agents from the last month"

示例2:“查找过去一个月内关于AI Agent的Show HN帖子”

undefined
undefined

Calculate timestamp for 30 days ago, then:

计算30天前的时间戳,然后发起请求:

Example 3: "What has pg posted recently?"

示例3:“pg最近发布了什么内容?”

Fetch: https://hn.algolia.com/api/v1/search_by_date?tags=author_pg&hitsPerPage=10
Fetch: https://hn.algolia.com/api/v1/search_by_date?tags=author_pg&hitsPerPage=10

Data Source

数据来源

Algolia HN Search API — Free, no authentication required. Indexes all public Hacker News content in near real-time.
Algolia HN Search API — 免费,无需身份验证。 近乎实时地索引所有公开的Hacker News内容。