<reponame>dmilcevski/dockerfiles<filename>kg-db-neo4j/neo4j_index_database.py
#!/usr/bin/env python
# encoding: utf-8

"""
Create indexes on neo4j graph database. The indexes are on wikidata instances and spatial indexes.

Usage:
  neo4j_index_database.py -h HOST_ADDRESS --neo4j-username=NEO4J_USER --neo4j-password=<PASSWORD>
  
Options:
  -h HOST_ADDRESS --host=HOST_ADDRESS                           Neo4j Server Host Address
  --neo4j-username=NEO4J_USER                           Neo4j Username
  --neo4j-password=<PASSWORD>                           Neo4j Password
"""
import sys
from docopt import docopt
from neo4j.v1 import GraphDatabase, basic_auth, ServiceUnavailable


def main(argv=None):
  print("Connect to server")
  global driver
  driver = GraphDatabase.driver(neo4j_uri, auth=basic_auth(neo4j_username, neo4j_password), connection_timeout=60)
  createWikidataIndex()
  createSpatialIndex()
  driver.close()
   
def createWikidataIndex():
  global driver
  print('Start creating index on WikidataInstances')
  try:
    with driver.session() as session:
      session.run("CREATE INDEX ON :WikidataInstance(url)")
    return True
  except ServiceUnavailable:
    return False
   
def createSpatialIndex():
  global driver
  print('Start Spatial indexing')
  try:
    with driver.session() as session:

      session.run("CREATE INDEX ON :Location(location)");
      session.run("CALL spatial.addPointLayer('geom')")
      session.run("CALL apoc.periodic.commit(\"MATCH (l:Location) WHERE NOT (l)<-[:RTREE_REFERENCE]-() WITH l LIMIT 10000 WITH collect(l) as locs call spatial.addNodes('geom', locs) YIELD count return count\", {})")


    return True

  except ServiceUnavailable:
    return False
  
if __name__ == "__main__":
  # parse options
  options = docopt(__doc__)
  
  neo4j_uri = options['--host']
  neo4j_username = options['--neo4j-username']
  neo4j_password = options['--neo4j-password']
  
  print("Neo4j server: " + neo4j_uri)
  
  sys.exit(main())
#!/usr/bin/env python
# encoding: utf-8

"""
Create indexes on neo4j graph database. The indexes are on wikidata instances and spatial indexes.

Usage:
  neo4j_index_database.py -h HOST_ADDRESS --neo4j-username=NEO4J_USER --neo4j-password=<PASSWORD>
  
Options:
  -h HOST_ADDRESS --host=HOST_ADDRESS                           Neo4j Server Host Address
  --neo4j-username=NEO4J_USER                           Neo4j Username
  --neo4j-password=<PASSWORD>                           Neo4j Password
"""
import sys
from docopt import docopt
from neo4j.v1 import GraphDatabase, basic_auth, ServiceUnavailable


def main(argv=None):
  print("Connect to server")
  global driver
  driver = GraphDatabase.driver(neo4j_uri, auth=basic_auth(neo4j_username, neo4j_password), connection_timeout=60)
  createWikidataIndex()
  createSpatialIndex()
  driver.close()
   
def createWikidataIndex():
  global driver
  print('Start creating index on WikidataInstances')
  try:
    with driver.session() as session:
      session.run("CREATE INDEX ON :WikidataInstance(url)")
    return True
  except ServiceUnavailable:
    return False
   
def createSpatialIndex():
  global driver
  print('Start Spatial indexing')
  try:
    with driver.session() as session:

      session.run("CREATE INDEX ON :Location(location)");
      session.run("CALL spatial.addPointLayer('geom')")
      session.run("CALL apoc.periodic.commit(\"MATCH (l:Location) WHERE NOT (l)<-[:RTREE_REFERENCE]-() WITH l LIMIT 10000 WITH collect(l) as locs call spatial.addNodes('geom', locs) YIELD count return count\", {})")


    return True

  except ServiceUnavailable:
    return False
  
if __name__ == "__main__":
  # parse options
  options = docopt(__doc__)
  
  neo4j_uri = options['--host']
  neo4j_username = options['--neo4j-username']
  neo4j_password = options['--neo4j-password']
  
  print("Neo4j server: " + neo4j_uri)
  
  sys.exit(main())
#!/usr/bin/env python # encoding: utf-8 Create indexes on neo4j graph database. The indexes are on wikidata instances and spatial indexes.  Usage:   neo4j_index_database.py -h HOST_ADDRESS --neo4j-username=NEO4J_USER --neo4j-password=<PASSWORD>    Options:   -h HOST_ADDRESS --host=HOST_ADDRESS                           Neo4j Server Host Address   --neo4j-username=NEO4J_USER                           Neo4j Username   --neo4j-password=<PASSWORD>                           Neo4j Password # parse options