14.10.5.5 Creating a Subgraph from Collection Filters

A subgraph can be obtained by using vertex or edge collection filters.

You can create a subgraph from vertex collection filter, as shown in the following code:

// Obtain a vertex collection from an algorithm, query execution or any other way
opg4j> VertexCollection<?> vertexCollection = ...
// Define a filter from the collection
opg4j> var vertexFilter = VertexFilter.fromCollection(vertexCollection)
// Create a subgraph of g containing the matched vertices in the vertex collection and the edges that connect them if any.
opg4j> var newGraph = g.filter(vertexFilter)
// Obtain a vertex collection from an algorithm, query execution or any other way
VertexCollection<?> vertexCollection = ...
// Define a filter from the collection
VertexFilter vertexFilter = VertexFilter.fromCollection(vertexCollection);
// Create a subgraph of g containing the matched vertices in the vertex collection and the edges that connect them if any.
PgxGraph newGraph = g.filter(vertexFilter);

You can create a subgraph from edge collection filter, as shown in the following code:

// Obtain an edge collection from an algorithm, query execution or any other way
opg4j> EdgeCollection edgeCollection = ...
// Define a filter from the collection
opg4j> var edgeFilter = EdgeFilter.fromCollection(edgeCollection)
// Create a subgraph of g containing the matched edges in the collection and their corresponding source and destination vertices.
opg4j> var newGraph = g.filter(edgeFilter)
// Obtain an edge collection from an algorithm, query execution or any other way
EdgeCollection edgeCollection = ...
// Define a filter from the collection
EdgeFilter edgeFilter = EdgeFilter.fromCollection(edgeCollection);
// Create a subgraph of g containing the matched edges in the collection and their corresponding source and destination vertices.
PgxGraph newGraph = g.filter(edgeFilter);