cancel
Showing results for 
Search instead for 
Did you mean: 

Neo4j 4.3 relationship index on type(r)

766801122
Node Link

I create a index on relationship

CREATE LOOKUP INDEX rel_type_lookup_index FOR ()-[r]-() ON EACH type(r)

But this can't hit the relationship index

explain match (p:Person)-[r]->(m:Movie) where type(r) ='Knows' return p ;

How should I create an index?

1 ACCEPTED SOLUTION

I don't know where you did get this kind of model from, I definitely recommend to you to adjust your model to the recommended form otherwise you're fighting the system and won't be happy 🙂

Usually graph models are like this (Subject Predicate Object) on the schema-level:

(:Person)-[:WROTE|READ]->(:Book)

So they form a fact, which you then can query by the start- and end-labels and rel-types.

e.g.

MATCH (p:Person)-[:WROTE|READ]->(b:Book)
RETURN p, b

or

MATCH (p:Person) 
WHERE exists { (p)-[:WROTE|READ]->(:Book) }
RETURN p

View solution in original post

9 REPLIES 9

it only works when you use the literal rel-types

explain match (p:Person)-[r:Knows]->(m:Movie) return p ;

But how to get this hit to the relationship index? How should I create an index?I don't know the specific type of edge.

explain match (p:Person)-[r]->(m:Movie) where type(r) ends with 'nows' return p ;

The index is only per type. Not generally for all rels.

What does your data model look like?

A person has many relationships, and I created different names for these relationships. For example, person-[person_read_book]-book, person-[person_write_book]-book, I want to find out who read or wrote a book, person-[r]-[book] type(r) ends with book.

So I want to create index for relationships.

Why not instead check the label on the other node.

Where other:Book

Sorry, let me re-describe the scene. reader-[reader_read_book]-book, writer-[writer_write_book]-book, I want to know who has read or written a certain book, there are many different types, I don’t know which node types are there , I only know book. So I query (n)-[r]-(b:Book) where type(r) ends with 'book' return n

I don't know where you did get this kind of model from, I definitely recommend to you to adjust your model to the recommended form otherwise you're fighting the system and won't be happy 🙂

Usually graph models are like this (Subject Predicate Object) on the schema-level:

(:Person)-[:WROTE|READ]->(:Book)

So they form a fact, which you then can query by the start- and end-labels and rel-types.

e.g.

MATCH (p:Person)-[:WROTE|READ]->(b:Book)
RETURN p, b

or

MATCH (p:Person) 
WHERE exists { (p)-[:WROTE|READ]->(:Book) }
RETURN p

What you said makes sense, and thank you very much for your reply.