Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:33+00:00 2026-05-26T21:09:33+00:00

I’ve this small snipped of code that load a file in an embedded Neo4j

  • 0

I’ve this small snipped of code that load a file in an embedded Neo4j Database.

With this code I’ve two problems and I don’t find documentation to solve it.

I’m following the samples of the documentation to create an index, but:
a) How can I detect if the index exists yet? The documentation explais that if the index already exists,
it’s returned, but in my case it returns an error.

b) When I get the a node from the index I get an error

from neo4j import GraphDatabase, INCOMING, Evaluation

# Create a database
db = GraphDatabase("c:/temp/graph")

with db.transaction:
    # Create an index for "users" nodes
    # to look for them using some of the properties  

    # HERE I GET AN ERROR WHEN THE INDEX EXISTS PREVIOUSLY, BUT THE DOCUMENTATION EXPLAINS THE OPOSITE.
    users_idx = db.node.indexes.create('users')

    # Create the "users_reference" node to connect all "users" nodes to here
    users_reference = db.node()
    db.reference_node.USERS(users_reference, provider='lucene', type='fulltext')

    '''
    Content of the file
    1,Marc
    2,Didac
    3,Sergi
    4,David
    '''

    f = open("../files/users","r")
    for line in f:
        v = line.split(",")
        i = v[0]
        name = v[1]

        # All write operations happen in a transaction
        user = db.node(id=i, name=name)
        user.INSTANCE_OF(users_reference)
        users_idx['id'][i] = user

# I suppose that here, the transaction is closed

# I want get the node whose property "Id" has value "3" 
# to print the property "name" of the node with id = 3

# HERE I GET AN ERROR WHEN THE THERE'RE MULTIPLE NODES WITH THE SAME VALUE FOR THE PROPERTY "ID"

c = users_idx['id']['3'].single
print c['name']                

'''
If the file has a duplicated ID, the previouly code returns an error... 
1,Marc
1,Marc_2
1,Marc_3
2,Didac
3,Sergi
4,David
'''    

# Always shut down your database when your application exits
db.shutdown()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:09:34+00:00Added an answer on May 26, 2026 at 9:09 pm

    In your first example, the documentation is wrong. There is currently only one way to determine if an index exists, and it is to check for a ValueError when getting an index. Like this:

    try:
        idx = db.node.indexes.get('my_index')
    except ValueError,e:
        idx = db.node.indexes.create('my_index')
    

    That should be changed to some more specific exception, since this pattern breaks if something else triggers a ValueError.. Will add an issue for that.

    I’ve just pushed an update to the documentation, and I’ve added an “exists” method, to check if an index exists. It will be available on Pypi after the next neo4j milestone release.

    if db.node.indexes.exists('my_index'):
        db.node.indexes.get('my_index')
    else:
        db.node.indexes.create('my_index')
    

    In your second example, I think this is correct behavior. The ‘single’ property assures that there is a single result. If you expect a single result, but get multiple, that is an error. If you want the first result, you should be able to do something like:

    hits = iter(users_idx['id']['3'])
    c = hits.next()
    hits.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I have this code to decode numeric html entities to the UTF8 equivalent character.
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.