2
votes

Here my problem. I insert in my Neo4j lists of persons (with unique identifier) and create relationship between them.

What is the most efficient way to know if the person already exists in Neo4j and then create/update it? Since Neo4jclient doesn't support labels, is it more efficient to store the type information as an attribute of the node, or it's better to link all the nodes of the same type to a "root node" of this type?

Thank you in advance,

Emeric

2

2 Answers

1
votes

Neo4jClient does support labels, via standard Cypher. (Build 1.0.0.602 and up.)

graphClient
    .Merge("(p:Person {jim}")
    .WithParam("jim", new Person { Name = "Jim" })
    .ExecuteWithoutResults();

You can also return it:

var jimNode = graphClient
    .Merge("(p:Person {jim}")
    .WithParam("jim", new Person { Name = "Jim" })
    .Return(p => p.Node<Person>())
    .Results
    .Single();
0
votes

You could add to an index and do an index query, so:

Create Index:

if (!client.CheckIndexExists("Persons", IndexFor.Node))
    client.CreateIndex("Persons", new IndexConfiguration {Provider = IndexProvider.lucene, Type = IndexType.exact}, IndexFor.Node);

Add a person (with index entries)

var chris = new Person {Name = "Chris", Id = DateTime.Now.Ticks};
client.Create(chris, null, GetIndexEntries(chris));

Where GetIndexEntries looks like:

private static IEnumerable<IndexEntry> GetIndexEntries(Person person)
{
    var indexEntries = new List<IndexEntry>
    {
        new IndexEntry
        {
            Name = "Persons",
            KeyValues = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("name", person.Name),
                new KeyValuePair<string, object>("id", person.Id)
            }
        }
    };

    return indexEntries;
}

Then you query the index:

var indexQuery = 
    client.Cypher
    .Start(new {n = Node.ByIndexLookup("Persons", "name", "Chris")})
    .Return<Node<Person>>("n");
var results = indexQuery.Results.ToList();

Console.WriteLine("Found {0} results", results.Count());
foreach (var result in results)
    Console.WriteLine(result.Data.Id);