arcgis.graph module

The arcgis.graph module contains classes and functions for working with ArcGIS Knowledge graphs. The available functions allow for searching and querying the graph data, and viewing the data model of the graph database.

Knowledge graphs consist of entities and the relationships between them, each of which can contain properties which describe their attributes. The data model of the knowledge graph can show you which entities, relationships, and properties are in your database, along with other information about the graph. Performing a search or openCypher query on the graph will return results from the database based on the search or query terms provided.

Note

Applications based on ArcGIS API for Python version 2.0.1 can only communicate with knowledge graphs in an ArcGIS Enterprise 10.9.1 or 11.0 deployment. ArcGIS Enterprise 11.1 includes breaking changes for knowledge graphs. Only applications based on ArcGIS API for Python version 2.1.0 or later will be able to communicate with knowledge graphs in an Enterprise 11.1 deployment. See the ArcGIS Enterprise Knowledge Server documentation for more details.

KnowledgeGraph

class arcgis.graph.KnowledgeGraph(url, *, gis=None)

Provides access to the Knowledge Graph service data model and properties, as well as methods to search and query the graph.

Parameter

Description

url

Knowledge Graph service URL

gis

an authenticated arcigs.gis.GIS object.

# Connect to a Knowledge Graph service:

gis = GIS(url="url",username="username",password="password")

knowledge_graph = KnowledgeGraph(url, gis=gis)
apply_edits(adds=[], updates=[], deletes=[], input_transform=None, cascade_delete=False)

Allows users to add new graph entities/relationships, update existing entities/relationships, or delete existing entities/relationships. For details on how the dictionaries for each of these operations should be structured, please refer to the samples further below.

Parameter

Description

adds

Optional list of dicts. The list of objects to add to the graph, represented in dictionary format.

updates

Optional list of dicts. The list of existent graph objects that are to be updated, represented in dictionary format.

deletes

Optional list of dicts. The list of existent objects to remove from the graph, represented in dictionary format.

input_transform

Optional dict. Allows a user to specify custom quantization parameters for input geometry, which dictate how geometries are compressed and transferred to the server. Defaults to lossless WGS84 quantization.

cascade_delete

Optional boolean. When True, relationships connected to entities that are being deleted will automatically be deleted as well. When False, these relationships must be deleted manually first. Defaults to False.

# example of an add dictionary- include all properties
{
    "_objectType": "entity",
    "_typeName": "Person",
    "_id": "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}"
    "_properties": {
        "name": "PythonAPILover",
        "hometown": "Redlands",
    }
}

# update dictionary- include only properties being changed
{
    "_objectType": "entity",
    "_typeName": "Person",
    "_id": "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}"
    "_properties": {
        "hometown": "Lisbon",
    }
}

# delete dictionary- pass a list of id's to be deleted
{
    "_objectType": "entity",
    "_typeName": "Person",
    "_ids": ["{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}"]
}
Returns

A dict showing the results of the edits.

property datamodel

Returns the datamodel for the Knowledge Graph service

classmethod fromitem(item)

Returns the Knowledge Graph service from an Item

graph_property_adds(type_name, graph_properties)

Adds properties to a named type in the data model

Learn more about adding properties in a knowledge graph

Parameter

Description

type_name

Required string. The entity or relationship type to which the properties will be added.

graph_properties

Required list of dicts. The list of properties to add to the named type, represented in dictionary format.

# example of a shape property to be added to a named type
{
    "name": "MyPointGeometry",
    "alias": "MyPointGeometry",
    "fieldType": "esriFieldTypeGeometry",
    "geometryType": "esriGeometryPoint",
    "hasZ": False,
    "hasM": False,
    "nullable": True,
    "editable": True,
    "visible": True,
    "required": False,
    "isSystemMaintained": False,
    "role": "esriGraphPropertyRegular"
}

# example of an integer property to be added to a named type
{
    "name": "MyInt",
    "alias": "MyInt",
    "fieldType": "esriFieldTypeInteger",
    "nullable": True,
    "editable": True,
    "defaultValue": 123,
    "visible": True,
    "required": False,
    "isSystemMaintained": False,
    "role": "esriGraphPropertyRegular",
    "domain": "MyIntegerDomain"
}
Returns

A dict showing the results of the property adds.

graph_property_delete(type_name, property_name)

Delete a property for a named type in the data model

Learn more about deleting properties in a knowledge graph

Parameter

Description

type_name

Required string. The entity or relationship type containing the property to be deleted.

property_name

Required string. The property to be deleted.

# Delete a named type's property in the data model
delete_result = knowledge_graph.graph_property_delete("Person", "Address")
Returns

A dict showing the results of the property delete.

graph_property_update(type_name, property_name, graph_property, mask)

Updates a property for a named type in the data model

Learn more about updating properties in a knowledge graph

Parameter

Description

type_name

Required string. The entity or relationship type containing the property to be updated.

property_name

Required string. The property to be updated.

graph_property

Required dict. The graph property to be updated, represented in dictionary format.

mask

Required dict. A dictionary representing the properties of the field to be updated.

# example of a shape property to be updated
{
    "name": "MyPointGeometry",
    "alias": "MyPointGeometry",
    "fieldType": "esriFieldTypeGeometry",
    "geometryType": "esriGeometryPoint",
    "hasZ": False,
    "hasM": False,
    "nullable": True,
    "editable": True,
    "visible": True,
    "required": False,
    "isSystemMaintained": False,
    "role": "esriGraphPropertyRegular"
}

# example: update the property's alias
{
    "update_alias": True
}
# OR
{
    "update_name": False,
    "update_alias": True,
    "update_field_type": False,
    "update_geometry_type": False,
    "update_default_value": False,
    "update_nullable": False,
    "update_editable": False,
    "update_visible": False,
    "update_required": False,
    "update_has_z": False,
    "update_has_m": False,
    "update_domain:" False
}
Returns

A dict showing the results of the property update.

named_object_type_adds(entity_types=[], relationship_types=[])

Adds entity and relationship types to the data model

Learn more about adding named types to a knowledge graph

Parameter

Description

entity_types

Optional list of dicts. The list of entity types to add to the data model, represented in dictionary format.

relationship_types

Optional list of dicts. The list of relationship types to add to the data model, represented in dictionary format.

# example of a named type to be added to the data model
{
    "name": "Person",
    "alias": "Person",
    "role": "esriGraphNamedObjectRegular",
    "strict": False,
    "properties": {
        "Name": {
            "name": "Name",
            "alias": "Name",
            "fieldType": "esriFieldTypeString",
            "editable": True,
            "visible": True,
            "required": False,
            "isSystemMaintained": False,
            "role": "esriGraphPropertyRegular"
        },
        "Nickname": {
            "name": "Nickname",
            "alias": "Nickname",
            "fieldType": "esriFieldTypeString",
            "editable": True,
            "visible": True,
            "required": False,
            "isSystemMaintained": False,
            "role": "esriGraphPropertyRegular"
        }
    }
}
Returns

A dict showing the results of the named type adds.

named_object_type_delete(type_name)

Deletes an entity or relationship type in the data model

Learn more about deleting named types in a knowledge graph

Parameter

Description

type_name

Required string. The named type to be deleted.

# Delete a named type in the data model
delete_result = knowledge_graph.named_object_type_delete("Person")
Returns

A dict showing the results of the named type delete.

named_object_type_update(type_name, named_type_update, mask)

Updates an entity or relationship type in the data model

Learn more about updating named types in a knowledge graph

Parameter

Description

type_name

Required string. The named type to be updated.

named_type_update

Required dict. The entity or relationship type to be updated, represented in dictionary format.

mask

Required dict. A dictionary representing the properties of the named type to be updated.

# example of a named type to be updated
{
    "name": "Person",
    "alias": "Person",
    "role": "esriGraphNamedObjectRegular",
    "strict": False
}

# update the named type's alias:
{
    "update_alias": True
}
# OR
{
    "update_name": False,
    "update_alias": True,
    "update_role": False,
    "update_strict": False
}
Returns

A dict showing the results of the named type update.

property properties

Returns the properties of the Knowledge Graph service

query(query)

Queries the Knowledge Graph using openCypher

Learn more about querying a knowledge graph

Parameter

Description

query

Required String. Allows you to return the entities and relationships in a graph, as well as the properties of those entities and relationships, by providing an openCypher query.

# Perform an openCypher query on the knowledge graph
query_result = knowledge_graph.query("MATCH path = (n)-[r]-(n2) RETURN path LIMIT 5")
Returns

List[list]

search(search, category='both')

Allows for the searching of the properties of entities, relationships, or both in the graph using a full-text index.

Learn more about searching a knowledge graph

Parameter

Description

search

Required String. The search to perform on the Knowledge Graph.

category

Optional String. The category is the location of the full text search. This can be isolated to either the entities or the relationships. The default is to look in both.

The allowed values are: both, entities, relationships

Note

Check the service definition for the Knowledge Graph service for valid values of category. Not all services support both.

#Perform a search on the knowledge graph
search_result = knowledge_graph.search("cat")

# Perform a search on only entities in the knowledge graph
searchentities_result = knowledge_graph.search("cat", "entities")
Returns

List[list]