Create Attribute Index using Python API

470
1
Jump to solution
04-10-2023 01:33 PM
mpboyle
Occasional Contributor III

If I view the properties of a layer or table within a hosted feature layer, I'm able to view the existing indexes as a dictionary object.

Is it possible to create an attribute index on a hosted feature layer using the Python API?  If so, are there any examples available?  I can't find any in the Python API docs.  

0 Kudos
1 Solution

Accepted Solutions
mpboyle
Occasional Contributor III

After some testing, this does seem doable using the Python API.  Below is some sample code that others may find useful.

import arcgis

# connect to portal
portal = arcgis.GIS(...your authentication method...)

# set item
item = portal.content.get('...item id...')

# get layers and tables (in my case I was targeting a table)
layers = item.layers
tables = item.tables

# target layer or table
fl = tables['x']  # x is the index of the layer or table

# create attribute index json/dictionary
idx = {'name': '...name of index...',
       'fields': 'Field1, Field2, Field3, etc...',
       'isAscending': True,
       'isUnique': False,  # only set to True if layer or table has unique values
       'description': '...description of index...'}

# add index to layer/table definition --- seems that whether there is 1 or multiple indexes you are adding, the object must be a list
r = fl.manager.add_to_definition({"indexes": [idx]})

# check result
print(r)

 

View solution in original post

1 Reply
mpboyle
Occasional Contributor III

After some testing, this does seem doable using the Python API.  Below is some sample code that others may find useful.

import arcgis

# connect to portal
portal = arcgis.GIS(...your authentication method...)

# set item
item = portal.content.get('...item id...')

# get layers and tables (in my case I was targeting a table)
layers = item.layers
tables = item.tables

# target layer or table
fl = tables['x']  # x is the index of the layer or table

# create attribute index json/dictionary
idx = {'name': '...name of index...',
       'fields': 'Field1, Field2, Field3, etc...',
       'isAscending': True,
       'isUnique': False,  # only set to True if layer or table has unique values
       'description': '...description of index...'}

# add index to layer/table definition --- seems that whether there is 1 or multiple indexes you are adding, the object must be a list
r = fl.manager.add_to_definition({"indexes": [idx]})

# check result
print(r)