问题描述
是否可以在现有关系上添加索引.更具体地说,我有一个名为Relatadness的关系,它具有一个名为score(得分为double)的属性,我想对其进行索引(使用Java或通过Web客户端).那怎么办预先感谢
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).How can do that?thanks in advance
推荐答案
由于已经具有关系,因此必须遍历所有关系.此代码示例将创建一个名为RelatadnessIndex
的索引,并将关系存储在score
键下的索引中:
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
:
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);
}
}
请注意,默认情况下,Neo4j/Lucene会将值索引为字符串,因此无法进行数字范围搜索.如果要将其存储为数字,则需要更改以添加为:
Note that by default Neo4j/Lucene will index the value as a String, so doing numeric range searches won't work. 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() );
这篇关于索引neo4j中的现有关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!