Neo4j/Cypher:动态属性匹配 [英] Neo4j/Cypher: Match on dynamic property

查看:422
本文介绍了Neo4j/Cypher:动态属性匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据开发人员手册第3.2.4节参数":

According to the developer manual section 3.2.4 on Parameters:

参数不能用于以下构造,因为它们构成了> query结构的一部分,该结构已编译为查询计划:

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

  • 属性键;因此,MATCH(n)在哪里n.$ param ='something'无效(
  • property keys; so, MATCH (n) WHERE n.$param = 'something' is invalid (source)

有没有办法解决此问题(使用官方驱动程序使用C#)?我遇到的情况是,特定类型的节点可以包含从几到几百个属性,并且我希望用户能够搜索其中任何一个而不必事先编写所有可能的查询.理想情况下,我希望能够执行与

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

提供一个词典,其中prop: "a.Description"val: "Freeform item description"

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?

推荐答案

@cybersam的响应是获取结果的一种方法,但是正如InverseFalcon所说-您失去了索引性能-假设您打算对查询进行更多的设计不只是示例而已.

@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.

您不需要编写数百个查询(每个属性1个),这肯定是string.Format的工作吗?这样,您就可以解决字符串的concat问题,并且可以正确使用索引.

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.

类似的东西:

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

您将使用的方式:

string.Format(QueryFormat, "Description");

所以...

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

大概您已经提供了一个属性列表供用户选择,因此仅将其与参数一起传递就有意义. string.Format没有concat这样做的问题,因此从性能角度来看它会更好,因为从长远来看,它会更好,因为您可以在Neo4j上使用索引.

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.

这篇关于Neo4j/Cypher:动态属性匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆