Neo4j 中的全文搜索与向量搜索
在 Neo4j 中添加全文索引和向量索引,超越精确匹配查询,实现模糊文本搜索和语义相似性查询,从而扩展数据库以支持现代搜索和人工智能工作负载。
Neo4j 中的全文搜索与向量搜索 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Search Indexes Matter
Plain property lookups in Neo4j are great for exact matches, but real applications need more. Users misspell words, search across multiple fields, and increasingly expect semantic results.
Neo4j extends its capabilities with two specialized index types:
- Full-text indexes for fuzzy, multi-field text search
- Vector indexes for similarity search over embeddings
Both are first-class features you can manage with Cypher.
Creating a Full-Text Index
A full-text index is built over one or more node labels and properties. Once created, it powers tokenized, case-insensitive search.
The example creates an index named movieSearch over the title and plot properties of Movie nodes.
CREATE FULLTEXT INDEX movieSearch
FOR (m:Movie)
ON EACH [m.title, m.plot];Querying a Full-Text Index
You query full-text indexes with the db.index.fulltext.queryNodes procedure. It returns matching nodes plus a relevance score.
This Lucene-style syntax supports wildcards, fuzzy matching with ~, and boolean operators.
CALL db.index.fulltext.queryNodes('movieSearch', 'matrix~')
YIELD node, score
RETURN node.title AS title, score
ORDER BY score DESC;Fuzzy and Wildcard Matching
Full-text search shines with imperfect input. A few common operators:
star~— fuzzy match, tolerates typosstar*— prefix wildcardtitle:matrix— restrict to one fieldmatrix AND reloaded— boolean combination
These let one query handle the messy real-world queries users actually type.
CALL db.index.fulltext.queryNodes('movieSearch', 'title:matr*')
YIELD node, score
RETURN node.title, score;What Are Vector Embeddings?
A vector embedding is a list of numbers that captures the meaning of text, an image, or other data. Items with similar meaning have vectors that point in similar directions.
By storing an embedding as a property on a node, Neo4j can answer questions like find the documents most semantically similar to this one — not just keyword matches.
Creating a Vector Index
Vector indexes require you to declare the dimension (length of the embedding) and the similarity function (cosine or euclidean).
The db.index.vector.createNodeIndex procedure creates one over a label and property. Here we index a 1536-dimension embedding stored on Document nodes.
CALL db.index.vector.createNodeIndex(
'docEmbedding',
'Document',
'embedding',
1536,
'cosine'
);Storing an Embedding on a Node
Embeddings are usually produced by an external model and written back to Neo4j. The db.create.setNodeVectorProperty procedure stores the float array efficiently.
In practice the array has hundreds or thousands of values; it is shortened here for readability.
MATCH (d:Document {id: 'doc-1'})
CALL db.create.setNodeVectorProperty(d, 'embedding', [0.12, -0.04, 0.88])
RETURN d.id;Querying for Similar Nodes
To find the nearest neighbors, call db.index.vector.queryNodes with the index name, the number of results, and a query vector.
It returns nodes ordered by similarity along with a score between 0 and 1.
CALL db.index.vector.queryNodes('docEmbedding', 5, [0.10, -0.02, 0.90])
YIELD node, score
RETURN node.title AS title, score
ORDER BY score DESC;Combining Search with the Graph
The real power of Neo4j is mixing search with traversal. You can find semantically similar documents, then follow relationships to enrich the results.
This query finds similar documents and returns their authors — something a pure vector database cannot do in one step.
CALL db.index.vector.queryNodes('docEmbedding', 3, [0.1, -0.02, 0.9])
YIELD node, score
MATCH (node)<-[:WROTE]-(a:Author)
RETURN node.title, a.name, score;Managing Search Indexes
Like any index, full-text and vector indexes can be listed and dropped. Use SHOW INDEXES to inspect them and DROP INDEX to remove one.
Always check that an index is ONLINE before relying on it in production queries.
SHOW INDEXES
WHERE type IN ['FULLTEXT', 'VECTOR'];
// Remove one:
DROP INDEX docEmbedding IF EXISTS;Best Practices
To get the most from search indexes:
- Keep embedding dimensions consistent with your model output
- Choose
cosinesimilarity for most text embeddings - Re-embed and update vectors when source data changes
- Limit result counts and post-filter with Cypher for relevance
These habits keep searches fast and accurate as data grows.
Quick Check
Test your understanding of Neo4j search indexes.
Recap
You extended Neo4j with two powerful search capabilities:
- Full-text indexes — tokenized, fuzzy, multi-field keyword search via
db.index.fulltext.queryNodes - Vector indexes — semantic similarity over embeddings via
db.index.vector.queryNodes
Best of all, both integrate with graph traversals, letting you blend search relevance with relationship context in a single Cypher query.
常见问题解答
「Neo4j 中的全文搜索与向量搜索」课时是免费的吗?
是的 — 「Neo4j 中的全文搜索与向量搜索」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
「Neo4j 中的全文搜索与向量搜索」这节课中我会学到什么?
在 Neo4j 中添加全文索引和向量索引,超越精确匹配查询,实现模糊文本搜索和语义相似性查询,从而扩展数据库以支持现代搜索和人工智能工作负载。 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Neo4j Graph Database Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「Neo4j 中的全文搜索与向量搜索」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?
能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 存储过程与 UDF
- 与 BI 和可视化工具集成
- 高级数据摄取管道
- Neo4j 中的全文搜索与向量搜索