Java 如何检查neo4j中节点属性的架构索引是否已经存在?

Java 如何检查neo4j中节点属性的架构索引是否已经存在?,java,neo4j,Java,Neo4j,我用它来填充neo4j中给定属性的索引 IndexDefinition indexDefinition = schema.indexFor(DynamicLabel.label("Person")).on("NodeType").create(); 问题是,当我用索引填充的类似代码再次执行该程序时,我得到以下异常 org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException: Already indexed :label[0](

我用它来填充neo4j中给定属性的索引

IndexDefinition indexDefinition = schema.indexFor(DynamicLabel.label("Person")).on("NodeType").create();
问题是,当我用索引填充的类似代码再次执行该程序时,我得到以下异常

 org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException: Already indexed :label[0](property[0]).
    at org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementContext.checkIndexExistence(DataIntegrityValidatingStatementContext.java:107)
    at org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementContext.indexCreate(DataIntegrityValidatingStatementContext.java:78)

我只想检查一个属性的索引是否已经存在,然后不应该进行后续索引填充。

为什么不在尝试创建标签并获得正确的异常之前检查标签的索引


在尝试创建标签并获得正确的异常之前,为什么不检查标签的索引


这是我在代码中使用的一个helper函数,所以我不会忘记将其包装在tx中

/**
 * Helper method to get indexes by Label, wrapped in a tx
 * 
 * @param label
 * @return one or more IndexDefinitions to iterate over, or null if there
 *         were none matching the label.
 */
public Iterable<IndexDefinition> getIndexByLabel(Label label) {
    Iterable<IndexDefinition> x = null;
    try (Transaction tx = graphDb.beginTx()) {
        x = graphDb.schema().getIndexes(label);
    }
    return x;
}

这是我在代码中使用的一个helper函数,所以我不会忘记将其包装在tx中

/**
 * Helper method to get indexes by Label, wrapped in a tx
 * 
 * @param label
 * @return one or more IndexDefinitions to iterate over, or null if there
 *         were none matching the label.
 */
public Iterable<IndexDefinition> getIndexByLabel(Label label) {
    Iterable<IndexDefinition> x = null;
    try (Transaction tx = graphDb.beginTx()) {
        x = graphDb.schema().getIndexes(label);
    }
    return x;
}

如果异常已经存在,请忽略它?这不是解决方案。为什么它不是解决方案?@fge poorprogramming@KarlMorrison不同意这就像人们在将id行插入数据库之前检查它是否存在:这是一种浪费;只要试着插入,如果它已经存在,那么,你可以通过返回的错误判断。如果它已经存在,就忽略异常?这不是解决方案。为什么它不是解决方案?@fge poorprogramming@KarlMorrison不同意这就像人们在将id行插入数据库之前检查它是否存在:这是一种浪费;只要尝试并插入,如果它已经存在,那么,您可以通过返回的错误来判断。Downvote。仅链接回答,链接将带您进入Neo4j的默认页面,查看断开的链接。Downvote。仅链接回答,链接将带您进入Neo4j的默认页面,查看断开的链接。Downvote。问题是关于属性上的索引,答案是关于label.Downvote上的索引。问题是关于属性上的索引,这个答案是关于标签上的索引。