Data:
https://www.kaggle.com/datasets/hgultekin/bbcnewsarchive
Content Consists of 2225 documents from the BBC news website corresponding to stories in five topical areas from 2004-2005. Class Labels: 5 (business, entertainment, politics, sport, tech)
This project builds a local semantic search system using:
- Neo4j as the graph database
- Sentence Transformers for local embeddings
- Neo4j Vector Index for similarity search
The goal is to search BBC News articles by meaning, not only by exact keywords.
Example:
Instead of searching only for:
profit
you can ask:
Companies reporting financial growth
and retrieve articles related to revenue growth, advertising performance, and business success.
User Query
|
v
Sentence Transformer Model
(all-MiniLM-L6-v2)
|
v
Query Embedding
(384 dimensions)
|
v
Neo4j Vector Index
|
v
Similar Article Embeddings
|
v
Relevant Articles
Source:
BBC News Dataset
Columns:
| Column | Description |
|---|---|
| category | News category |
| filename | Original file name |
| title | Article title |
| content | Article text |
Example:
business | 001.txt | Ad sales boost Time Warner profit | Quarterly profits...
Properties:
Article
├── category
├── filename
├── title
├── content
└── embedding
embedding contains a 384-dimensional vector generated by the embedding model.
Properties:
Category
└── name
(Article)-[:BELONGS_TO]->(Category)
Created constraints:
CREATE CONSTRAINT article_identity IF NOT EXISTS
FOR (a:Article)
REQUIRE (a.category, a.filename) IS UNIQUE;CREATE CONSTRAINT category_name_unique IF NOT EXISTS
FOR (c:Category)
REQUIRE c.name IS UNIQUE;python3 -m venv venvActivate:
source venv/bin/activateInstall dependencies:
pip install sentence-transformers neo4j tqdmFile:
generate_embeddings.py
Purpose:
- Connect to Neo4j.
- Read all Article nodes.
- Combine:
title + content
- Generate embeddings using:
all-MiniLM-L6-v2
- Store vectors:
Article.embedding
Run:
python generate_embeddings.pyValidation:
MATCH (a:Article)
WHERE a.embedding IS NOT NULL
RETURN count(a);Check vector size:
MATCH (a:Article)
RETURN size(a.embedding)
LIMIT 1;Expected:
384
Create Neo4j vector index:
CREATE VECTOR INDEX article_embeddings IF NOT EXISTS
FOR (a:Article)
ON (a.embedding)
OPTIONS {
indexConfig: {
`vector.dimensions`: 384,
`vector.similarity_function`: 'cosine'
}
};File:
semantic_search.py
Purpose:
- Accept user question.
- Convert question into embedding.
- Search Neo4j vector index.
- Return similar articles.
Example:
Input:
Companies reporting financial growth
Output:
Title:
Ad sales boost Time Warner profit
Category:
business
Similarity Score:
0.82
Stored article:
TimeWarner profits increased because of advertising sales
gets converted into:
[0.12, -0.31, 0.44 ...]
User query:
Companies reporting financial growth
gets converted into:
[0.10, -0.29, 0.41 ...]
Neo4j compares the vectors using cosine similarity and returns the closest matches.
- Neo4j
- Python
- Sentence Transformers
- all-MiniLM-L6-v2
- Vector Search