如何在neo4j中读取节点的属性?

时间:2015-11-06 06:18:11

标签: neo4j

我是neo4j的新手,并构建 db ,其中包含> 10M 节点。在查询操作期间,我想通过使用它的两个properties来查找节点。例如:node - name: xxx surname: yyy id:1在查询操作期间我需要获取id的节点name: xxx, surname: yyy。如何使用java查询(不是密码)?并且将有多个具有给定属性的条目。

1 个答案:

答案 0 :(得分:1)

以下是如何查找ID的示例:

GraphDatabaseService database;

Label label = DynamicLabel.label("your_label_name");
String propertyId = "id";
String propertyName = "name";
String propertySurname = "surname";

public Set<Node> getIdsForPeople(Set<Person> people) {

    Set<String> ids = new HashSet<>();

    try(Transaction tx = database.beginTx()) {
        for (Person person in people) {
            Node node = database.findNode(label, propertyName, person.getName());

            if (node.hasProperty(propertySurname)) {
                if (node.getProperty(propertySurname) == person.getSurname()) {
                    String id = node.getProperty(propertyId).toString();

                    ids.add(id);
                }
            }
        }

        tx.success();
    }

    return ids;
}

持有人

public class Person {

    private final String name;
    private final String surname;

    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public String getName() { return name; }
    public String getSurname() { return surname; }
}

例如

Set<Person> people = new HashSet<Person>(){{
    add(new Person("xxx1", "yyy1"));
    add(new Person("xxx2", "yyy2"));
    add(new Person("xxx3", "yyy3");
    add(new Person("xxx4", "yyy4");
}};

Set<String> ids = getIdsForPeople(people);