简体   繁体   English

索引neo4j中的现有关系

[英]Index existing relationships in neo4j

Is it possible to add index on existing relationships. 是否可以在现有关系上添加索引。 More specifically I have a relationship called Relatadness with one property called score(score is double) and I want to index it (with java or through the web client). 更具体地说,我有一个名为Relatadness的关系,该关系具有一个称为score(得分为double)的属性,我想对其进行索引(使用Java或通过Web客户端)。 How can do that? 那怎么办 thanks in advance 提前致谢

Since you already have the relationships, you'll have to iterate through all of them. 由于已经有了关系,因此必须遍历所有关系。 This code example will create an index called RelatadnessIndex , and store the relationship in the index under a key of score : 此代码示例将创建一个称为RelatadnessIndex的索引,并将该关系存储在index的score

    GlobalGraphOperations ggo = GlobalGraphOperations.at(db);
    Index<Relationship> relatadnessIndex = db.index().forRelationships("RelatadnessIndex");
    for (Relationship r : ggo.getAllRelationships()) {
        if (r.getType().name().equals("Relatadness")) {
            double score = (double) r.getProperty("score");
            relatadnessIndex.add(r, "score", score);
        }
    }

Note that by default Neo4j/Lucene will index the value as a String, so doing numeric range searches won't work. 请注意,默认情况下,Neo4j / Lucene会将值索引为字符串,因此无法进行数字范围搜索。 If you want to have it stored as a Numeric, you'll need to change to add to be this: 如果要将其存储为数字,则需要更改以添加为:

relatadnessIndex.add(r, "score", new ValueContext( score ).indexNumeric() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM