Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/clean up queries #206

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changes

- clean up non-functional cypher query style issues

### Deprecated

### Removed

### Fixed

- do not raise server error when search query is not found

### Security

## [0.23.0] - 2024-11-19
Expand Down
2 changes: 1 addition & 1 deletion mex/backend/graph/cypher/exists_merged_item.cql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Returns:
-#>
RETURN EXISTS {
MATCH (:<<node_labels|join("|")>> {identifier: $identifier})
} as exists;
} AS exists;
8 changes: 4 additions & 4 deletions mex/backend/graph/cypher/fetch_extracted_or_rule_items.cql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Returns:
CALL () {
<%- block match_clause -%>
<%- if filter_by_query_string %>
CALL db.index.fulltext.queryNodes("search_index", $query_string)
OPTIONAL CALL db.index.fulltext.queryNodes("search_index", $query_string)
YIELD node AS hit, score
<%- endif %>
OPTIONAL MATCH (n:<<rule_labels|join("|")>>|<<extracted_labels|join("|")>>)
Expand Down Expand Up @@ -47,16 +47,16 @@ CALL () {
label: type(r),
position: r.position,
value: referenced.identifier
} ELSE NULL END as ref
} ELSE NULL END AS ref
UNION
OPTIONAL MATCH (n)-[r]->(nested:<<nested_labels|join("|")>>)
RETURN CASE WHEN nested IS NOT NULL THEN {
label: type(r),
position: r.position,
value: properties(nested)
} ELSE NULL END as ref
} ELSE NULL END AS ref
}
WITH n, collect(ref) as refs
WITH n, collect(ref) AS refs
RETURN n{.*, entityType: head(labels(n)), _refs: refs}
ORDER BY n.identifier, n.entityType ASC
SKIP $skip
Expand Down
2 changes: 1 addition & 1 deletion mex/backend/graph/cypher/fetch_full_text_search_index.cql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Returns:
SHOW INDEXES
YIELD name, labelsOrTypes, properties
WHERE name = "search_index"
RETURN labelsOrTypes as node_labels, properties as search_fields;
RETURN labelsOrTypes AS node_labels, properties AS search_fields;
8 changes: 4 additions & 4 deletions mex/backend/graph/cypher/fetch_identities.cql
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ WHERE
<%- endif -%>
<%- endif %>
RETURN
merged.identifier as stableTargetId,
primary_source.identifier as hadPrimarySource,
n.identifierInPrimarySource as identifierInPrimarySource,
n.identifier as identifier
merged.identifier AS stableTargetId,
primary_source.identifier AS hadPrimarySource,
n.identifierInPrimarySource AS identifierInPrimarySource,
n.identifier AS identifier
ORDER BY n.identifier ASC
LIMIT $limit;
12 changes: 6 additions & 6 deletions mex/backend/graph/cypher/fetch_merged_items.cql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Returns:
CALL () {
<%- block match_clause -%>
<%- if filter_by_query_string %>
CALL db.index.fulltext.queryNodes("search_index", $query_string)
OPTIONAL CALL db.index.fulltext.queryNodes("search_index", $query_string)
YIELD node AS hit, score
<%- endif %>
OPTIONAL MATCH (n:<<rule_labels|join("|")>>|<<extracted_labels|join("|")>>)-[:stableTargetId]->(merged:<<merged_labels|join("|")>>)
Expand All @@ -35,7 +35,7 @@ CALL () {
<<and_()>>merged.identifier = $stable_target_id
<%- endif %>
<<and_()>>ANY(label IN labels(merged) WHERE label IN $labels)
WITH DISTINCT merged as merged
WITH DISTINCT merged AS merged
<%- endblock %>
RETURN COUNT(merged) AS total
}
Expand All @@ -49,18 +49,18 @@ CALL () {
label: type(r),
position: r.position,
value: referenced.identifier
} ELSE NULL END as ref
} ELSE NULL END AS ref
UNION
OPTIONAL MATCH (n)-[r]->(nested:<<nested_labels|join("|")>>)
RETURN CASE WHEN nested IS NOT NULL THEN {
label: type(r),
position: r.position,
value: properties(nested)
} ELSE NULL END as ref
} ELSE NULL END AS ref
}
WITH merged, n, collect(ref) as refs
WITH merged, n, collect(ref) AS refs
ORDER BY merged.identifier, n.identifier, head(labels(n)) ASC
WITH merged, collect(n{.*, entityType: head(labels(n)), _refs: refs}) as n
WITH merged, collect(n{.*, entityType: head(labels(n)), _refs: refs}) AS n
RETURN merged{entityType: head(labels(merged)), identifier: merged.identifier, components: n}
SKIP $skip
LIMIT $limit
Expand Down
6 changes: 3 additions & 3 deletions mex/backend/graph/cypher/merge_edges.cql
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ CALL (source) {
RETURN edge
<%- endfor %>
<%- else %>
RETURN null as edge
RETURN null AS edge
<%- endif %>
}
WITH source, count(edge) as merged, collect(edge) as edges
WITH source, count(edge) AS merged, collect(edge) AS edges
CALL (source, edges) {
MATCH (source)-[outdated_edge]->(:<<merged_labels|join("|")>>)
WHERE NOT outdated_edge IN edges
DELETE outdated_edge
RETURN count(outdated_edge) as pruned
RETURN count(outdated_edge) AS pruned
}
RETURN merged, pruned, edges;
14 changes: 7 additions & 7 deletions mex/backend/graph/cypher/merge_item.cql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<# Upsert the current item with its accociated merged item and all nested objects.
<# Upsert the current item with its associated merged item and all nested objects.

One extracted or rule item is created (or updated) along with its accociated
One extracted or rule item is created (or updated) along with its associated
merged item. In addition, all nested objects of the current item are created as their
own nodes and linked to the current item via edges that have the field names as labels.
We also prune all connected nodes that have nested labels but are not part of
Expand All @@ -10,7 +10,7 @@ Globals:
nested_labels: List of all labels for nested objects

Args:
merged_label: Label for the merged item accociated with the current item,
merged_label: Label for the merged item associated with the current item,
e.g. MergedPerson
current_label: Label of the current extracted or rule item,
e.g. ExtractedPerson or AdditiveVariable
Expand All @@ -28,7 +28,7 @@ Returns:
pruned: Number of nested objects that were removed by this statement
-#>
MERGE (merged:<<merged_label>> {identifier: $stable_target_id})
MERGE (current:<<current_label>> {<<current_constraints|render_constraints>>})-[stableTargetId:stableTargetId {position: 0}]->(merged)
MERGE (current:<<current_label>> {<<current_constraints|render_constraints>>})-[:stableTargetId {position: 0}]->(merged)
ON CREATE SET current = $on_create
ON MATCH SET current += $on_match
<%- for edge_label in nested_edge_labels -%>
Expand All @@ -38,12 +38,12 @@ ON CREATE SET value_<<index>> = $nested_values[<<index>>]
ON MATCH SET value_<<index>> += $nested_values[<<index>>]
<%- endfor %>
WITH current,
[<<range(nested_edge_labels|count)|map("ensure_prefix", "edge_")|join(", ")>>] as edges,
[<<range(nested_edge_labels|count)|map("ensure_prefix", "value_")|join(", ")>>] as values
[<<range(nested_edge_labels|count)|map("ensure_prefix", "edge_")|join(", ")>>] AS edges,
[<<range(nested_edge_labels|count)|map("ensure_prefix", "value_")|join(", ")>>] AS values
CALL (current, values) {
MATCH (current)-[]->(outdated_node:<<nested_labels|join("|")>>)
WHERE NOT outdated_node IN values
DETACH DELETE outdated_node
RETURN count(outdated_node) as pruned
RETURN count(outdated_node) AS pruned
}
RETURN current, edges, values, pruned;
2 changes: 1 addition & 1 deletion mex/backend/merged/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def search_merged_items(
skip: Annotated[int, Query(ge=0, le=10e10)] = 0,
limit: Annotated[int, Query(ge=1, le=100)] = 10,
) -> MergedItemSearch:
"""Search for merged items by query text or by type and id."""
"""Search for merged items by query text or by type and identifier."""
return search_merged_items_in_graph(
q,
identifier,
Expand Down
4 changes: 3 additions & 1 deletion tests/extracted/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,16 @@ def test_search_extracted_items_mocked(
},
),
("?stableTargetId=thisIdDoesNotExist", {"items": [], "total": 0}),
("?q=queryNotFound", {"items": [], "total": 0}),
],
ids=[
"limit 1",
"skip 1",
"entity type contact points",
"full text search",
"stable target id filter",
"empty result",
"identifier not found",
"full text not found",
],
)
@pytest.mark.usefixtures("load_dummy_data")
Expand Down
Loading