Exemple #1
0
 def node_exist(self, number):
     client = Graph(password=os.getenv('NEO4J_PASSWORD', 'Neo4j'))
     selector = NodeSelector(client)
     selected = selector.select("number", **{"number": number}).first()
     if selected:
         return selected
     return False
Exemple #2
0
class Neo4jClient(object):
    def __init__(self,
                 user='******',
                 password='******',
                 host='localhost',
                 port=7687):
        # self.db = Database("bolt://camelot.example.com:7687")
        self.graph = Graph(bolt=True,
                           host=host,
                           bolt_port=port,
                           user=user,
                           password=password)
        self.selector = NodeSelector(self.graph)

    def get_graph(self, cypher):
        data = self.graph.data(cypher)
        return list(data)

    def get_data_frame(self, cypher):
        data = self.graph.data(cypher)
        return DataFrame(data)

    def select(self, labels, props):
        data = self.selector.select(labels, props)
        return list(data)

    def select_where(self, label, constraint):
        selected = self.selector.select(label).where(constraint)
        return list(selected)

    def cypher_execute(self, cypher):
        result = self.graph.cypher.execute(cypher)
        return result
Exemple #3
0
def get_projects_of_user(current_user, umcn):

    selector = NodeSelector(graph)
    users = selector.select("Person", umcn=umcn)
    user = list(users)[0]

    myprojects = []
    for rel in graph.match(start_node=user, rel_type="IS_USER"):
        project = rel.end_node()
        myprojects.append(project)

    projects_list = []
    for project in myprojects:

        users_list = []
        for rel in graph.match(end_node=project, rel_type="IS_USER"):
            user_umcn = rel.start_node()["umcn"]
            users_list.append(user_umcn)

        admin = ""
        for rel in graph.match(end_node=project, rel_type="IS_ADMIN"):
            admin_umcn = rel.start_node()["umcn"]
            admin = admin_umcn

        project_obj = ({"id": remote(project)._id,
                        "name": project['name'],
                        "client": project['client'],
                        "deadline": project['deadline'],
                        "priority": project['priority'],
                        "users": users_list,
                        "admin": admin
                        })
        projects_list.append(project_obj)

    return jsonify(Projects=projects_list)
Exemple #4
0
def get_all_projects(current_user):

    #if not current_user.is_admin:
    #return jsonify({'message': 'User not authorized!'})

    selector = NodeSelector(graph)
    projects = selector.select("Project")

    projects_list = []
    for project in projects:

        users_list = []
        for rel in graph.match(end_node=project, rel_type="IS_USER"):
            user_umcn = rel.start_node()["umcn"]
            users_list.append(user_umcn)

        admin = ""
        for rel in graph.match(end_node=project, rel_type="IS_ADMIN"):
            admin_umcn = rel.start_node()["umcn"]
            admin = admin_umcn

        project_obj = ({
            "id": remote(project)._id,
            "name": project['name'],
            "client": project['client'],
            "deadline": project['deadline'],
            "priority": project['priority'],
            "users": users_list,
            "admin": admin
        })
        projects_list.append(project_obj)

    return jsonify(Projects=projects_list)
Exemple #5
0
def node_exists_label_in_list(graph, nodeType, propertyname, propertyvalue,
                              **kwargs):
    """
    Check if a node exists given:
     - type
     - a property of type list:
     - a value to search for in it
    On success will return the node.
    On failure, will return none
    """
    graph = graphs[graph]

    selector = NodeSelector(graph)
    search1 = selector.select(nodeType)

    if search1:
        search_res = list(search1)
        for ind_res in search_res:
            if propertyname in ind_res:
                for ind_prop in ind_res[propertyname]:
                    if ind_prop == propertyvalue:
                        ind_res["nodeType"] = list(ind_res.labels())[0]
                        return ind_res
        return None
    else:
        return None
Exemple #6
0
def store_in_neo4j(triple):
	from py2neo import Graph, Node, Relationship ,NodeSelector
	graph = Graph('http://52.83.213.55:7474',user ='******',password = '******')
	# graph = Graph('http://localhost:7474',user = '******',password='******')
	select = NodeSelector(graph)
	# 加载entity
	with open('entity.pkl','rb') as f:
		entities = pickle.load(f)
	entities = list(flatten(entities))
	# 添加所有实体为结点
	for en in entities:
		node = Node('Entity',name= en)
		graph.create(node)
	# 遍历三元组,添加节点的属性,结点间关系等
	for en, kw in triple.items():
		node_1 = select.select('Entity').where(name = en).first()
		for item in kw:
			if item[1] in triple.keys():
				node_2 = select.select('Entity').where(name = item[1]).first()
				relate = Relationship(node_1,item[0],node_2)
				graph.create(relate)
			else:
				node_1[item[0]] = item[1]
				graph.push(node_1)
	print('数据存储完毕')
Exemple #7
0
 def __init__(self, password="******", userid="A0001"):
     self.graph = Graph("http://localhost:7474/db/data/", password=password)
     self.selector = NodeSelector(self.graph)
     # self.locations = get_navigation_location()
     self.is_scene = False
     self.user = self.selector.select("User", userid=userid).first()
     self.usertopics = self.get_usertopics(userid=userid)
     self.address = get_location_by_ip(self.user['city'])
     self.topic = ""
     self.qa_id = get_current_time()
     self.qmemory = deque(maxlen=10)
     self.amemory = deque(maxlen=10)
     self.pmemory = deque(maxlen=10)
     self.cmd_end_scene = ["退出业务场景", "退出场景", "退出", "返回", "结束", "发挥"]
     self.cmd_previous_step = ["上一步", "上一部", "上一页", "上一个"]
     self.cmd_next_step = ["下一步", "下一部", "下一页", "下一个"]
     self.cmd_repeat = ['重复', '再来一个', '再来一遍', '你刚说什么', '再说一遍', '重来']
     self.do_not_know = [
         "这个问题太难了,{robotname}还在学习中", "这个问题{robotname}不会,要么我去问下",
         "您刚才说的是什么,可以再重复一遍吗", "{robotname}刚才走神了,一不小心没听清",
         "{robotname}理解的不是很清楚啦,你就换种方式表达呗", "不如我们换个话题吧", "咱们聊点别的吧",
         "{robotname}正在学习中", "{robotname}正在学习哦", "不好意思请问您可以再说一次吗",
         "额,这个问题嘛。。。", "{robotname}得好好想一想呢", "请问您说什么", "您问的问题好有深度呀",
         "{robotname}没有听明白,您能再说一遍吗"
     ]
Exemple #8
0
 def __init__(self, password="******", userid="A0001"):
     self.graph = Graph("http://localhost:7474/db/data/", password=password)
     self.selector = NodeSelector(self.graph)
     # self.locations = get_navigation_location()
     self.is_scene = False
     self.user = None
     # self.user = self.selector.select("User", userid=userid).first()
     # self.usertopics = self.get_usertopics(userid=userid)
     # self.address = get_location_by_ip(self.user['city'])
     self.topic = ""
     self.behavior = 0  # 场景类型 Add in 2018-6-7
     self.last_step_error = False  # 在场景内上一个问答是否正常 Add in 2018-6-12
     self.qa_id = get_current_time()
     self.qmemory = deque(maxlen=10)
     self.amemory = deque(maxlen=10)
     self.pmemory = deque(maxlen=10)
     # TODO:判断意图是否在当前话题领域内
     self.change_attention = ["换个话题吧", "太无聊了", "没意思", "别说了"]
     self.cmd_end_scene = ["退出业务场景", "退出场景", "退出", "返回", "结束", "发挥"]
     self.cmd_previous_step = ["上一步", "上一部", "上一页", "上一个"]
     self.cmd_next_step = ["下一步", "下一部", "下一页", "下一个"]
     self.cmd_repeat = ["重复", "再来一个", "再来一遍", "你刚说什么", "再说一遍", "重来"]
     self.yes = ["是", "是的", "对", "对的", "好的", "YES", "yes", "结束了"]
     self.no = ["没", "没有", "否", "不", "没有结束", "没结束"]
     self.do_not_know = [
         "这个问题太难了,{robotname}还在学习中", "这个问题{robotname}不会,要么我去问下",
         "您刚才说的是什么,可以再重复一遍吗", "{robotname}刚才走神了,一不小心没听清",
         "{robotname}理解的不是很清楚啦,你就换种方式表达呗", "不如我们换个话题吧", "咱们聊点别的吧",
         "{robotname}正在学习中", "{robotname}正在学习哦", "不好意思请问您可以再说一次吗",
         "额,这个问题嘛。。。", "{robotname}得好好想一想呢", "请问您说什么", "您问的问题好有深度呀",
         "{robotname}没有听明白,您能再说一遍吗"
     ]
 def find_wikidata_node(self, wd_item_id):
     try:
         selector = NodeSelector(self.graph)
         return selector.select('wikidata', wd_item_id=wd_item_id).first()
     except Exception, error:
         _logger.exception()
         return None
 def find_by_name_exactly_match_for_wikidata(self, name):
     try:
         selector = NodeSelector(self.graph)
         return selector.select("wikidata", labels_en=name).first()
     except Exception, error:
         _logger.exception()
         return None
def fill_attribute_graph(graph):
    # graph.delete_all()
    attribute_df = pd.read_csv('data/attributes.csv', usecols=['attribute', 'frequency'])
    coexistence_df = pd.read_csv('data/coexistencesProb.csv', usecols=['attribute1','attribute2','totals','exp','diff','weight'])
    freq_lookup_dict = dict(zip(attribute_df.attribute.values, attribute_df.frequency.values))

    # create nodes
    for attribute, frequency in freq_lookup_dict.items():
        x = Node('Attribute', attribute=str(attribute), frequency=str(frequency))  # create cypher object
        graph.merge(x, 'Attribute', 'attribute')  # merge is important to prevent duplicates

    # create relationships
    for index, row in coexistence_df.iterrows():
        rel_type = 'COOCCURS_WITH'
        try:
            selector = NodeSelector(graph)
            attrubute1_node = selector.select("Attribute", attribute=row.attribute1).first()
            attrubute2_node = selector.select("Attribute", attribute=row.attribute2).first()
            if (attrubute1_node != None
            and attrubute2_node != None
            and row.totals != None
            and row.weight != None):
                graph.merge(Relationship(attrubute1_node, rel_type, attrubute2_node, coexistance=row.totals, weight=row.weight))
        except AttributeError:
            print(index)
            print(row)
    def __init__(self, auth_tweepy, auth_neo4j, n, seed, next_node, w=50):
        # sets current account on Twitter, as specified in the secrets file. Only uses one.
        self.curr_acc = 0
        self.accounts = list(auth_tweepy.items())
        self.api = MHCrawler.auth_tweepy(self.accounts[self.curr_acc][1])

        # authenticates neo4j
        self.graph = MHCrawler.auth_neo4j(auth_neo4j)

        # starts node selector from py2neo
        self.node_selector = NodeSelector(self.graph)

        # starts random walk parameters
        self.seed, self.n, self.w = seed, n, w
        self.next_node = list(self.node_selector.select("User", id=next_node["id"]))[0] \
            if next_node is not None else next_node
        self.previous_node = None

        # checks for uniqueness constraints/index keys in the database and creates them if they don't exist
        if self.graph.schema.get_uniqueness_constraints("User") != ["id"]:
            self.graph.schema.create_uniqueness_constraint("User", "id")
        if self.graph.schema.get_uniqueness_constraints("Tweet") != ["id"]:
            self.graph.schema.create_uniqueness_constraint("Tweet", "id")
        if self.graph.schema.get_uniqueness_constraints("Media") != ["url"]:
            self.graph.schema.create_uniqueness_constraint("Media", "url")
        if "number" not in self.graph.schema.get_indexes("User"):
            self.graph.schema.create_index("User", "number")
Exemple #13
0
 def exists_node(self, label, name):
     ''' checks to see if a node exists by name '''
     print("lookup ", label, " ", name)
     g = Graph(password=self.password)
     selector = NodeSelector(g)
     selected = selector.select(label, name=name)
     ret = list(selected)
     return len(ret), ret
Exemple #14
0
def find_node(graph, node_name, output=None):
    s = NodeSelector(graph)
    try:
        if (output):
            return output(s.select(name=node_name).first())
        else:
            return s.select(name=node_name).first()
    except:
        return '没有这个' + node_name + '节点,查询失败'
 def connect(self):
     print("http://" + self.host + ":" + str(self.port), self.username,
           self.password)
     self.graph = Graph("http://" + self.host + ":" + str(self.port),
                        username=self.username,
                        password=self.password)
     if self.graph != None:
         print("Neo4j Database Connected.")
         self.selector = NodeSelector(self.graph)
 def is_persisted(self):
     """
     Check if this already is persisted to neo4j
     TODO: Make code less hateable
     """
     neo = self.caller.to_neo()
     label = list(neo.labels())[0]
     neo.update(self.caller.props)
     selector = NodeSelector(client)
     return len(list(selector.select(label, **dict(neo)))) > 0
Exemple #17
0
    def add_constant_relationship_test2(self, dict_entities, article_num):
        # key = type, "src" = initial , "dst" = destination
        selector = NodeSelector(self.graph)
        selected = selector.select(article=article_num)

        for i, n1 in enumerate(list(selected)):
            print("Building links... node ", i, "/", len(list(selected)))
            for n2 in list(selected):
                if n1 != n2:
                    self.graph.create(Relationship(n1, "SEMANTIC", n2))
Exemple #18
0
 def get_nodes_by_properties(self, properties):
     """
     :param properties: Dictionary of properties, keys and values.
     :return:  List of node instances.
     """
     conditions = list()
     for key in properties.keys():
         conditions.append('_.{} = "{}"'.format(key, properties[key]))
     selector = NodeSelector(self.graph_db)
     selected = selector.select().where(*conditions)
     return list(selected)
Exemple #19
0
    def new_person(n,y):
		selector = NodeSelector(connection.g)
		node = selector.select("Person",name=n,born=y)
		nodelist=list(node)
		if len(nodelist) >1:
			EditMovie.popupmsg("Person exists!")
		else:
			query = "CREATE (a:Person{name:'"+str(n)+"', born:'"+str(y)+"'})"
			connection.g.run(query)
			time.sleep(5)
			EditMovie.controller.show_page("EditMovie")
Exemple #20
0
    def add_constant_relationship(self, dict_entities, article_num):
        # key = type, "src" = initial , "dst" = destination
        selector = NodeSelector(self.graph)
        selected = selector.select(article=article_num)

        document = Node("DOCUMENT", name=str(article_num), article=article_num)
        self.graph.create(document)

        for i, n1 in enumerate(list(selected)):
            print("Building links... node ", i, "/", len(list(selected)))
            self.graph.create(Relationship(n1, "MENTIONED IN", document))
Exemple #21
0
	def enum_vul(self, TaskID, Cypher_Conditions=None):
		if Cypher_Conditions:
			# selector.select.where not good for use , not support zh_cn just pure cypher
			cypher = 'MATCH (n:HostVul) where n.TaskID="%s" %s RETURN n ' % (TaskID, Cypher_Conditions)
			for data in self.graph.data(cypher):
				yield data["n"]
		else:
			selector = NodeSelector(self.graph)
			selected = selector.select("HostVul", TaskID=TaskID)
			for data in list(selected):
				yield data
Exemple #22
0
 def get_node_by_uuid(self, node_id):
     """
     Retrieve a node from the neo4j database.
     :param node_id: THe node to retrieve.
     :return: The node
     """
     selector = NodeSelector(self.graph_db)
     selected = list(selector.select().where('_.name="{}"'.format(node_id)))
     if selected:
         return selected[0]
     return None
Exemple #23
0
 def __init__(self,
              user='******',
              password='******',
              host='localhost',
              port=7687):
     # self.db = Database("bolt://camelot.example.com:7687")
     self.graph = Graph(bolt=True,
                        host=host,
                        bolt_port=port,
                        user=user,
                        password=password)
     self.selector = NodeSelector(self.graph)
Exemple #24
0
    def new_gk(n,type):
		selector = NodeSelector(connection.g)
		node = selector.select(type,name=n)
		nodelist=list(node)
		if len(nodelist) >1:
			EditMovie.popupmsg("Input already exists!")
		else:
			query = "CREATE (a:"+type+"{name:'"+str(n)+"'})"
			connection.g.run(query)
			#EditMovie.popupmsg("done!")
			time.sleep(5)
			EditMovie.controller.show_page("EditMovie")
Exemple #25
0
def query_node_by_id(node_id):

    database_config = current_app.config['SERVER_CONFIG']['database']

    print("[api_v1_app] /search: connecting to neo4j...")
    database = Graph("{protocol}://{ip}:{port}".format(
        protocol=database_config['connection']['protocol'],
        ip=database_config['connection']['ip'],
        port=database_config['connection']['port']),
                     user=database_config['auth']['user'],
                     password=database_config['auth']['password'])

    selector = NodeSelector(database)
    records = list(selector.select().where(
        "id(_)={node_id}".format(node_id=node_id)))

    if len(records) == 0:
        result = {
            'app': 'npdp-server',
            'api': 'node/ids',
            'timestamp': time.time(),
            'data': {
                'status': 'error',
                'message': "can't find node by id.",
                'request': {
                    'id': node_id
                }
            }
        }
        return jsonify(result)

    record = records[0]
    node_dict = {
        'labels': list(record.labels()),
        'props': dict(record),
        'id': node_id
    }

    result = {
        'app': 'npdp-server',
        'api': 'node/ids',
        'timestamp': time.time(),
        'data': {
            'status': 'ok',
            'request': {
                'id': node_id
            },
            'node': node_dict
        }
    }

    return jsonify(result)
Exemple #26
0
    def __init__(self, **neo4j_params):
        """
        Method to instantiate the class in an object for the neostore.

        :param neo4j_params: dictionary with Neo4J User, Pwd and Database. If host is not default localhost, it also
        needs to be defined in the dictionary.

        :return: Object to handle neostore commands.
        """
        self.graph = self.connect2db(**neo4j_params)
        self.calendar = GregorianCalendar(self.graph)
        self.selector = NodeSelector(self.graph)
        return
Exemple #27
0
def create_sg():
    securityGroups = ec2.describe_security_groups()
    for sg in securityGroups['SecurityGroups']:
        # print(sg)
        selector = NodeSelector(graph)
        graphVPC = selector.select("VPC", vpcId=sg['VpcId']).first()
        tx = graph.begin()
        graphSg = Node("SecurityGroup",
                       securityGroupId=sg['GroupId'],
                       name=sg['GroupName'],
                       parent=graphVPC['name'])
        tx.merge(graphSg)
        tx.commit()
def check_for_relation(node1, node2):
    graph = Graph('http://localhost:7474/db/data', user='******', password='******')
    #neo4j_transaction = graph.begin()

    #******************** TYPE ONE METHOD ***************************#
    #neo4j_transaction.append(" MATCH (n:Word)-[:CO_OCCURENCED]->(m:Word) RETURN n", n=node1, m=node2)

    #************************ TYPE TWO NEO$J ************************#
    selector = NodeSelector(graph)
    selected1 = selector.select("Word", value=node1)
    selected2 = selector.select("Word", value=node2)

    #********************* TYPE 3 NEO$j ****************************#
    #selected1 = Node("Word",value=node1)
    #selected2 = Node("Word",value=node2)
    #selected1 = graph.run("MATCH (a:Word) WHERE a.value={b} RETURN a.value", b=node1)
    #selected2 = graph.run("MATCH (a:Word) WHERE a.value={b} RETURN a.value", b=node2)
    print(selected1)
    print(selected2)
    #print(dir(selected1.values))
    #ab = Relationship(selected1, "CO_OCCURENCED", selected2, bidirectional=True)
    #print(ab)
    #neo4j_transaction.commit()
    #result = neo4j_transaction.commit()
    #result = graph.exists(ab)

    #******************* TYPE 4 **********************************#
    res = graph.match(start_node=selected1, end_node=selected2)
    print(res)

    #print(result)

    if not res:
        print("HAI")
        neo4j_transaction = graph.begin()

        #***************************** TYPE ONE INSERT **************#
        #neo4j_transaction.append(" CREATE (n:Word{value:{a}})-[r:CO_OCCURENCED]->(m:Word{value:{b}}) RETURN r", n=node1, m=node2, a=node1, b=node2)


        #***************************** TYPE 3 INSERT *****************#
        """selected1 = Node("Word",value=node1)
        selected2 = Node("Word",value=node2)
        ab = Relationship(selected1, "CO_OCCURENCED", selected2, bidirectional=True)
        neo4j_transaction.create(ab)
        neo4j_transaction.commit()
        result = graph.exists(ab)
        print(result)"""
        #result = neo4j_transaction.commit()

    return 1
Exemple #29
0
 def __init__(self, password="******", userid="A0001"):
     graphURL = "http://" + getConfig("neo4j", "host") + ":" + getConfig("neo4j", "port")
     self.rdb = None
     self.graph = Graph(graphURL, username = getConfig("neo4j", "user"), password = getConfig("neo4j", "password"))
     self.selector = NodeSelector(self.graph)
     self.user = self.selector.select("User", userid=userid).first()
     if not self.user:
         thispath = os.path.split(os.path.realpath(__file__))[0]
         with open(thispath + '/data/user.txt', 'r', encoding="UTF-8") as file:
             create_user = file.read()
         self.graph.run(create_user)
         self.user = self.selector.select("User", userid=userid).first()
     self.skb = ''
     self.dkb = []
Exemple #30
0
 def __init__(self, password="******", userid="A0001"):
     self.rdb = None
     self.graph = Graph("http://localhost:7474/db/data", password=password)
     self.selector = NodeSelector(self.graph)
     self.user = self.selector.select("User", userid=userid).first()
     if not self.user:
         thispath = os.path.split(os.path.realpath(__file__))[0]
         with open(thispath + '/data/user.txt', 'r',
                   encoding="UTF-8") as file:
             create_user = file.read()
         self.graph.run(create_user)
         self.user = self.selector.select("User", userid=userid).first()
     self.skb = ''
     self.dkb = []
Exemple #31
0
    def init_app(self, app):
        """Initialize Neo4j.
        """
        graph = neo4j.Neo4j(app).gdb
        self.driver = NodeSelector(graph)

        self.set_defaults(app)

        self.register_schema(app)
Exemple #32
0
def get_command_last_run(course_key, graph):
    """
    This information is stored on the course node of a course in neo4j
    Args:
        course_key: a CourseKey
        graph: a py2neo Graph

    Returns: The datetime that the command was last run, converted into
        text, or None, if there's no record of this command last being run.
    """
    selector = NodeSelector(graph)
    course_node = selector.select(
        "course",
        course_key=six.text_type(course_key)
    ).first()

    last_this_command_was_run = None
    if course_node:
        last_this_command_was_run = course_node['time_last_dumped_to_neo4j']

    return last_this_command_was_run
Exemple #33
0
def match(graph, definition):
    selector = NodeSelector(graph)
    result = list(selector.select("Region", **definition))
    return result
Exemple #34
0
class Neo4j(DataLayer):
    """Neo4j data layer access for Eve REST API.
    """

    serializers = {
        'datetime': str_to_date,
        'integer': lambda value: int(value) if value is not None else None,
        'float': lambda value: float(value) if value is not None else None,
    }

    def init_app(self, app):
        """Initialize Neo4j.
        """
        graph = neo4j.Neo4j(app).gdb
        self.driver = NodeSelector(graph)

        self.set_defaults(app)

        self.register_schema(app)

    def set_defaults(self, app):
        """Fill individual resource settings with default settings.
        """
        for resource, settings in app.config['DOMAIN'].items():
            self._set_resource_defaults(resource, settings)

    def _set_resource_defaults(self, resource, settings):
        """Low-level method which sets default values for one resource.
        """
        settings.setdefault('datasource', {})
        ds = settings['datasource']
        ds.setdefault('relation', False)

    def register_schema(self, app):
        """Register schema for Neo4j indexes.

        :param app: Flask application instance.
        """
        for k, v in app.config['DOMAIN'].items():
            if 'datasource' in v and 'source' in v['datasource']:
                label = v['datasource']['source']
            else:
                label = k

            if 'id_field' in v:
                id_field = v['id_field']
            else:
                id_field = app.config['ID_FIELD']

            try:
                self.driver.graph.schema.create_uniqueness_constraint(
                    label, id_field)
            except Exception:
                pass

    def find(self, resource, req, sub_resource_lookup):
        """ Retrieves a set of documents matching a given request.

        :param resource: resource being accessed. You should then use
                         the ``datasource`` helper function to retrieve both
                         the db collection/table and base query (filter), if
                         any.
        :param req: an instance of ``eve.utils.ParsedRequest``. This contains
                    all the constraints that must be fulfilled in order to
                    satisfy the original request (where and sort parts, paging,
                    etc). Be warned that `where` and `sort` expresions will
                    need proper parsing, according to the syntax that you want
                    to support with your driver. For example ``eve.io.Mongo``
                    supports both Python and Mongo-like query syntaxes.
        :param sub_resource_lookup: sub-resoue lookup from the endpoint url.
        """
        label, filter_, fields, sort = self._datasource_ex(resource, [])
        selected = self.driver.select(label)

        if req.where:
            properties = json.loads(req.where)
            selected = selected.where(**properties)

        if req.max_results:
            selected = selected.limit(req.max_results)

        if req.page > 1:
            selected = selected.skip((req.page - 1) * req.max_results)

        return Neo4jResultCollection(selected)

    def find_one(self, resource, req, **lookup):
        """ Retrieves a single document/record. Consumed when a request hits an
        item endpoint (`/people/id/`).

        :param resource: resource being accessed. You should then use the
                         ``datasource`` helper function to retrieve both the
                         db collection/table and base query (filter), if any.
        :param req: an instance of ``eve.utils.ParsedRequest``. This contains
                    all the constraints that must be fulfilled in order to
                    satisfy the original request (where and sort parts, paging,
                    etc). As we are going to only look for one document here,
                    the only req attribute that you want to process here is
                    ``req.projection``.

        :param **lookup: the lookup fields. This will most likely be a record
                         id or, if alternate lookup is supported by the API,
                         the corresponding query.
        """
        document = self.driver.select(resource, **lookup).first()
        return node_to_dict(document) if document else None

    def _node_by_id(self, nodeid, resource):
        label, _, _, _ = self._datasource_ex(resource, [])
        id_field = config.DOMAIN[resource]['id_field']
        lookup = {id_field: nodeid}
        return self.driver.select(label, **lookup).first()

    def insert(self, resource, doc_or_docs):
        """ Inserts a document as a node with a label.

        :param resource: resource being accessed.
        :param doc_or_docs: json document or list of json documents to be added
                            to the database.
        """
        indexes = []
        label, _, _, _ = self._datasource_ex(resource, [])
        id_field = config.DOMAIN[resource]['id_field']
        relation = config.DOMAIN[resource]['datasource']['relation']
        schema = config.DOMAIN[resource]['schema']

        tx = self.driver.graph.begin()
        for document in doc_or_docs:
            if relation:
                properties = prepare_properties(document)
                start_node = self._node_by_id(
                    properties.pop('start_node'),
                    schema['start_node']['data_relation']['resource'])
                end_node = self._node_by_id(
                    properties.pop('end_node'),
                    schema['end_node']['data_relation']['resource'])
                relation = Relationship(
                    start_node, label, end_node, **properties)
                relation[id_field] = str(uuid.uuid4())
                tx.create(relation)
                indexes.append(relation[id_field])
            else:
                node = create_node(label, document)
                node[id_field] = str(uuid.uuid4())
                tx.create(node)
                indexes.append(node[id_field])
        tx.commit()
        return indexes

    def update(self, resource, id_, updates, original):
        """ Updates a graph node.
        :param resource: resource being accessed.
        :param id_: the unique id of the node.
        :param updates: json updates to be performed on the node.
        :param original: definition of the json document that should be
        updated.
        :raise OriginalChangedError: raised if the database layer notices a
        change from the supplied `original` parameter.
        """
        label, _, _, _ = self._datasource_ex(resource, [])
        id_field = config.DOMAIN[resource]['id_field']
        filter_ = {id_field: id_}
        node = self.driver.select(label, **filter_).first()
        if node is None:
            abort(500, description=debug_error_message('Object not existent'))
        properties = prepare_properties(updates)
        node.update(**properties)
        self.driver.graph.push(node)

    def replace(self, resource, id_, document, original):
        """ Replaces a graph node.

        :param resource: resource being accessed.
        :param id_: the unique id of the node.
        :param document: the new json document
        :param original: definition of the json document that should be
                         updated.
        :raise OriginalChangedError: raised if the database layer notices a
                                     change from the supplied `original`
                                     parameter.
        """
        label, _, _, _ = self._datasource_ex(resource, [])
        id_field = config.DOMAIN[resource]['id_field']
        filter_ = {id_field: id_}
        old_node = self.driver.select(label, **filter_).first()

        # Delete the old node
        if old_node is None:
            abort(500, description=debug_error_message('Object not existent'))
        self.remove(resource, filter_)

        # create and insert the new one
        node = create_node(label, document)
        node[id_field] = id_
        self.driver.graph.create(node)

    def remove(self, resource, lookup={}):
        """ Removes a node or an entire set of nodes from a graph label.

        :param resource: resource being accessed. You should then use
                         the ``datasource`` helper function to retrieve
                         the actual datasource name.
        :param lookup: a dict with the query that documents must match in order
                       to qualify for deletion. For single document deletes,
                       this is usually the unique id of the document to be
                       removed.
        """
        datasource, filter_, _, _ = self._datasource_ex(resource, lookup)
        nodes = self.driver.select(datasource, **filter_)

        tx = self.driver.graph.begin()
        for node in nodes:
            remote_node = node.__remote__
            if remote_node:
                statement = 'MATCH (_) WHERE id(_)={node_id} DETACH DELETE _'
                tx.run(statement, node_id=remote_node._id)
                del node.__remote__
        tx.commit()