Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nebulagraph matching tag property #5743

Closed
Braingearceo opened this issue Oct 26, 2023 · 7 comments
Closed

nebulagraph matching tag property #5743

Braingearceo opened this issue Oct 26, 2023 · 7 comments

Comments

@Braingearceo
Copy link

I'am going into NebulaGraph and I have some trouble with querying, I may be stupid but I also don't get the documentation (either syntax or example, and I can't found a way to match my need)

SO. Admiting I got the following Vertice and Edge.

INSERT VERTEX Person(Name, Age, Sexe) VALUES "Alice_Watson":("Alice Watson", "32", "Female");
INSERT VERTEX Resource(Type, Support, Title, Author, Date_access) VALUES "Paper_on_Neural_Networks":("Article", "Online", "In-depth Analysis of Neural Networks", "Dr. Bob Green", "2023-09-22");
INSERT VERTEX Emotion(Name) VALUES "Curiosity":("Curiosity");
INSERT EDGE USES_RESOURCE VALUES "Alice_Watson"->"Paper_on_Neural_Networks":("Alice mentioned this paper during the discussion");
INSERT EDGE HAS_EMOTION VALUES "Alice_Watson"->"Curiosity":("Alice expressed curiosity about LLM");

Why does the following is empty :
MATCH (p:Person)-[r:USES_RESOURCE]->(res:Resource) WHERE id(p) CONTAINS "Alice" AND res.Title CONTAINS "Neural Networks" RETURN res;

Also,

Why does this work:
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE r.relationship CONTAINS "LLM" RETURN p;
But not:
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Name == "Curiosity" AND r.relationship CONTAINS "LLM" RETURN p;
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Name == "Curiosity" RETURN p;

Thank you very much in advance,
Cheers,
KL

@wey-gu
Copy link
Contributor

wey-gu commented Oct 27, 2023

Why does the following is empty :
MATCH (p:Person)-[r:USES_RESOURCE]->(res:Resource) WHERE id(p) CONTAINS "Alice" AND res.Title CONTAINS "Neural Networks" RETURN res;

This was due to the NebulaGraph flavor cypher requiring an explicit specific tag of vertex when expressing its prop filter, thus res.Resource.Title CONTAINS "Neural Networks" should be the expected expression.

Another hint here is, that CONTAINS will be quite expensive here as it underlying requires a full scan of data.

Why does this work:
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE r.relationship CONTAINS "LLM" RETURN p;
But not:
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Name == "Curiosity" AND r.relationship CONTAINS "LLM" RETURN p;
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Name == "Curiosity" RETURN p;

I think this is similar to the first question.

MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Emotion.Name == "Curiosity" AND r.relationship CONTAINS "LLM" RETURN p;
MATCH (p:Person)-[r:HAS_EMOTION]->(e:Emotion) WHERE e.Emotion.Name == "Curiosity" RETURN p;

@Braingearceo
Copy link
Author

Braingearceo commented Oct 27, 2023

Thank you for your answer. However, I can't make it to work.

For the first query :
Why does the following is empty :
MATCH (p:Person)-[r:USES_RESOURCE]->(res:Resource) WHERE id(p) CONTAINS "Alice" AND res.Title CONTAINS "Neural Networks" RETURN res;

This was due to the NebulaGraph flavor cypher requiring an explicit specific tag of vertex when expressing its prop filter, thus res.Resource.Title CONTAINS "Neural Networks" should be the expected expression.

I changed it to
MATCH (p:Person)-[r:USES_RESOURCE]->(res:Resource) WHERE id(p) CONTAINS "Alice" AND res.Title CONTAINS "Neural Networks" RETURN res;

But the result is the same, still empty table.

for the second query, it seems like Emotion.Name =="Curiosity" is necessary relied to a Full text index.

Also, for debugging sake I tried simpler query and got an interesting yet incomprehensible result.

INSERT EDGE IN_RELATIONSHIP VALUES "Alice_Watson"->"Colleague_relationship":("Alice is a colleague");

MATCH (p:Person)-[u:IN_RELATIONSHIP]->(r:Relationship) WHERE id(p) CONTAINS "Alice" RETURN r;
return
("Colleague_relationship" :Relationship{Categorie: "Colleague"})

but

INSERT EDGE USES_RESOURCE VALUES "Alice_Watson"->"Paper_on_Neural_Networks":("Alice mentioned this paper during the discussion");

MATCH (p:Person)-[u:USES_RESOURCE]->(r:Resource) WHERE id(p) CONTAINS "Alice" RETURN r;
return Empty.

I don't get it.

EDIT :
I can use CONTAINS and == With node's ID but not with Node property (like title or anything else) which sounds normal without indexing.
MATCH (p:Person)-[u:USES_RESOURCE]->(r:Resource) WHERE id(p) CONTAINS "Alice" RETURN r;
The problem appears to be related to the Resource ID containing underscores, which seem to cause errors.

@wey-gu
Copy link
Contributor

wey-gu commented Oct 27, 2023

I changed it to
MATCH (p:Person)-[r:USES_RESOURCE]->(res:Resource) WHERE id(p) CONTAINS "Alice" AND res.Title CONTAINS "Neural Networks" RETURN res;

We have to specify like res.Resource.Title CONTAINS...

(root@nebula) [basketballplayer]> 
MATCH (p:player)-[r:serve]->(res:team) WHERE p.player.name CONTAINS "Tim" AND res.team.name CONTAINS "s" 
+----------------------------------+
| res                              |
+----------------------------------+
| ("team204" :team{name: "Spurs"}) |
+----------------------------------+
Got 1 rows (time spent 7.826ms/59.028125ms)

@wey-gu
Copy link
Contributor

wey-gu commented Oct 27, 2023

but

INSERT EDGE USES_RESOURCE VALUES "Alice_Watson"->"Paper_on_Neural_Networks":("Alice mentioned this paper during the discussion");

MATCH (p:Person)-[u:USES_RESOURCE]->(r:Resource) WHERE id(p) CONTAINS "Alice" RETURN r;
return Empty.

I don't get it.


Aha, another pitfall, sorry for this!!
This was due to you creating dangling edges without creating their vertices! You must also insert vertices of the edges if you have not yet done so.

https://docs.nebula-graph.io/3.6.0/8.service-tuning/2.graph-modeling/#about_dangling_edges

@wey-gu
Copy link
Contributor

wey-gu commented Oct 27, 2023

EDIT :
I can use CONTAINS and == With node's ID but not with Node property (like title or anything else) which sounds normal without indexing.
MATCH (p:Person)-[u:USES_RESOURCE]->(r:Resource) WHERE id(p) CONTAINS "Alice" RETURN r;
The problem appears to be related to the Resource ID containing underscores, which seem to cause errors.


The most ideal graph query(the most graphy one) pattern would be to know the id of starting node(or edge) of the whole graph pattern( id(p) == "foo" or id(p) in ["foo", "bar"] ). And that's also the cheapest graph query regarding seeking the "starting point".

But when we need to seek starting vertex or edge of the pattern, similar things could be done like it was in tabular(sql) or elasticsearch(Inverted index).

In those cases, we need to explicitly create indexes to ensure guaranteed performance in a distributed graph system.

Tag/Edge indexes(towards properties) will enable data sorted based on those props and it's similar to indexes or tabular data in SQL database, it introduced extra write during data mutation but made it possible to find vertex/edge based on indexed props.

Similar things are fulltext index, where, indexed data will be sync to elasticsearch with the raft listener configured to enable more flexible search patterns brought by elasticsearch

@wey-gu
Copy link
Contributor

wey-gu commented Oct 27, 2023

And... big welcome to the NeublaGraph community KL!

@Braingearceo
Copy link
Author

OK got it now. So at the very moment i'am creating any TAG and EDGES, I should also create index for each property inside each TAG/EDGE to make them queryable from property content.

Thank you very much for your time. everything works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants