Neo4j/Cypher: Match on dynamic property

无人久伴 提交于 2019-12-25 08:38:17

问题


According to the developer manual section 3.2.4 on Parameters:

Parameters cannot be used for the following constructs, as these form part of the >query structure that is compiled into a query plan:

  • property keys; so, MATCH (n) WHERE n.$param = 'something' is invalid (source)

Is there no way around this (c#, using the official driver)? I have a case where a node of a particular type can contain from a few up to hundreds of properties, and I want the users to be able to search for any of these without having to write all possible queries in advance. Ideally I'd like to be able to do something similar to

MATCH (a) WHERE ({prop} CONTAINS {val}) RETURN a

supplying a dictionary where prop: "a.Description" and val: "Freeform item description"

Is this at all possible, or is string concatenation (with related drawbacks) the only possibility here?


回答1:


@cybersam's response is one way of getting the result, but as InverseFalcon said - you lose indexing performance - assuming you're planning on making the query a bit more detailed than just the example.

You don't need to write hundreds of queries (1 for each property), surely this is just a job for string.Format? That way you get around the concat issue of string, and you can use indexing properly.

Something like:

const string QueryFormat = "MATCH (a) WHERE (a.{0} CONTAINS {{val}})";

which you'd use like:

string.Format(QueryFormat, "Description");

So...

session.Run(string.Format(QueryFormat, "Description"), /*params object here*/);

Presumably you've already provided a list of properties for a user to select from, so it makes sense just to pass that in with the parameters. string.Format doesn't have the issues that concat does so performance wise it'll be better in the long run as you can use the indexing on Neo4j when you get to it.




回答2:


The syntax, n['foo'], can be used to reference n.foo.

So, this should work (I use the now-preferred $foo syntax instead of {foo} for parameters):

MATCH (a) WHERE a[$propName] CONTAINS $val
RETURN a;

Where propName: "Description" and val: "Freeform item description"



来源:https://stackoverflow.com/questions/44422028/neo4j-cypher-match-on-dynamic-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!