Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AttributeFilter } from "@/connector/useGEFetchTypes";

import { createVertexId } from "@/core";
import { SEARCH_TOKENS } from "@/utils";
import { normalizeWithNoSpace as normalize } from "@/utils/testing";

import { UnrepresentableNumberError } from "../../queryValueError";
Expand Down Expand Up @@ -242,6 +243,12 @@ describe("Gremlin > oneHopTemplate", () => {
);
});

it("matches a neighbor ID exactly", () => {
expect(
fragmentFor([{ name: SEARCH_TOKENS.NODE_ID, value: "airport'1" }]),
).toContain(normalize("and(hasId('airport\\'1'))"));
});

it("requires every filter to match", () => {
expect(
fragmentFor([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import type {
NeighborsRequest,
} from "@/connector/useGEFetchTypes";

import { query } from "@/utils";
import { createVertexId } from "@/core";
import { query, SEARCH_TOKENS } from "@/utils";

import { fragment } from "../fragments";

function attributeFilterTemplate({ name, value }: AttributeFilter): string {
if (name === SEARCH_TOKENS.NODE_ID) {
return `hasId(${fragment.id(createVertexId(value))})`;
}
return `has(${fragment.identifier(name)},containing(${fragment.string(value)}))`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AttributeFilter } from "@/connector/useGEFetchTypes";

import { createVertexId } from "@/core";
import { query } from "@/utils";
import { query, SEARCH_TOKENS } from "@/utils";

import oneHopTemplate from "./oneHopTemplate";

Expand Down Expand Up @@ -172,6 +172,12 @@ describe("OpenCypher > oneHopTemplate", () => {
);
});

it("matches a neighbor ID exactly", () => {
expect(
templateFor([{ name: SEARCH_TOKENS.NODE_ID, value: 'airport"1' }]),
).toContain('ID(tgt) = "airport\\"1"');
});

it("requires every filter to match", () => {
expect(
templateFor([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import type {
NeighborsRequest,
} from "@/connector/useGEFetchTypes";

import { query } from "@/utils";
import { createVertexId } from "@/core";
import { query, SEARCH_TOKENS } from "@/utils";

import { fragment } from "../fragments";

const attributeFilterTemplate = ({ name, value }: AttributeFilter): string =>
`tgt.${fragment.identifier(name)} CONTAINS ${fragment.string(value)}`;
function attributeFilterTemplate({ name, value }: AttributeFilter): string {
return name === SEARCH_TOKENS.NODE_ID
? `ID(tgt) = ${fragment.id(createVertexId(value))}`
: `tgt.${fragment.identifier(name)} CONTAINS ${fragment.string(value)}`;
}

/**
* @example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createVertexId } from "@/core";
import { LABELS, query } from "@/utils";
import { LABELS, query, SEARCH_TOKENS } from "@/utils";
import {
normalize as normalizeCollapsed,
normalizeWithNewlines as normalize,
Expand Down Expand Up @@ -340,6 +340,22 @@ describe("oneHopNeighborsTemplate", () => {
);
});

it("should match a neighbor ID exactly", () => {
const template = oneHopNeighborsTemplate({
resourceURI: createVertexId("http://www.example.com/soccer/resource#EPL"),
attributeFilters: [
{
name: SEARCH_TOKENS.NODE_ID,
value: "http://www.example.com/soccer/resource#Arsenal",
},
],
});

expect(template).toContain(
"FILTER(?neighbor = <http://www.example.com/soccer/resource#Arsenal>)",
);
});

it("should produce one existential FILTER EXISTS per filter on the same attribute", () => {
const template = oneHopNeighborsTemplate({
resourceURI: createVertexId("http://www.example.com/soccer/resource#EPL"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AttributeFilter } from "@/connector/useGEFetchTypes";

import { query } from "@/utils";
import { query, SEARCH_TOKENS } from "@/utils";

import {
getLimit,
Expand Down Expand Up @@ -213,13 +213,18 @@ export function findNeighborsUsingFilters({
* makes `&&` across filters unsatisfiable and dominates query cost.
*/
function getFilterTemplate(attributeFilters: AttributeFilter[]) {
const createFilterTemplate = (filter: AttributeFilter) =>
query`
function createFilterTemplate(filter: AttributeFilter) {
if (filter.name === SEARCH_TOKENS.NODE_ID) {
return `FILTER(?neighbor = ${fragment.iri(filter.value)})`;
}

return query`
FILTER EXISTS {
?neighbor ${fragment.iri(filter.name)} ?filterValue .
FILTER(isLiteral(?filterValue) && CONTAINS(LCASE(STR(?filterValue)), LCASE(${fragment.string(filter.value)})))
}
`;
}

return attributeFilters.map(createFilterTemplate).join("\n");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createVertexId, type Vertex } from "@/core";
import { SEARCH_TOKENS } from "@/utils";
import { createTestableVertex } from "@/utils/testing";

import type { BlankNodeItem, BlankNodesMap } from "../types";
Expand Down Expand Up @@ -63,6 +64,23 @@ describe("SPARQL > storedBlankNodeNeighborsRequest", () => {
expect(response.vertices).toStrictEqual([seattle]);
});

it("should match a stored neighbor ID exactly", async () => {
const seattle = airportWith({ city: "Seattle" });
const portland = airportWith({ city: "Portland" });

const response = await storedBlankNodeNeighborsRequest(
blankNodesWithNeighbors([seattle, portland]),
{
resourceURI: blankNodeId,
attributeFilters: [
{ name: SEARCH_TOKENS.NODE_ID, value: String(seattle.id) },
],
},
);

expect(response.vertices).toStrictEqual([seattle]);
});

it("should match the attribute regardless of case", async () => {
const seattle = airportWith({ city: "Seattle" });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { NeighborsResponse } from "@/connector/useGEFetchTypes";

import { SEARCH_TOKENS } from "@/utils";

import type { BlankNodesMap, SPARQLNeighborsRequest } from "../types";

/**
Expand All @@ -26,6 +28,13 @@ export const storedBlankNodeNeighborsRequest = (
}

for (const filter of req.attributeFilters ?? []) {
if (filter.name === SEARCH_TOKENS.NODE_ID) {
if (String(vertex.id) !== filter.value) {
return false;
}
continue;
}

const attrVal = vertex.attributes[filter.name];
if (attrVal == null) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/graph-explorer/src/connector/useGEFetchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export type SchemaResponse = {
/**
* Narrows neighbors to those whose attribute contains the given value.
*
* Only string attributes are filterable, so the value needs no type of its own.
* The node ID search token matches the ID exactly. Only string attributes are
* otherwise filterable, so the value needs no type of its own.
*/
export type AttributeFilter = {
/** Attribute name. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "@/components";
import { useSearchableAttributes } from "@/core";
import useTranslations from "@/hooks/useTranslations";
import { SEARCH_TOKENS } from "@/utils";

let nextFilterId = 1;
export type NodeExpandFilter = {
Expand All @@ -45,10 +46,13 @@ export type NodeExpandFiltersProps = {

function useAttributeOptions(selectedType: string) {
const allSearchableAttributes = useSearchableAttributes(selectedType);
return allSearchableAttributes.map(a => ({
label: a.displayLabel,
value: a.name,
}));
return [
{ label: "ID", value: SEARCH_TOKENS.NODE_ID },
...allSearchableAttributes.map(a => ({
label: a.displayLabel,
value: a.name,
})),
];
}

const NodeExpandFilters = ({
Expand Down