Neo4j cypher内如何使用包含?

时间:2019-02-18 19:17:07

标签: neo4j graph-databases

我最近已移交给Neo4j数据库。阅读了文档之后,它似乎并不是一个很大的资源。当前的Neo4j有11个节点和大约几十万个边缘。我不确定Neo4j的大小或属性是否会减慢处理速度。

由于查询量很大,因此我将在问题末尾发布一次。

如果我将where子句用于包含目的,它将在7-8秒内给我结果。

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

但是,如果按照以下方式使用,则同一查询会在几毫秒内返回精确结果,但它只会返回精确匹配项,而不会返回所有包含“ 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

1 个答案:

答案 0 :(得分:2)

CONTAINS可与现有索引配合使用,除了在node属性上使用toLower()

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

这可以防止使用:Contacts(Name)索引查找,因为计划人员现在已将所有:Contacts节点的Name属性转换为小写字母以执行检查。

要允许对这样的查询进行索引查找,假设Name属性区分大小写,则可能需要添加一个附加字段来保存名称的小写形式,并且您可以运行查询而无需使用名称属性上的lower()函数。

或者,如果您可以升级到Neo4j 3.5.x,则我们现在有一个fulltext schema indexes,专门用于这类搜索,并且对查询不区分大小写。