How to query the graph when the intended property name is unknown?

余生颓废 提交于 2019-12-25 01:55:07

问题


A related question is here: How to query property value when property name is a parameter?

Let's say if I want to ask "Recommend me 500-dollar phones". And in my graph, I have:

create (p:Product {name:"iPhone 1", price:"500", color:"white"})
create (p:Product {name:"iPhone 2", price:"400", color:"white"})
create (p:Product {name:"iPhone 3", price:"800", color:"black"})

Based on the language "Recommend me 500-dollar phones", I know I should query on the Product node, but I don't know the 'price' property. I just want to match any product node with some property value = "500". In this case, how to achieve this in the query?

Match(p:Product) where p.x = 500

Here I don't know what "x" is and I don't have a property name for x to accept. Is this possible?

EDITED:

match (p:Product)
where any(key in keys(node) where key contains 'TEST')
RETURN p.key

How to return the 'p.key' value?


回答1:


It is not clear what your [EDITED] question is asking, so here are answers to 2 possible interpretations:

  1. If you know that the property key(s) of interest contain the string "TEST", and you want to get their distinct property values:

    MATCH (p:Product)
    WITH p, [k in KEYS(p) WHERE k CONTAINS 'TEST' | p[k]] AS values
    UNWIND values AS value
    RETURN DISTINCT value;
    
  2. If you know that the property value(s) of interest contain the string "TEST", and you want to get the distinct values:

    MATCH (p:Product)
    WITH p, [k in KEYS(p) WHERE p[k] CONTAINS 'TEST' | p[k]] AS values
    UNWIND values AS value
    RETURN DISTINCT value;
    

The UNWIND operation produces no rows if the list being unwound is empty.

Please note that these queries are expensive, since they have to scan every property of every Product node.




回答2:


Well, you can do an workaround using any() and keys() functions, but I think this is not recommended because you are iterating over all node properties and Neo4j will ignore all property indexes:

match (node:Product)
where any(key in keys(node) WHERE node[key] = "500")
RETURN node


来源:https://stackoverflow.com/questions/48913965/how-to-query-the-graph-when-the-intended-property-name-is-unknown

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