属性和标签上的索引仅适用于 Cypher 中 WHERE 子句中的相等比较:http://docs.neo4j.org/chunked/stable/query-schema-index.html#schema-index-use-index-with-where
要使用 year_month 属性上的索引,您必须指定精确匹配以优化查找。
WITH 2014 as year, 5 as month
WITH ((year * 12) + month) as year_month
MATCH (p:Person { year_month: year_month })
RETURN p
以下是一些针对 year_month 上的范围查询和完全匹配查询优化 Cypher 查询的提示。
将您的年份和月份转换为整数,而不是浮点值。
转换为:monthIndex = (year * 12) + month
转换自:year = (monthIndex / 12), month = (monthIndex % 12)
使用属性month 和year 的复合索引创建节点为year_month:
WITH 2014 as year, 5 as month
CREATE (michael:Person {
name: "Michael",
year_month: ((year * 12) + month),
year: year,
month: month })
在标签Person 的year_month 属性上创建索引:
CREATE INDEX ON :Person(year_month)
这种方法允许您按范围查询。
通过5/2013和5/2014之间的范围查询节点:
WITH 2013 as year_from, 5 as month_from,
2014 as year_to, 5 as month_to
WITH ((year_from * 12) + month_from) as date_from,
((year_to * 12) + month_to) as date_to
MATCH (p:Person)
WHERE p.year_month >= date_from AND p.year_month <= date_to
RETURN p