Taxonomy

View as Markdown

Taxonomy

Taxonomy helps you categorize pieces of content within your stack to facilitate easy navigation and retrieval of information.

Taxonomy(stack:)

The Taxonomy(stack:) method initializes a new instance of the Taxonomy class.

NameTypeDescription
stack (required)Stack

The Stack instance for making API requests

Example:

let stack = Contentstack.stack( apiKey: "your_api_key", deliveryToken: "your_delivery_token", environment:"your_environment" ) 
let taxonomy = Taxonomy(stack: stack)

query()

The query() method creates a new Query instance for retrieving taxonomy entries.

Example:

let stack = Contentstack.stack(apiKey: "api_key", deliveryToken: "delivery_token", environment: "environment") 
let taxonomy = Taxonomy(stack: stack) 
let query = taxonomy.query() 
query.where(valueAtKey: "taxonomies.test_taxonomy", .equals("test_term"))
query.limit(10)
query.skip(0)

fetch()

The fetch() method retrieves taxonomy data from Contentstack.

Example:

let stack = Contentstack.stack(apiKey: "api_key", deliveryToken: "delivery_token", environment: "environment") 

let taxonomy = Taxonomy(stack: stack)

let query = taxonomy.query() query.whereKey("title", .equals("Sample Taxonomy")) 

query.fetch { (result:Result<TaxonomyModel, Error>, response) in 

switch result { 

case .success(let taxonomyModel): print("Found taxonomy: \(taxonomyModel.title)") 

case .failure(let error): print("Query error: \(error.localizedDescription)")

 } 

}

where(valueAtKey:operation:)

The where(valueAtKey:operation:) method adds a constraint to the query.

NameTypeDescription
valueAtKeyvalueAtKey

The field key to query on

operationQuery.Operation

The operation to perform

Example:

let stack = Contentstack.stack(apiKey: "api_key", deliveryToken: "delivery_token", environment: "environment") 
let taxonomy = Taxonomy(stack: stack) 
let query = taxonomy.query()
query.where(valueAtKey: "taxonomies.test_taxonomy", .equals("test_term"))

operator()

The where(valueAtKey:operation:) method adds a constraint to the query.

NameTypeDescription
operationQuery.LogicalOperation

The logical operation (.and/.or) with array of queries

Example:

let query1 = taxonomy.query().where(valueAtKey: "taxonomies.test_taxonomy", .equals("term1"))
let query2 = taxonomy.query().where(valueAtKey: "taxonomies.test_taxonomy", .equals("term2"))
query.operator(.or([query1, query2]))

Query Operations

Following are the available Query Operations:

Operations

Description

Example

.equals()

Performs an exact match.

.equals("test_term")

.includes()

Checks if the value exists in the array.

.includes(["term1", "term2"])

.below()

Matches terms below (excluding) the specified value.

.below("parent_term")

.eqBelow()

Matches terms below (including) the specified value.

.eqBelow("parent_term")

.above()

Matches terms above (excluding) the specified value.

.above("child_term")

.eqAbove()

Matches terms above (including) the specified value.

.eqAbove("child_term")

.or()

Combines queries using OR logic

.operator(.or([query1, query2]))

.and()

Combines queries using AND logic

.operator(.and([query1, query2]))