Search code examples
python-3.xneo4jcypherneo4j-driverneo4j-python-driver

Neo4j graph blind search for any node and relationship containing an expression?


I am trying to build a blind search, given an expression/string.

Using Python Neo4j driver I am running:

from neo4j import GraphDatabase
driver = GraphDatabase.driver("neo4j://localhost:7687")
def query_engine(tx, query):
    res = tx.run(query)
    values = [record for record in res]
    return values


def fuzzy_search(tx, search_expression):
    query = f"MATCH (n) WHERE ANY(x in keys(n) WHERE n[x] =~ '(i?){search_expression}.*') RETURN n"
    res = query_engine(tx, query)
    return res


with driver.session() as session:
    result = session.read_transaction(fuzzy_search, "kuku.*")

driver.close()

I know I need to add full text index to make it faster, please advise what is the best practice to define the full text index in Neo4j when I want to perform full graph search on the nodes/relations params? For example, I am searching for 'kuku' in my graph across all nodes and relations and if there are any nodes/relations that contain kuku, I would like to be able to return it as a result.

Additional info: I have added to all my nodes an additional label (FTIndex) and I am able to create a full text index, BUT(!), how can I config it to index ALL nodes available params + to be sure it will be updated if I will add new ones?


Solution

  • You would have to enumerate the properties you want to search for in the full-text index.

    Unfortunately there is no way around that.

    So basically create an index for your label FTIndex and all properties then that index should efficiently find your results.

    In general please don't use string interpolation but parameters, i.e. $search_expression to avoid injection security issues.

    and then

    MATCH (n) 
    WHERE ANY(x in keys(n) 
          WHERE n[x] =~ '(i?)'+$search_expression+'.*') 
    RETURN n