|
| 1 | +# HackerNews Trending Topics Example |
| 2 | + |
| 3 | +[](https://github.com/cocoindex-io/cocoindex) |
| 4 | + |
| 5 | +In this example, we use [CocoIndex Custom Source](https://cocoindex.io/docs/custom_ops/custom_targets) to define a source to get HackerNews recent content by calling [HackerNews API](https://hn.algolia.com/api). |
| 6 | +We build an index for HackerNews threads and their comments, and use LLM to extract trending topics from the text. |
| 7 | + |
| 8 | +The pipeline uses `ExtractByLlm` to identify topics like product names, technologies, models, and company names mentioned in threads and comments, storing them in canonical form (avoiding acronyms unless very popular). |
| 9 | + |
| 10 | +We appreciate a star ⭐ at [CocoIndex Github](https://github.com/cocoindex-io/cocoindex) if this is helpful. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- **Custom Source Integration**: Fetches HackerNews threads and comments via API |
| 15 | +- **LLM Topic Extraction**: Automatically extracts topics using `ExtractByLlm` function |
| 16 | +- **Canonical Topic Forms**: Topics are stored in canonical form (e.g., "Large Language Model" instead of "LLM") |
| 17 | +- **Multiple Query Handlers**: |
| 18 | + - `search_by_topic`: Search content by specific topic |
| 19 | + - `get_trending_topics`: Get trending topics ranked by mention count |
| 20 | + |
| 21 | +## Steps |
| 22 | + |
| 23 | +### Indexing Flow |
| 24 | + |
| 25 | +1. We define a custom source connector `HackerNews` to get HackerNews recent threads by calling HackerNews API. |
| 26 | +2. For each thread and comment, we extract topics using LLM (`ExtractByLlm`). |
| 27 | +3. We build two indexes: |
| 28 | + - `hn_messages`: Full text of threads and comments |
| 29 | + - `hn_topics`: Extracted topics with references to their source content, keyed by (topic, message_id) |
| 30 | + |
| 31 | +## Prerequisite |
| 32 | + |
| 33 | +[Install Postgres](https://cocoindex.io/docs/getting_started/installation#-install-postgres) if you don't have one. |
| 34 | + |
| 35 | +## Run |
| 36 | + |
| 37 | +Install dependencies: |
| 38 | + |
| 39 | +```bash |
| 40 | +pip install -e . |
| 41 | +``` |
| 42 | + |
| 43 | +Update the target: |
| 44 | + |
| 45 | +```bash |
| 46 | +cocoindex update --setup main |
| 47 | +``` |
| 48 | + |
| 49 | +Each time when you run the `update` command, cocoindex will only re-process threads that have changed, and keep the target in sync with the recent 500 threads from HackerNews. |
| 50 | + |
| 51 | +You can also run `update` command in live mode, which will keep the target in sync with the source continuously: |
| 52 | + |
| 53 | +```bash |
| 54 | +cocoindex update --setup -L main.py |
| 55 | +``` |
| 56 | + |
| 57 | +## Query Examples |
| 58 | + |
| 59 | +After running the pipeline, you can query the extracted topics: |
| 60 | + |
| 61 | +```bash |
| 62 | +# Get trending topics |
| 63 | +cocoindex query main.py get_trending_topics --limit 20 |
| 64 | + |
| 65 | +# Search content by specific topic |
| 66 | +cocoindex query main.py search_by_topic --topic "Claude" |
| 67 | + |
| 68 | +# Search by text content |
| 69 | +cocoindex query main.py search_text --query "artificial intelligence" |
| 70 | +``` |
| 71 | + |
| 72 | +## CocoInsight |
| 73 | + |
| 74 | +I used CocoInsight (Free beta now) to troubleshoot the index generation and understand the data lineage of the pipeline. |
| 75 | +It just connects to your local CocoIndex server, with Zero pipeline data retention. Run following command to start CocoInsight: |
| 76 | + |
| 77 | +``` |
| 78 | +cocoindex server -ci -L main |
| 79 | +``` |
| 80 | + |
| 81 | +Then open the CocoInsight UI at [https://cocoindex.io/cocoinsight](https://cocoindex.io/cocoinsight). |
0 commit comments