2012-01-31 45 views
0

我遇到索引问题/使用SDN 2.0.0.RELEASE和Neo4j 1.5存在问题。persist()始终创建新节点

我有一个域类“字”基本上是这样的:

@NodeEntity 
public class Word { 

@GraphId 
Long graphId; 

@Indexed(indexType = IndexType.SIMPLE, indexName = "Word_wordString") 
private String wordString; 

@RelatedTo(direction = Direction.INCOMING, elementClass = Sentence.class, type = "HAS") 
private Set<Sentence> sentences; 

public Word() { 
} 

public Word(String wordString) { 
    setWordString(wordString); 
} 
} 

我坚持的话用这种方法:

private void persistWord(Sentence sentence, Word word) { 
System.out.println("checking index for wordString: " 
       + word.getWordString()); 
Word existingWord = wordRepository.findByPropertyValue(
       "wordString", word.getWordString()); 
if (existingWord != null) { 
    existingWord.addSentence(sentence); 
    existingWord.persist(); 
    System.out.println("persisted already existing word " 
        + existingWord); 
} else { 
    word.persist(); 
    System.out.println("persisted word " + word); 
} 

它应该检查这个词已经在如果是这样,我改变了由wordRepository返回的对象的一些属性,然后坚持更改( - > if(existingWord!= null))。如果该单词不在图中,它只是持久( - > else)。

但是,这总是为每个单词创建一个新节点,即使它存在于图表中。可以这么说,persist()总是创建一个新节点。 后有两个词具有相同wordString在图中,库方法抛出:

More than one element in [email protected] First element is 'Node[45]' and the second element is 'Node[83]' 

这是怎么回事?

我也想知道IndexType.SIMPLE,IndexType.POINT和IndexType.FULLTEXT之间的区别是什么。 (这不是API或良好关系的指南)

回答

0

好吧,我发现这个问题:

当我坚持我总是添加他们的话来的“字”设置当前句子,我将字的句子财产。我认为这就是为什么有些词语被多次坚持的原因。

1

托比亚斯,

你可以检查是否接受一个索引名作为第一个参数findByPropertyValue,并通过在你的“Word_wordString”指标会有所帮助。你的仓库也必须扩展NamedIndexRepository`。无论如何,存储库应该自动考虑您的自定义索引名称。

您也可以尝试不使用单独的索引名称,那么它将默认为简单的类名称,即Word,这将自动使用。

你使用的是什么类型的单词是字母数字字符或其他字符,如空格,换行符等?

+0

我已经让我的WordRepository扩展了GraphRepository和NamedIndexRepository并使用了'wordRepository.findByPropertyValue(“Word_wordString”,“wordString”,word.getWordString());'来检查现有的单词。 代码与我原来的问题相同,除了检查索引的行外。会发生什么是,一个单词首次被持续存在时,索引将如预期返回null,新单词将被保留。第二次,索引按预期返回一个对象实例。然而,第三次索引抛出了与我原来的问题相同的例外。 – Tobias 2012-02-07 22:08:38

+0

因此,我认为,坚持方法总是在索引中创建一个新节点和或条目!我会检查这些单词包含哪些字符,但为了测试目的,我们在初始设置时将它们全部修剪。 – Tobias 2012-02-07 22:12:49