Neo4j 查询结合来自 Elasticsearch 的数据以获取基于时间的图形
Neo4j query combining data from Elasticsearch for time based graph
Neo4j 和 Cypher 大师,
我正在使用 Neo4j、Elasticsearch 和 Spring Data Neo4j。我有彼此相关的实体节点。在关系上有一个计数字段,它是两个实体之间关系的总数。我正在使用以下 Cypher 恢复实体的前 50 个关系:
MATCH (e1:Entity)-[r1:RELATED_TO]-(e2:Entity)
WHERE e1.uuid = '<ENTITY_ID>'
RETURN e1,r1,e2
ORDER BY r1.count DESC
LIMIT 50
现在我想做的是通过返回上周(上个月等)的前 50 个关系来可视化实体的基于时间的图表。我没有在 Neo4j 中存储时间序列数据,只存储关系的总数。时间序列数据存储在 Elasticsearch 索引中,格式如下。
Date, entityOrRelationshipId, startId, endId, type
每次更新关系时,都会在索引中插入一行,其中包含日期时间、关系 ID 和实体 ID。
可以使用以下 Elasticsearch 查询搜索和聚合关系计数:
GET localhost:9200/trends/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"type": "RELATIONSHIP"
}
},
{
"range": {
"date": {
"gte": "2020-04-01T00:00:00.000+00:00",
"lt": "2020-04-28T00:00:00.000+00:00"
}
}
},
{
"bool": {
"should": [
{ "term": { "startId": "<ENTITY_ID>"} },
{ "term": { "endId": "<ENTITY_ID>" } }
]
}
}
]
}
},
"aggs": {
"my_rels": {
"terms": {
"field": "entityOrRelationshipId",
"size": 50
}
}
}
}
这会为特定日期范围内的每个关系 ID 生成以下结果,其中包含计数 (doc_count):
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2273,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"my_rels": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 145,
"buckets": [
{
"key": "2fa94be4-828b-4a20-b5f8-4965d5516149",
"doc_count": 303
},
{
"key": "74fb5f46-a6e8-41a8-bd11-cb374324b285",
"doc_count": 197
},
{
"key": "dc57fdcf-ea88-4808-9310-4e09d368e743",
"doc_count": 178
},
{
"key": "c4fbda1f-717e-4422-bc10-66ca6a6f39d7",
"doc_count": 79
},
etc.
]
}
}
}
使用 Neo4J APOC 库,我如何将 Elasticsearch 计数结果合并到我的 Cypher 查询中,而不必将计数值存储在 Neo4J 中?
如有任何帮助,我们将不胜感激。
假设:
RELATED_TO
关系有一个 uuid
属性 作为关系 ID,并且
Entity
id 和 "buckets" 列表作为 parameters entityId
和 buckets
传递给查询,这应该工作:
UNWIND $buckets AS b
MATCH (e1:Entity)-[r1:RELATED_TO]-(e2)
WHERE e1.uuid = $entityId AND r1.uuid = b.key
RETURN e1, r1, e2, b.doc_count AS count
ORDER BY count DESC
不需要 LIMIT
子句,因为结果行数将由 buckets
列表的大小决定。
Neo4j 和 Cypher 大师,
我正在使用 Neo4j、Elasticsearch 和 Spring Data Neo4j。我有彼此相关的实体节点。在关系上有一个计数字段,它是两个实体之间关系的总数。我正在使用以下 Cypher 恢复实体的前 50 个关系:
MATCH (e1:Entity)-[r1:RELATED_TO]-(e2:Entity)
WHERE e1.uuid = '<ENTITY_ID>'
RETURN e1,r1,e2
ORDER BY r1.count DESC
LIMIT 50
现在我想做的是通过返回上周(上个月等)的前 50 个关系来可视化实体的基于时间的图表。我没有在 Neo4j 中存储时间序列数据,只存储关系的总数。时间序列数据存储在 Elasticsearch 索引中,格式如下。
Date, entityOrRelationshipId, startId, endId, type
每次更新关系时,都会在索引中插入一行,其中包含日期时间、关系 ID 和实体 ID。
可以使用以下 Elasticsearch 查询搜索和聚合关系计数:
GET localhost:9200/trends/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"type": "RELATIONSHIP"
}
},
{
"range": {
"date": {
"gte": "2020-04-01T00:00:00.000+00:00",
"lt": "2020-04-28T00:00:00.000+00:00"
}
}
},
{
"bool": {
"should": [
{ "term": { "startId": "<ENTITY_ID>"} },
{ "term": { "endId": "<ENTITY_ID>" } }
]
}
}
]
}
},
"aggs": {
"my_rels": {
"terms": {
"field": "entityOrRelationshipId",
"size": 50
}
}
}
}
这会为特定日期范围内的每个关系 ID 生成以下结果,其中包含计数 (doc_count):
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2273,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"my_rels": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 145,
"buckets": [
{
"key": "2fa94be4-828b-4a20-b5f8-4965d5516149",
"doc_count": 303
},
{
"key": "74fb5f46-a6e8-41a8-bd11-cb374324b285",
"doc_count": 197
},
{
"key": "dc57fdcf-ea88-4808-9310-4e09d368e743",
"doc_count": 178
},
{
"key": "c4fbda1f-717e-4422-bc10-66ca6a6f39d7",
"doc_count": 79
},
etc.
]
}
}
}
使用 Neo4J APOC 库,我如何将 Elasticsearch 计数结果合并到我的 Cypher 查询中,而不必将计数值存储在 Neo4J 中?
如有任何帮助,我们将不胜感激。
假设:
RELATED_TO
关系有一个uuid
属性 作为关系 ID,并且Entity
id 和 "buckets" 列表作为 parametersentityId
和buckets
传递给查询,这应该工作:UNWIND $buckets AS b MATCH (e1:Entity)-[r1:RELATED_TO]-(e2) WHERE e1.uuid = $entityId AND r1.uuid = b.key RETURN e1, r1, e2, b.doc_count AS count ORDER BY count DESC
不需要 LIMIT
子句,因为结果行数将由 buckets
列表的大小决定。