知识库

使用显式索引进行文本搜索

请注意,在 Neo4j 3.5 中,全文搜索作为 Cypher 存储过程的一部分在 Neo4j 中可用。更多文档请参见此处:https://neo4j.ac.cn/docs/cypher-manual/3.5/schema/index/#schema-index-fulltext-search

从 Neo4j 3.4.x 开始,模式索引最适合索引精确属性值,但不支持“模糊”或全文搜索。但是,旧版索引允许针对这些类型的查询进行优化,依赖于自动索引或显式索引。在 Neo4j 3.4 版本中,添加了对使用内置存储过程的显式索引的额外支持,以利用 Neo4j 中的文本搜索功能来搜索节点和/或关系。

以下是如何在节点属性上进行文本搜索的示例。

创建并添加节点到索引

match (n:Movie)
with n
call db.index.explicit.addNode("MovieTitle",n,"title",n.title) yield success return count(*)

通过运行以下命令验证索引是否已创建

call db.index.explicit.list

输出

╒══════╤════════════╤════════════════════════════════════╕
│"type"│"name"      │"config"                            │
╞══════╪════════════╪════════════════════════════════════╡
│"NODE"│"MovieTitle"│{"type":"exact","provider":"lucene"}│
└──────┴────────────┴────────────────────────────────────┘

运行查询以查找以 The 开头的电影名称

call db.index.explicit.searchNodes('MovieTitle','title:The*')

输出

╒════════════════════════════════════════════════════════════════════════════════════════════════════════════╤════════╕
│"node"                                                                                                      │"weight"│
╞════════════════════════════════════════════════════════════════════════════════════════════════════════════╪════════╡
│{"title":"The Matrix Revolutions","tagline":"Everything that has a beginning has an end","released":2003}   │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Da Vinci Code","tagline":"Break The Codes","released":2006}                                   │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Green Mile","tagline":"Walk a mile you'll never forget.","released":1999}                     │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Devil's Advocate","tagline":"Evil has its winning ways","released":1997}                      │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Polar Express","tagline":"This Holiday Season… Believe","released":2004}                      │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Replacements","tagline":"Pain heals, Chicks dig scars... Glory lasts forever","released":2000}│1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Matrix","tagline":"Welcome to the Real World","released":1999}                                │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Matrix Reloaded","tagline":"Free your mind","released":2003}                                  │1.0     │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│{"title":"The Birdcage","tagline":"Come as you are","released":1996}                                        │1.0     │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────┘

如果添加了与该索引关联的更多数据,它不会自动更新节点到索引。需要按如下方式将这些其他节点添加到索引中

match (n:Movie)
with n
call db.index.explicit.addNode("MovieTitle",n,"title",n.title) yield success return count(*)