知识库

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

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

自 Neo4j 3.4.x 起,模式索引(schema index)最适合对精确属性值进行索引,但不支持“模糊”(fuzzy)或全文搜索。然而,传统索引(legacy indexing)可以对这类查询进行优化,依赖自动索引或显式索引。在 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(*)
© . This site is unofficial and not affiliated with Neo4j, Inc.