function complex_query_example
Executes a Neo4j Cypher query to find relationships between LIMS_Parameters nodes where either the source or target node name contains a specified search term.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2283 - 2291
moderate
Purpose
This function demonstrates a complex graph database query pattern for searching bidirectional parameter references in a LIMS (Laboratory Information Management System) database. It retrieves nodes and their relationships where parameter names match a search term, useful for discovering dependencies and connections between laboratory parameters.
Source Code
def complex_query_example(search_term, limit=10):
"""Example of a more complex query combining multiple node types"""
query = """
MATCH (source:LIMS_Parameters)-[r:REFERENCES_PARAMETERS]->(target:LIMS_Parameters)
WHERE source.name CONTAINS $search_term OR target.name CONTAINS $search_term
RETURN source, r, target
LIMIT $limit
"""
return run_query(query, {"search_term": search_term, "limit": limit})
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
search_term |
- | - | positional_or_keyword |
limit |
- | 10 | positional_or_keyword |
Parameter Details
search_term: A string value used to filter LIMS_Parameters nodes. The query searches for nodes whose 'name' property contains this term (case-sensitive substring match). Expected to be a non-empty string representing a parameter name or partial name.
limit: An integer that restricts the maximum number of results returned by the query. Defaults to 10. Should be a positive integer; higher values may impact performance on large datasets.
Return Value
Returns the result of the run_query() function call, which typically returns a list of records containing matched source nodes, relationship objects (r), and target nodes. Each record represents a REFERENCES_PARAMETERS relationship where at least one node's name contains the search term. The exact return type depends on the run_query() implementation, but is likely a list of dictionaries or Neo4j Record objects with 'source', 'r', and 'target' keys.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j is configured
from neo4j import GraphDatabase
# Define or import run_query function
def run_query(query, params):
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
with driver.session() as session:
result = session.run(query, params)
return [record.data() for record in result]
driver.close()
# Use the function
results = complex_query_example('temperature', limit=5)
for record in results:
print(f"Source: {record['source']['name']}")
print(f"Relationship: {record['r'].type}")
print(f"Target: {record['target']['name']}")
print('---')
Best Practices
- Ensure the run_query() function is properly defined with error handling and connection management
- Consider adding input validation for search_term to prevent empty or None values
- The CONTAINS operator is case-sensitive; consider using toLower() in Cypher for case-insensitive searches
- Be cautious with the limit parameter on large datasets; too high values may cause performance issues
- Ensure proper Neo4j connection pooling and session management in the run_query() implementation
- Consider adding pagination support for better handling of large result sets
- Add error handling for cases where the database connection fails or the query syntax is invalid
- Document the expected schema (node labels and relationship types) for users of this function
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_lims_parameters_with_references_parameters_lims_parameters 76.3% similar
-
function get_lims_testparameters_with_references_parameters_lims_parameters 74.7% similar
-
function get_all_lims_parameters 74.1% similar
-
function get_lims_sampletestresults_with_references_testparameters_lims_testparameters 71.9% similar
-
function get_lims_testparameters_with_references_tests_lims_tests 71.4% similar