2

有没有办法使用数字比较运算符(>=、<=、<、>、...)在 Neo4j 中搜索手动索引?查看 Gremlin 索引示例,它们都倾向于呈现具有特定属性值的搜索。

假设我有大约 10M 的两种类型的关系,它们都在一个名为“property”的属性中具有数值,在第一个 double 中,在第二个 int 中。

gremlin> g.e(123).getProperty('property')
==> 1.57479276459179

现在,如果我知道确切的属性值,它是一个基于对数的双精度 pvalue,我可以很容易地找到节点

gremlin> g.idx('index_e_ASSOC').get('property', 1.57479276459179)
==> e[2421730][31493-ASSOCIATION->53378]
==> e[4885094][53378-ASSOCIATION->31493]
==> e[866409][37891-ASSOCIATION->6292]
==> e[123][6292-ASSOCIATION->37891]

相反,我想对'property'进行范围搜索,例如查找'property'> = 0 && 'property' <= 1.6的所有边。这可以用 Gremlin 做吗?看看Gremlin 用户讨论组告诉我,即使是从全文 Lucene 索引中进行通配符搜索也有点 hack,而且Neo4j API也无济于事。

编辑:在 Stackoverflow 中发现了另一个类似的问题(标题为“使用 Lucene 查询语法在 Neo4j 中进行范围查询”;新用户最多只能发布两个超链接)导致 Neo4j 文档。我通过对数值使用 ValueContext 重新创建了索引。通过遵循在 neo4j 讨论组中找到的示例(标题:将 numericRange 查询与关系查询结合起来),我可以进行如下查询

start a=node(123)
match a-[rel]-(b)
where type(rel) = "ASSOCIATION" AND rel.`property` > 1.0 AND rel.`property` < 2.0
RETURN b
LIMIT 20;

它使用范围搜索。Gremlin 的语法是什么?它应该是这样的

g.idx('index_e_ASSOC')[[property: Neo4jTokens.QUERY_HEADER + "[1.0 TO 2.0]"]].count()

这在语法上是正确的,但是即使在该范围内有具有属性的边,计数也会产生 0 结果。

4

2 回答 2

2

您可以在所有边缘上使用 Gremlin 过滤器步骤,但这会进行表扫描:

g.E.filter{it.property >= 0 && it.property <= 1.6}

https://github.com/tinkerpop/gremlin/wiki/Gremlin-Steps

如果索引index_e_ASSOC包含所有边的子集,则可以使用通配符查询来缩小范围:

start = g.idx('index_e_ASSOC')[['property': Neo4jTokens.QUERY_HEADER + "*"]]
start.filter{it.property >= 0 && it.property <= 1.6}

请注意,Neo4jTokens.QUERY_HEADER解析为,"%query%"因此您也可以这样编写:

start = g.idx('index_e_ASSOC')[['property': "%query%" + "*"]]
start.filter{it.property >= 0 && it.property <= 1.6}
于 2012-07-15T17:47:01.550 回答
0

可能你最好的选择是通过 groovy 使用 Neo4j API,就像http://docs.neo4j.org/chunked/snapshot/gremlin-plugin.html#rest-api-send-an-arbitrary-groovy-script-- -lucene 排序

于 2012-07-09T12:41:47.000 回答