如何在 Neo4j 密码中使用包含?

How to use contains within Neo4j cypher?

我最近收到了一个 Neo4j 数据库。阅读文档后,它似乎不是一个大来源。当前的 Neo4j 有 11 个节点和大约几十万条边。我不确定 Neo4j 的大小或属性是否会减慢处理速度。

由于查询非常大,我会 post 在问题结束时查询一次。

如果我使用 where 子句来包含目的,它会在 7-8 秒内给出结果。

MATCH (contact:Contacts)    
where lower(contact.Name) contains lower('Rick')          
WITH contact         
ORDER BY contact.Source asc         
SKIP 0 LIMIT 20

但是如果以下列方式使用相同的查询 returns 精确结果会在几毫秒内产生,但它仅 returns 完全匹配而不是所有包含 'Rick'.

MATCH (contact:Contacts{Name:'Rick'})          
WITH contact         
ORDER BY contact.Source asc         
SKIP 0 LIMIT 20

有没有办法用后一种方式使用contains,因为它似乎更快。

以下是使用的整个查询:

MATCH (contact:Contacts{Name:'Rick'})          
WITH contact         
ORDER BY contact.Source asc         
SKIP 0 LIMIT 20          
OPTIONAL MATCH (contact)-[workingFor:WorkingFor]->(company:Company)         
with contact, workingFor, company 
OPTIONAL MATCH (contact)-[contactForEmployee:ContactForEmployee]->(employee:Employee)        
with contact,workingFor, company, contactForEmployee, employee 
OPTIONAL MATCH (contact)-[InfoFor:InfoFor]-(LeadInfo:LeadInfo)          with contact,workingFor, company, contactForEmployee, employee, InfoFor, LeadInfo 
optional MATCH (contact)-[connectedTo:ConnectionDetails]-(contactTo:Contacts)       
where date( connectedTo.LinckedInConnectedOn) <> date('1900-01-01')       
WITH contact,connectedTo,  contactTo,  workingFor, company, contactForEmployee, employee ,InfoFor, LeadInfo       
ORDER BY connectedTo.LinckedInConnectedOn DESC  
WITH contact, collect(connectedTo)[..5] AS liConnections, collect(contactTo)[..5] AS liContacts, workingFor, company,         contactForEmployee, employee, InfoFor, LeadInfo 
optional MATCH (contact)-[ocConnections:ConnectionDetails]-(ocContactTo:Contacts)       
where ocConnections.EmailConnectionStrengthStrong <> 0 or ocConnections.EmailConnectionStrengthMedium <> 0 or ocConnections.EmailConnectionStrengthLow <> 0       
WITH contact,ocConnections, ocContactTo, liConnections, liContacts, workingFor, company,contactForEmployee, employee, InfoFor, LeadInfo       
ORDER BY ocConnections.EmailConnectionStrengthStrong desc,      ocConnections.EmailConnectionStrengthMedium desc,
 ocConnections.EmailConnectionStrengthLow desc  
WITH contact, collect(ocConnections)[..5] AS ocConnections, collect(ocContactTo)[..5] AS ocContactTo,        
 liConnections, liContacts,  workingFor, company, contactForEmployee, employee,InfoFor, LeadInfo 
RETURN contact, workingFor, company, contactForEmployee, employee,InfoFor, LeadInfo,              
 collect(liConnections) AS liConnections, collect(liContacts) AS liConnectedTo,             
 collect(ocConnections) as  ocConnections,  collect(ocContactTo) as ocConnectedTo

CONTAINS 适用于现有索引,除了您在节点 属性 上使用 toLower()

其中 lower(contact.Name) 包含 lower('Rick')

这会阻止使用 :Contacts(Name) 索引查找,因为规划器现在已将所有 :Contacts 节点的 Name 属性 转换为小写以执行检查。

要允许索引查找这样的查询,假设名称 属性 区分大小写,您可能需要添加一个额外的字段来保存名称的小写形式,并且您可以 运行 无需对名称 属性.

使用 lower() 函数的查询

或者,如果您可以升级到 Neo4j 3。5.x,我们现在有一个 fulltext schema indexes 专为此类搜索而设计,并且对查找不区分大小写。