gpt4 book ai didi

javascript - 如何使用 NodeJS 驱动程序在 Neo4j 中链接查询,同时将一些数据合并到新属性中?

转载 作者:行者123 更新时间:2023-12-03 04:36:37 26 4
gpt4 key购买 nike

我有一个 Javascript 对象格式的标记元素列表,我正在 NodeJS 项目中为其编写导入函数。列表中可能存在这些元素的重复项,因为该列表是来自不同源的列表的组合。

示例列表(这是 test.json 的内容):

[
//0 - first time this element appears on the list
{
name : "Name 1",
identifier : "string0001",
added_date : "1437013195",
tags : ["tag1", "tag2"]
},
//1 - same as 0 but the added_date is different and the name is different, an additional tag3 is present
{
name : "Name 2",
identifier : "string0001",
added_date : "1437082145",
tags : ["tag1", "tag3"]
},
//3 - a second unique element but it uses the same tags as 0
{
name : "Name 3",
identifier : "string0002",
added_date : "1358426363",
tags : ["tag1", "tag2"]
},
//4 - third unique element with a new tag tag4
{
name : "Name 4",
identifier : "string0003",
added_date : "1422912783",
tags : ["tag1", "tag4"]
},
// 5 - same element as 4, it was added before 4, it's tagged less than 4
{
name : "Name 4",
identifier : "string0003",
added_date : "1358426363",
tags : ["tag1"]
}
]

这里的唯一标识符是属性identifier,不关心名称不同。对于元素 01 的情况,我期望数据库中有一个 :Element Node 。

我将有两个 Node :

  • Element 包含nameidentifieradded_date
  • Tag 具有标签名称
  • 我的关系是:元素TAGGED_WITH 标签

到目前为止,我的可怕的小脚本创建了一个由 3 个查询组成的查询,首先创建 Element,然后创建 Tag,然后关联 ElementTag 并执行它。

我的脚本不执行的是:

  • MERGE 仅使用标识符的 Element,它使用所有三个属性 nameidentifier添加_日期
  • 它不会记录同一标识符是否具有多个名称(例如元素 01 有两个不同的名称,并且可能存储名称数组:{名称:“名称2”,标识符:“string0001”,added_date:“1437013195”,all_names:[“名称1”,“名称2”]})。这并不重要,当我知道如何正确链接查询时我会处理这个问题
  • 同样,added_date 属性也可以合并:{name: "Name 2",identifier:"string0001", added_date:"1437013195", all_names:["Name 1", "Name 2"], all_added_dates: ["1437013195","1437082145"]} 一旦我学会了正确的链接,我将再次解决这个问题。
  • 它不会捕获每个步骤中的错误

我的代码是:

var neo4j = require('neo4j-driver').v1;

// Create a driver instance, for the user neo4j with password neo4j.
// It should be enough to have a single driver per database per application.
var driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "123456"));

// Register a callback to know if driver creation was successful:
driver.onCompleted = function () {
// proceed with using the driver, it was successfully instantiated
console.log('successfully connected');
};

// Register a callback to know if driver creation failed.
// This could happen due to wrong credentials or database unavailability:
driver.onError = function (error) {
console.log('Driver instantiation failed', error);
};

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();
//console.log(session);

var test = require('./test.json');
for ( var element in test ) {

if (test.hasOwnProperty(element)) {
var obj = test[element];
var element_object = {name:'', identifier:'',add_date:''};
var tags;
for ( var prop in obj ) {
if (obj.hasOwnProperty(prop)) {
//console.log('obj.' + prop + ' = ' + obj[prop]);
if (prop === 'tags') {
tags = obj[prop];
} else {
element_object[prop] = obj[prop].replace(/["']/g, "\\\"");
}
}
}
console.log('gonna create this element', JSON.stringify(element_object));
console.log('tagged by', tags);

var q = 'MERGE (element:Element {identifier:"'+element_object.identifier+'", name:"'+element_object.name+'", add_date:"'+element_object.add_date+'"})\n';
var q2 = '';
var q3 = '';
for(var i=0; i<tags.length;i++){
q2+= 'MERGE(tag'+i+':Tag {name:"'+tags[i]+'"})\n';
q3+= 'MERGE(element)-[:TAGGED_WITH]->(tag'+i+')\n';
}
q += q2;
q += q3;
q += ";";



console.log('query:', q);


session
.run(q)
.then( function(result) {
console.log('added element:',result);
driver.close();
})
.catch( function(error) {
console.log(error);
// Close the driver when application exits
driver.close();
})

}
}

我想为查询运行编写的是与该算法相对应的一系列 promise :

  • 是否已存在带有标识符 string0001 的元素?
  • 如果没有创建它;如果是,则使用它(在这里我可能会编写这些增强功能来记录所有其他 added_datename 属性)。现在我有了对该元素的引用
  • 是否创建了名为 tag1 的标签?创建或返回标签。现在我有了对该标签
  • 的引用
  • 如果 elementtag 之间尚不存在关系,则建立关系。

预期结果:

如果结果采用 JavaScript 数组格式,则数据库中的结果可以可视化如下:

 [

{
name : "Name 2", //took the latest name on the list order, notice it is not Name 1 anymore
identifier : "string0001",
added_date : "1437082145", //took the latest added_date on the list order
tags : ["tag1", "tag2", "tag3"]
},
{
name : "Name 3",
identifier : "string0002",
added_date : "1358426363",
tags : ["tag1", "tag2"]
},
{
name : "Name 4",
identifier : "string0003",
added_date : "1358426363", //notice that the element 4 was added later than this element 5 but we took the
// older date because this was merged last. in other words it was the latest element
// with "string0003" in the list.
tags : ["tag1", "tag4"]
}
]

在我进行增强以支持所有名称和所有添加日期之后,它可能如下所示:

[
{
name : "Name 2", //took the newest name in terms of added_date
identifier : "string0001",
added_date : "1437082145", //took the greatest added_date
all_added_dates_and_names : [{'1437013195' : 'Name 1', '1437082145' : 'Name 2'}],
tags : ["tag1", "tag2", "tag3"]
},
{
name : "Name 3",
identifier : "string0002",
added_date : "1358426363",
tags : ["tag1", "tag2"]
},
{
name : "Name 4",
identifier : "string0003",
added_date : "1422912783", //took the greatest added_date
all_added_dates_and_names : [{'1422912783' : 'Name 4', '1358426363' : 'Name 4'}],
tags : ["tag1", "tag4"]
}

]

我一直在研究这些以找出最佳实践:

我的期望与 https://neo4j.com/developer/javascript/ 上的示例类似:

var neo4j = require('neo4j-driver').v1;

var driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();
session
.run( "CREATE (a:Person {name: {name}, title: {title}})", {name: "Arthur", title: "King"})
.then( function()
{
return session.run( "MATCH (a:Person) WHERE a.name = {name} RETURN a.name AS name, a.title AS title",
{name: "Arthur"})
})
.then( function( result ) {
console.log( result.records[0].get("title") + " " + result.records[0].get("name") );
session.close();
driver.close();
});

但我希望它能够捕获每个步骤中的错误,并添加想要在我的 for 循环中进行参数化的操作。

最佳答案

有一些复杂情况需要进行一些更改。

第一个是你的迭代方法。这对于 Cypher 来说通常性能不佳,并且也不建议使用字符串连接来构造查询。相反,我建议参数化您的输入集合,并在查询中使用 UNWIND 将集合展开为行,以便一次性处理整个 JSON。

第二个复杂因素是您的 all_add_dates_and_names 属性。 Neo4j 目前不允许 map 类型属性,也不允许 map 类型属性的集合。剩下的选项是将 map 转换为字符串,或者将每个添加的名称转换为具有附加日期属性的连接 Node 。

您还需要APOC Procedures为了尽可能轻松地构建查询,因为您需要使用集合联合函数。

这是一个应该可以工作的查询,尽管您需要替换 with ... as json引用您传入的 json 参数, unwind $json as row .

with [
{
name : "Name 1",
identifier : "string0001",
added_date : "1437013195",
tags : ["tag1", "tag2"]
},
{
name : "Name 2",
identifier : "string0001",
added_date : "1437082145",
tags : ["tag1", "tag3"]
},
{
name : "Name 3",
identifier : "string0002",
added_date : "1358426363",
tags : ["tag1", "tag2"]
},
{
name : "Name 4",
identifier : "string0003",
added_date : "1422912783",
tags : ["tag1", "tag4"]
},
{
name : "Name 4",
identifier : "string0003",
added_date : "1358426363",
tags : ["tag1"]
}
] as json

unwind json as row
with row.identifier as identifier, max(toInt(row.added_date)) as latestDate,
collect({date:toInt(row.added_date), name:row.name}) as allDatesAndNames, collect(row.tags) as allTags
// now union all collections of tags per entry with the same identifier
with identifier, latestDate, allDatesAndNames,
reduce(tagSet = head(allTags), tags in allTags | apoc.coll.union(tagSet, tags)) as allTags
// now get the latest name corresponding with latest date
with identifier, latestDate, allDatesAndNames, allTags,
head([entry in allDatesAndNames where entry.date = latestDate | entry.name]) as latestName
// data pre-processed, now start the merge
merge (el:Element{identifier:identifier})
set el.added_date = latestDate, el.name = latestName
foreach (entry in allDatesAndNames |
merge (el)-[:NAME_CHANGE]->(:NameChange{date:entry.date, name:entry.name}))
foreach (tagName in allTags |
merge (tag:Tag{name:tagName})
merge (el)-[:TAGGED_WITH]->(tag))

如果您需要考虑添加日期和名称,其中图表中的日期和名称比您添加的任何内容都更新,您可能只想合并 :NameChange Node (忽略处理查询的任何部分)包含latestDate 或latestName),然后在最后找到具有最新日期的 :NameChange Node ,并从该 Node 设置 date_added 和 name 属性。

关于javascript - 如何使用 NodeJS 驱动程序在 Neo4j 中链接查询,同时将一些数据合并到新属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com